Skip to content

Commit

Permalink
[resourcecache] simplified how resources work so they only need one f…
Browse files Browse the repository at this point in the history
…ile path
  • Loading branch information
PanosK92 committed Nov 30, 2024
1 parent fecc46b commit 419d69f
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 164 deletions.
24 changes: 12 additions & 12 deletions editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace
}
}

Editor::Editor(const std::vector<std::string>& args)
Editor::Editor(const vector<string>& args)
{
Spartan::Engine::Initialize(args);
ImGui::CreateContext();
Expand Down Expand Up @@ -197,30 +197,30 @@ void Editor::BeginWindow()
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoNavFocus;

ImGuiStyle& style = ImGui::GetStyle();

// set window position and size
const ImGuiViewport* viewport = ImGui::GetMainViewport();
const float padding_offset = 2.0f * (style.FramePadding.y - TitleBar::GetPadding().y) - 1.0f;
const float offset_y = widget_menu_bar ? widget_menu_bar->GetHeight() + padding_offset : 0;

ImGui::SetNextWindowPos(ImVec2(viewport->Pos.x, viewport->Pos.y - offset_y));
ImGui::SetNextWindowSize(ImVec2(viewport->Size.x, viewport->Size.y - offset_y));
ImGui::SetNextWindowViewport(viewport->ID);

// set window style
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowBgAlpha(0.0f);

// begin window
std::string name = "##main_window";
bool open = true;
ImGui::Begin(name.c_str(), &open, window_flags);
ImGui::PopStyleVar(3);

// begin dock space
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DockingEnable)
{
Expand All @@ -232,24 +232,24 @@ void Editor::BeginWindow()
ImGui::DockBuilderRemoveNode(window_id);
ImGui::DockBuilderAddNode(window_id, ImGuiDockNodeFlags_None);
ImGui::DockBuilderSetNodeSize(window_id, ImGui::GetMainViewport()->Size);

// dockBuilderSplitNode(ImGuiID node_id, ImGuiDir split_dir, float size_ratio_for_node_at_dir, ImGuiID* out_id_dir, ImGuiID* out_id_other);
ImGuiID dock_main_id = window_id;
ImGuiID dock_right_id = ImGui::DockBuilderSplitNode(dock_main_id, ImGuiDir_Right, 0.17f, nullptr, &dock_main_id);
ImGuiID dock_right_down_id = ImGui::DockBuilderSplitNode(dock_right_id, ImGuiDir_Down, 0.6f, nullptr, &dock_right_id);
ImGuiID dock_down_id = ImGui::DockBuilderSplitNode(dock_main_id, ImGuiDir_Down, 0.22f, nullptr, &dock_main_id);
ImGuiID dock_down_right_id = ImGui::DockBuilderSplitNode(dock_down_id, ImGuiDir_Right, 0.5f, nullptr, &dock_down_id);

// dock windows
ImGui::DockBuilderDockWindow("World", dock_right_id);
ImGui::DockBuilderDockWindow("Properties", dock_right_down_id);
ImGui::DockBuilderDockWindow("Console", dock_down_id);
ImGui::DockBuilderDockWindow("Assets", dock_down_right_id);
ImGui::DockBuilderDockWindow("Viewport", dock_main_id);

ImGui::DockBuilderFinish(dock_main_id);
}

ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 0.0f);
ImGui::DockSpace(window_id, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_PassthruCentralNode);
ImGui::PopStyleVar();
Expand Down
2 changes: 1 addition & 1 deletion editor/Widgets/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void Properties::Inspect(const shared_ptr<Entity> entity)
// If we were previously inspecting a material, save the changes
if (!m_inspected_material.expired())
{
m_inspected_material.lock()->SaveToFile(m_inspected_material.lock()->GetResourceFilePathNative());
m_inspected_material.lock()->SaveToFile(m_inspected_material.lock()->GetResourceFilePath());
}
m_inspected_material.reset();
}
Expand Down
9 changes: 2 additions & 7 deletions editor/Widgets/ResourceViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ void ResourceViewer::OnTickVisible()
ImGuiTableFlags_ScrollY; // Enable vertical scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size.

static ImVec2 size = ImVec2(-1.0f);
if (ImGui::BeginTable("##Widget_ResourceCache", 6, flags, size))
if (ImGui::BeginTable("##Widget_ResourceCache", 5, flags, size))
{
// Headers
ImGui::TableSetupColumn("Type");
ImGui::TableSetupColumn("ID");
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Path");
ImGui::TableSetupColumn("Path (native)");
ImGui::TableSetupColumn("Size");
ImGui::TableHeadersRow();

Expand All @@ -106,12 +105,8 @@ void ResourceViewer::OnTickVisible()
ImGui::TableSetColumnIndex(3);
ImGui::Text(resource->GetResourceFilePath().c_str());

// Path (native)
ImGui::TableSetColumnIndex(4);
ImGui::Text(resource->GetResourceFilePathNative().c_str());

// Memory
ImGui::TableSetColumnIndex(5);
ImGui::TableSetColumnIndex(4);
print_memory(object->GetObjectSize());
}
}
Expand Down
28 changes: 17 additions & 11 deletions editor/Widgets/TitleBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,28 @@ void TitleBar::EntryWorld()

