diff --git a/Keishiki/Keishiki.cpp b/Keishiki/Keishiki.cpp index 03b60a2..6751fc6 100644 --- a/Keishiki/Keishiki.cpp +++ b/Keishiki/Keishiki.cpp @@ -94,7 +94,7 @@ namespace K { return false; } - logo = K::Resource::LoadStill("Keishiki.png"); + logo = Resource::Load("Keishiki.png"); state.width = 1280; state.height = 550; diff --git a/Keishiki/Resource.cpp b/Keishiki/Resource.cpp index 58b98b0..1ab92f0 100644 --- a/Keishiki/Resource.cpp +++ b/Keishiki/Resource.cpp @@ -1,12 +1,27 @@ #include -#include -#include #include #include #include +#include +#include namespace { + bgfx::ShaderHandle LoadShader(const std::filesystem::path& file_path) { + FILE *file = fopen(file_path.c_str(), "rb"); + fseek(file, 0, SEEK_END); + long fileSize = ftell(file); + fseek(file, 0, SEEK_SET); + + const bgfx::Memory *mem = bgfx::alloc(fileSize + 1); + fread(mem->data, 1, fileSize, file); + mem->data[mem->size - 1] = '\0'; + fclose(file); + + return bgfx::createShader(mem); + } + std::filesystem::path shader_path{}; + std::thread reload_worker; } @@ -25,49 +40,37 @@ namespace K::Resource { Resource *fallback_still; - Resource *LoadStill(const std::filesystem::path& file) { + template <> + Resource *Load(const std::filesystem::path& p) { std::scoped_lock lk{resource_lock}; - Resource res{.last_updated = std::filesystem::last_write_time(file)}; - res.buf = stbi_load(file.c_str(), &res.w, &res.h, &res.channels, 0); + Resource res{.last_updated = std::filesystem::last_write_time(p)}; + res.buf = stbi_load(p.c_str(), &res.w, &res.h, &res.channels, 0); if (res.buf == nullptr) { - K::LogError("Image loading failed for " + file.string()); + K::LogError("Image loading failed for " + p.string()); return nullptr; } const bgfx::Memory *ref = bgfx::makeRef(res.buf, res.w * res.h * res.channels * sizeof(u8)); res.format = res.channels == 4 ? bgfx::TextureFormat::RGBA8 : bgfx::TextureFormat::RGB8; res.tex = bgfx::createTexture2D( - res.w, - res.h, - false, - 1, - res.format, - BGFX_TEXTURE_NONE | BGFX_SAMPLER_UVW_CLAMP, - ref); - res.filename = file.filename(); - auto [it, check] = resources.emplace(file, res); + res.w, + res.h, + false, + 1, + res.format, + BGFX_TEXTURE_NONE | BGFX_SAMPLER_UVW_CLAMP, + ref); + res.filename = p.filename(); + auto [it, check] = resources.emplace(p, res); return &std::get>(it->second); } - bgfx::ShaderHandle LoadShader(const std::filesystem::path& file_path) { - FILE *file = fopen(file_path.c_str(), "rb"); - fseek(file, 0, SEEK_END); - long fileSize = ftell(file); - fseek(file, 0, SEEK_SET); - - const bgfx::Memory *mem = bgfx::alloc(fileSize + 1); - fread(mem->data, 1, fileSize, file); - mem->data[mem->size - 1] = '\0'; - fclose(file); - - return bgfx::createShader(mem); - } - - Resource *LoadShaderProgram(const String& shader_name) { + template <> + Resource *Load(const std::filesystem::path& p) { std::scoped_lock lk{resource_lock}; - const auto frag = shader_path / (shader_name + ".frag.bin"), - vert = shader_path / (shader_name + ".vert.bin"); - auto [iter, check] = resources.emplace(shader_name, Resource{ - .name = shader_name, + const auto frag = shader_path / (p.string() + ".frag.bin"), + vert = shader_path / (p.string() + ".vert.bin"); + auto [iter, check] = resources.emplace(p, Resource{ + .name = p, .frag_last_updated = std::filesystem::last_write_time(frag), .pg = bgfx::createProgram(LoadShader(vert), LoadShader(frag), true) }); @@ -159,7 +162,7 @@ namespace K::Resource { } reload_worker = std::thread(HotReload); - fallback_still = LoadStill("mmaker.png"); + fallback_still = Load("mmaker.png"); } void Shutdown() { diff --git a/Keishiki/UI.cpp b/Keishiki/UI.cpp index 00263d7..7377674 100644 --- a/Keishiki/UI.cpp +++ b/Keishiki/UI.cpp @@ -283,6 +283,7 @@ namespace K::UI { ImGui::SetCursorPos(view_pos + n.pos); ImGui::PushID(id); ImGui::PushStyleColor(ImGuiCol_ChildBg, selected ? 0xAA202040 : 0xAA080813); + static ImVec2 old_pos{}; if (ImGui::BeginChild(n.node->name.c_str(), {}, ImGuiChildFlags_AlwaysAutoResize | ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_Border)) { if (ImGui::BeginTable(n.node->name.c_str(), 3)) { ImGui::TableNextRow(ImGuiTableRowFlags_None, row_height); @@ -290,11 +291,11 @@ namespace K::UI { ImGui::TableNextColumn(); ImGui::Button(n.node->name.c_str(), {100.0f , 0.0f}); if (ImGui::IsItemClicked()) { - n.old_pos = n.pos; + old_pos = n.pos; stat = true; } if (ImGui::IsItemActive()) { - n.pos = n.old_pos + ImGui::GetMouseDragDelta(); + n.pos = old_pos + ImGui::GetMouseDragDelta(); } ImGui::TableNextColumn(); @@ -1792,7 +1793,7 @@ namespace K::UI { LogError("Unsupported Renderer"); } - bg.pg = Resource::LoadShaderProgram("Checkerboard")->pg; + bg.pg = Resource::Load("Checkerboard")->pg; bg.AddUniform("f_hw", ShaderGraph::Type::T_XYZ); } diff --git a/Keishiki/include/Common.h b/Keishiki/include/Common.h index 0c6b0b3..a072f09 100644 --- a/Keishiki/include/Common.h +++ b/Keishiki/include/Common.h @@ -25,7 +25,7 @@ namespace K { template using Vector = std::vector; template using Dict = std::unordered_map; - inline void LogBase(const String& s, u8 level) { + inline void LogBase(const std::string_view& s, u8 level) { static const char *levels[] = { "Info", "Warning", @@ -33,10 +33,10 @@ namespace K { }; level > 1 ? std::cerr : std::cout << "[" << levels[level] << "] Keishiki: " << s << std::endl; } - inline void Log(const String& s) { + inline void Log(const std::string_view& s) { LogBase(s, 0); } - inline void LogError(const String& s) { + inline void LogError(const std::string_view& s) { LogBase(s, 2); } diff --git a/Keishiki/include/Resource.h b/Keishiki/include/Resource.h index 5143fcf..ed5a8f9 100644 --- a/Keishiki/include/Resource.h +++ b/Keishiki/include/Resource.h @@ -79,8 +79,9 @@ namespace K::Resource { void Init(); void Shutdown(); - Resource *LoadStill(const std::filesystem::path& file); - Resource *LoadShaderProgram(const String& shader_name); + template + Resource *Load(const std::filesystem::path& p); + bgfx::ProgramHandle FetchUnmanagedShaderProgram(const String& shader_name); } diff --git a/Keishiki/include/ShaderNodes.h b/Keishiki/include/ShaderNodes.h deleted file mode 100644 index 5e9ac04..0000000 --- a/Keishiki/include/ShaderNodes.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "Common.h" -#include "ShaderGraph.h" - -namespace K::Shaders { - ShaderGraph::Node transform = { - .name = "Transform", - .inputs = {}, - .out = {}, - .shader_template = "" - }; -}