Skip to content

Commit

Permalink
GuiTexture resource (Kenix3#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai authored Mar 5, 2024
1 parent f0f91b4 commit d70fd7d
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 38 deletions.
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ set(Source_Files__Window__Gui
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/Font.cpp
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/FontFactory.h
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/FontFactory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/GuiTexture.h
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/GuiTexture.cpp
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/GuiTextureFactory.h
${CMAKE_CURRENT_SOURCE_DIR}/window/gui/resource/GuiTextureFactory.cpp
)

source_group("window/gui" FILES ${Source_Files__Window__Gui})
Expand Down
43 changes: 16 additions & 27 deletions src/window/gui/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "resource/File.h"
#include <stb/stb_image.h>
#include "window/gui/Fonts.h"
#include "window/gui/resource/GuiTextureFactory.h"

#ifdef __WIIU__
#include <gx2/registers.h> // GX2SetViewport / GX2SetScissor
Expand Down Expand Up @@ -150,6 +151,10 @@ void Gui::Init(GuiWindowInitData windowImpl) {
GetGuiWindow("Console")->Init();
GetGameOverlay()->Init();

Context::GetInstance()->GetResourceManager()->GetResourceLoader()->RegisterResourceFactory(
std::make_shared<ResourceFactoryBinaryGuiTextureV0>(), RESOURCE_FORMAT_BINARY, "GuiTexture",
static_cast<uint32_t>(RESOURCE_TYPE_GUI_TEXTURE), 0);

ImGuiWMInit();
ImGuiBackendInit();
#ifdef __SWITCH__
Expand Down Expand Up @@ -230,38 +235,22 @@ void Gui::ImGuiBackendInit() {
}

void Gui::LoadTextureFromRawImage(const std::string& name, const std::string& path) {
const auto res = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFileRaw(path);

if (!res) {
SPDLOG_ERROR("Failed to load resource");
return;
}
if (!res->Buffer || res->Buffer->empty()) {
SPDLOG_ERROR("Buffer is null or empty");
return;
}

GuiTexture asset;
asset.Width = 0;
asset.Height = 0;
uint8_t* imgData = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(res->Buffer->data()), res->Buffer->size(),
&asset.Width, &asset.Height, nullptr, 4);

if (imgData == nullptr) {
SPDLOG_ERROR("Error loading imgui texture {}", stbi_failure_reason());
return;
}
auto initData = std::make_shared<ResourceInitData>();
initData->Format = RESOURCE_FORMAT_BINARY;
initData->Type = static_cast<uint32_t>(RESOURCE_TYPE_GUI_TEXTURE);
initData->ResourceVersion = 0;
auto guiTexture = std::static_pointer_cast<GuiTexture>(
Context::GetInstance()->GetResourceManager()->LoadResource(path, false, initData));

GfxRenderingAPI* api = gfx_get_current_rendering_api();

// TODO: Nothing ever unloads the texture from Fast3D here.
asset.RendererTextureId = api->new_texture();
api->select_texture(0, asset.RendererTextureId);
guiTexture->Metadata.RendererTextureId = api->new_texture();
api->select_texture(0, guiTexture->Metadata.RendererTextureId);
api->set_sampler_parameters(0, false, 0, 0);
api->upload_texture(imgData, asset.Width, asset.Height);
api->upload_texture(guiTexture->Data, guiTexture->Metadata.Width, guiTexture->Metadata.Height);

mGuiTextures[name] = asset;
stbi_image_free(imgData);
mGuiTextures[name] = guiTexture->Metadata;
}

bool Gui::SupportsViewports() {
Expand Down Expand Up @@ -831,7 +820,7 @@ void Gui::LoadGuiTexture(const std::string& name, const std::string& path, const
texBuffer[pixel * 4 + 3] *= tint.w;
}

GuiTexture asset;
GuiTextureMetadata asset;
asset.RendererTextureId = api->new_texture();
asset.Width = res->Width;
asset.Height = res->Height;
Expand Down
9 changes: 2 additions & 7 deletions src/window/gui/Gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "window/gui/GuiWindow.h"
#include "window/gui/GuiMenuBar.h"
#include "libultraship/libultra/controller.h"
#include "window/gui/resource/GuiTexture.h"

namespace LUS {

Expand Down Expand Up @@ -100,18 +101,12 @@ class Gui {
int16_t GetIntegerScaleFactor();

private:
struct GuiTexture {
uint32_t RendererTextureId;
int32_t Width;
int32_t Height;
};

GuiWindowInitData mImpl;
ImGuiIO* mImGuiIo;
bool mNeedsConsoleVariableSave;
std::shared_ptr<GameOverlay> mGameOverlay;
std::shared_ptr<GuiMenuBar> mMenuBar;
std::map<std::string, GuiTexture> mGuiTextures;
std::map<std::string, GuiTextureMetadata> mGuiTextures;
std::map<std::string, std::shared_ptr<GuiWindow>> mGuiWindows;
};
} // namespace LUS
Expand Down
4 changes: 0 additions & 4 deletions src/window/gui/resource/FontFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ std::shared_ptr<IResource> ResourceFactoryBinaryFontV0::ReadResource(std::shared
font->Data = new char[font->DataSize];
reader->Read(font->Data, font->DataSize);

// for (uint32_t i = 0; i < dataSize; i++) {
// font->Data.push_back(reader->ReadChar());
// }

return font;
}
} // namespace LUS
20 changes: 20 additions & 0 deletions src/window/gui/resource/GuiTexture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "GuiTexture.h"

namespace LUS {
GuiTexture::GuiTexture() : Resource(std::shared_ptr<ResourceInitData>()) {
}

GuiTexture::~GuiTexture() {
if (Data != nullptr) {
stbi_image_free(Data);
}
}

void* GuiTexture::GetPointer() {
return Data;
}

size_t GuiTexture::GetPointerSize() {
return DataSize * sizeof(uint8_t);
}
} // namespace LUS
29 changes: 29 additions & 0 deletions src/window/gui/resource/GuiTexture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "resource/Resource.h"
#include <stb/stb_image.h>

namespace LUS {
#define RESOURCE_TYPE_GUI_TEXTURE 0x47544558 // GTEX

struct GuiTextureMetadata {
uint32_t RendererTextureId;
int32_t Width;
int32_t Height;
};

class GuiTexture : public Resource<void> {
public:
using Resource::Resource;

GuiTexture();
~GuiTexture();

void* GetPointer() override;
size_t GetPointerSize() override;

uint8_t* Data;
size_t DataSize;
GuiTextureMetadata Metadata;
};
}; // namespace LUS
28 changes: 28 additions & 0 deletions src/window/gui/resource/GuiTextureFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "window/gui/resource/GuiTextureFactory.h"
#include "window/gui/resource/GuiTexture.h"
#include "spdlog/spdlog.h"

namespace LUS {
std::shared_ptr<IResource> ResourceFactoryBinaryGuiTextureV0::ReadResource(std::shared_ptr<File> file) {
if (!FileHasValidFormatAndReader(file)) {
return nullptr;
}

auto guiTexture = std::make_shared<GuiTexture>(file->InitData);
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);

guiTexture->DataSize = file->Buffer->size();
guiTexture->Metadata.Width = 0;
guiTexture->Metadata.Height = 0;
guiTexture->Data =
stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(file->Buffer->data()), guiTexture->DataSize,
&guiTexture->Metadata.Width, &guiTexture->Metadata.Height, nullptr, 4);

if (guiTexture->Data == nullptr) {
SPDLOG_ERROR("Error loading imgui texture {}", stbi_failure_reason());
return nullptr;
}

return guiTexture;
}
} // namespace LUS
11 changes: 11 additions & 0 deletions src/window/gui/resource/GuiTextureFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "resource/Resource.h"
#include "resource/ResourceFactoryBinary.h"

namespace LUS {
class ResourceFactoryBinaryGuiTextureV0 : public ResourceFactoryBinary {
public:
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
};
}; // namespace LUS

0 comments on commit d70fd7d

Please sign in to comment.