ImGui::Separator();

if (ImGui::MenuItem("Load"))
// the engine has changed a lot, so I need to re-write resource cache serialization/deserialization
// grey out the options so users know that the functionality is part of the engine but currently disabled
ImGui::BeginDisabled(true);
{
ShowWorldLoadDialog();
}
if (ImGui::MenuItem("Load"))
{
ShowWorldLoadDialog();
}

ImGui::Separator();
ImGui::Separator();

if (ImGui::MenuItem("Save", "Ctrl+S"))
{
ShowWorldSaveDialog();
}
if (ImGui::MenuItem("Save", "Ctrl+S"))
{
ShowWorldSaveDialog();
}

if (ImGui::MenuItem("Save As...", "Ctrl+S"))
{
ShowWorldSaveDialog();
if (ImGui::MenuItem("Save As...", "Ctrl+S"))
{
ShowWorldSaveDialog();
}
}
ImGui::EndDisabled();

ImGui::EndMenu();
}
Expand Down
14 changes: 0 additions & 14 deletions runtime/Core/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,6 @@ namespace Spartan
return extension;
}

string FileSystem::NativizeFilePath(const string& path)
{
const string file_path_no_ext = GetFilePathWithoutExtension(path);

if (IsSupportedAudioFile(path)) return file_path_no_ext + EXTENSION_AUDIO;
if (IsSupportedImageFile(path)) return file_path_no_ext + EXTENSION_TEXTURE;
if (IsSupportedModelFile(path)) return file_path_no_ext + EXTENSION_MODEL;
if (IsSupportedFontFile(path)) return file_path_no_ext + EXTENSION_FONT;
if (IsSupportedShaderFile(path)) return file_path_no_ext + EXTENSION_SHADER;

SP_LOG_WARNING("Failed to nativize file path");
return path;
}

vector<string> FileSystem::GetDirectoriesInDirectory(const string& path)
{
vector<string> directories;
Expand Down
1 change: 0 additions & 1 deletion runtime/Core/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ namespace Spartan
static std::string GetFilePathWithoutExtension(const std::string& path);
static std::string ReplaceExtension(const std::string& path, const std::string& extension);
static std::string GetExtensionFromFilePath(const std::string& path);
static std::string NativizeFilePath(const std::string& path);
static std::string GetRelativePath(const std::string& path);
static std::string GetWorkingDirectory();
static std::string GetRootDirectory(const std::string& path);
Expand Down
8 changes: 4 additions & 4 deletions runtime/Rendering/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ namespace Spartan

textureNode.append_attribute("texture_type").set_value(i);
textureNode.append_attribute("texture_name").set_value(m_textures[i] ? m_textures[i]->GetObjectName().c_str() : "");
textureNode.append_attribute("texture_path").set_value(m_textures[i] ? m_textures[i]->GetResourceFilePathNative().c_str() : "");
textureNode.append_attribute("texture_path").set_value(m_textures[i] ? m_textures[i]->GetResourceFilePath().c_str() : "");
}

doc.save_file(file_path.c_str());
Expand Down Expand Up @@ -274,7 +274,7 @@ namespace Spartan
if (!texture)
continue;

if (texture->GetResourceFilePathNative() == path)
if (texture->GetResourceFilePath() == path)
return true;
}

Expand All @@ -297,7 +297,7 @@ namespace Spartan
if (!HasTextureOfType(texture_type))
return "";

return m_textures[static_cast<uint32_t>(texture_type)]->GetResourceFilePathNative();
return m_textures[static_cast<uint32_t>(texture_type)]->GetResourceFilePath();
}

vector<string> Material::GetTexturePaths()
Expand All @@ -308,7 +308,7 @@ namespace Spartan
if (!texture)
continue;

paths.emplace_back(texture->GetResourceFilePathNative());
paths.emplace_back(texture->GetResourceFilePath());
}

return paths;
Expand Down
2 changes: 1 addition & 1 deletion runtime/Rendering/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ namespace Spartan
SP_ASSERT(entity != nullptr);

// create a file path for this material (required for the material to be able to be cached by the resource cache)
const string spartan_asset_path = FileSystem::GetDirectoryFromFilePath(GetResourceFilePathNative()) + material->GetObjectName() + EXTENSION_MATERIAL;
const string spartan_asset_path = FileSystem::GetDirectoryFromFilePath(GetResourceFilePath()) + material->GetObjectName() + EXTENSION_MATERIAL;
material->SetResourceFilePath(spartan_asset_path);

// create a Renderable and pass the material to it
Expand Down
34 changes: 7 additions & 27 deletions runtime/Resource/IResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,14 @@ namespace Spartan

void SetResourceFilePath(const std::string& path)
{
const bool is_native_file = FileSystem::IsEngineMaterialFile(path) || FileSystem::IsEngineModelFile(path);
const std::string file_path_relative = FileSystem::GetRelativePath(path);

// foreign file
if (!FileSystem::IsEngineFile(path))
{
m_resource_file_path_foreign = file_path_relative;
m_resource_file_path_native = FileSystem::NativizeFilePath(file_path_relative);
}
// engine file
else
{
m_resource_file_path_foreign.clear();
m_resource_file_path_native = file_path_relative;
}

m_resource_directory = FileSystem::GetDirectoryFromFilePath(file_path_relative);
m_object_name = FileSystem::GetFileNameWithoutExtensionFromFilePath(file_path_relative);
m_resource_file_path = FileSystem::GetRelativePath(path);
m_object_name = FileSystem::GetFileNameWithoutExtensionFromFilePath(m_resource_file_path);
}

ResourceType GetResourceType() const { return m_resource_type; }
const char* GetResourceTypeCstr() const { return typeid(*this).name(); }
bool HasFilePathNative() const { return !m_resource_file_path_native.empty(); }
const std::string& GetResourceFilePath() const { return m_resource_file_path_foreign; }
const std::string& GetResourceFilePathNative() const { return m_resource_file_path_native; }
const std::string& GetResourceDirectory() const { return m_resource_directory; }
ResourceType GetResourceType() const { return m_resource_type; }
const char* GetResourceTypeCstr() const { return typeid(*this).name(); }
const std::string& GetResourceFilePath() const { return m_resource_file_path; }
const std::string& GetResourceDirectory() const { return FileSystem::GetDirectoryFromFilePath(m_resource_file_path); }

// flags
void SetFlag(const uint32_t flag, bool enabled = true)
Expand Down Expand Up @@ -117,8 +99,6 @@ namespace Spartan
uint32_t m_flags = 0;

private:
std::string m_resource_directory;
std::string m_resource_file_path_native;
std::string m_resource_file_path_foreign;
std::string m_resource_file_path;
};
}
70 changes: 5 additions & 65 deletions runtime/Resource/ResourceCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ namespace Spartan
SP_SUBSCRIBE_TO_EVENT(EventType::WorldClear, SP_EVENT_HANDLER_STATIC(Shutdown));
}

bool ResourceCache::IsCached(const string& resource_file_path_native, const ResourceType resource_type)
bool ResourceCache::IsCached(const string& file_path, const ResourceType resource_type)
{
SP_ASSERT(!resource_file_path_native.empty());
SP_ASSERT(!file_path.empty());

lock_guard<mutex> guard(m_mutex);

Expand All @@ -77,7 +77,7 @@ namespace Spartan
if (resource->GetResourceType() != resource_type)
continue;

if (resource_file_path_native == resource->GetResourceFilePathNative())
if (file_path == resource->GetResourceFilePath())
return true;
}

Expand Down Expand Up @@ -148,72 +148,12 @@ namespace Spartan

void ResourceCache::Serialize()
{
// create resource list file
string file_path = GetProjectDirectoryAbsolute() + World::GetName() + ".resource";
auto file = make_unique<FileStream>(file_path, FileStream_Write);
if (!file->IsOpen())
{
SP_LOG_ERROR("Failed to open file.");
return;
}

const uint32_t resource_count = GetResourceCount();

// start progress report
ProgressTracker::GetProgress(ProgressType::Resource).Start(resource_count, "Loading resources...");

// save resource count
file->Write(resource_count);

// save all the currently used resources to disk
for (shared_ptr<IResource>& resource : m_resources)
{
if (resource->HasFilePathNative())
{
SP_ASSERT_MSG(!resource->GetResourceFilePathNative().empty(), "Resources must have a native file path");
SP_ASSERT_MSG(resource->GetResourceType() != ResourceType::Max, "Resources must have a type");

file->Write(resource->GetResourceFilePathNative()); // file path
file->Write(static_cast<uint32_t>(resource->GetResourceType())); // type
resource->SaveToFile(resource->GetResourceFilePathNative()); // save
}

// update progress
ProgressTracker::GetProgress(ProgressType::Resource).JobDone();
}
// todo: since we won't be using custom file formats, we just need to save the resource paths, simple and reliable
}

void ResourceCache::Deserialize()
{
// open file
string file_path = GetProjectDirectoryAbsolute() + World::GetName() + ".resource";
unique_ptr<FileStream> file = make_unique<FileStream>(file_path, FileStream_Read);
if (!file->IsOpen())
return;

// go through each resource and load it
const uint32_t resource_count = file->ReadAs<uint32_t>();
for (uint32_t i = 0; i < resource_count; i++)
{
string file_path = file->ReadAs<string>();
const ResourceType type = static_cast<ResourceType>(file->ReadAs<uint32_t>());

switch (type)
{
case ResourceType::Mesh:
Load<Mesh>(file_path);
break;
case ResourceType::Material:
Load<Material>(file_path);
break;
case ResourceType::Texture:
Load<RHI_Texture>(file_path);
break;
case ResourceType::Audio:
Load<AudioClip>(file_path);
break;
}
}
// todo: we just need to load the resource paths, simple and reliable
}

void ResourceCache::Shutdown()
Expand Down
Loading

0 comments on commit 419d69f

Please sign in to comment.