refactour

This commit is contained in:
lachrymaLF 2024-06-21 22:06:55 -04:00
parent b80a0db027
commit faaa0f8e6f
6 changed files with 49 additions and 56 deletions

View file

@ -94,7 +94,7 @@ namespace K {
return false; return false;
} }
logo = K::Resource::LoadStill("Keishiki.png"); logo = Resource::Load<Resource::K_R_Still>("Keishiki.png");
state.width = 1280; state.width = 1280;
state.height = 550; state.height = 550;

View file

@ -1,12 +1,27 @@
#include <Resource.h> #include <Resource.h>
#include <stb_image.h>
#include <thread>
#include <Keishiki.h> #include <Keishiki.h>
#include <chrono> #include <chrono>
#include <mutex> #include <mutex>
#include <thread>
#include <stb_image.h>
namespace { 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::filesystem::path shader_path{};
std::thread reload_worker; std::thread reload_worker;
} }
@ -25,12 +40,13 @@ namespace K::Resource {
Resource<Type::K_R_Still> *fallback_still; Resource<Type::K_R_Still> *fallback_still;
Resource<K_R_Still> *LoadStill(const std::filesystem::path& file) { template <>
Resource<K_R_Still> *Load(const std::filesystem::path& p) {
std::scoped_lock lk{resource_lock}; std::scoped_lock lk{resource_lock};
Resource<K_R_Still> res{.last_updated = std::filesystem::last_write_time(file)}; Resource<K_R_Still> res{.last_updated = std::filesystem::last_write_time(p)};
res.buf = stbi_load(file.c_str(), &res.w, &res.h, &res.channels, 0); res.buf = stbi_load(p.c_str(), &res.w, &res.h, &res.channels, 0);
if (res.buf == nullptr) { if (res.buf == nullptr) {
K::LogError("Image loading failed for " + file.string()); K::LogError("Image loading failed for " + p.string());
return nullptr; return nullptr;
} }
const bgfx::Memory *ref = bgfx::makeRef(res.buf, res.w * res.h * res.channels * sizeof(u8)); const bgfx::Memory *ref = bgfx::makeRef(res.buf, res.w * res.h * res.channels * sizeof(u8));
@ -43,31 +59,18 @@ namespace K::Resource {
res.format, res.format,
BGFX_TEXTURE_NONE | BGFX_SAMPLER_UVW_CLAMP, BGFX_TEXTURE_NONE | BGFX_SAMPLER_UVW_CLAMP,
ref); ref);
res.filename = file.filename(); res.filename = p.filename();
auto [it, check] = resources.emplace(file, res); auto [it, check] = resources.emplace(p, res);
return &std::get<Resource<K_R_Still>>(it->second); return &std::get<Resource<K_R_Still>>(it->second);
} }
bgfx::ShaderHandle LoadShader(const std::filesystem::path& file_path) { template <>
FILE *file = fopen(file_path.c_str(), "rb"); Resource<K_R_ShaderProgram> *Load(const std::filesystem::path& p) {
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<K_R_ShaderProgram> *LoadShaderProgram(const String& shader_name) {
std::scoped_lock lk{resource_lock}; std::scoped_lock lk{resource_lock};
const auto frag = shader_path / (shader_name + ".frag.bin"), const auto frag = shader_path / (p.string() + ".frag.bin"),
vert = shader_path / (shader_name + ".vert.bin"); vert = shader_path / (p.string() + ".vert.bin");
auto [iter, check] = resources.emplace(shader_name, Resource<K_R_ShaderProgram>{ auto [iter, check] = resources.emplace(p, Resource<K_R_ShaderProgram>{
.name = shader_name, .name = p,
.frag_last_updated = std::filesystem::last_write_time(frag), .frag_last_updated = std::filesystem::last_write_time(frag),
.pg = bgfx::createProgram(LoadShader(vert), LoadShader(frag), true) .pg = bgfx::createProgram(LoadShader(vert), LoadShader(frag), true)
}); });
@ -159,7 +162,7 @@ namespace K::Resource {
} }
reload_worker = std::thread(HotReload); reload_worker = std::thread(HotReload);
fallback_still = LoadStill("mmaker.png"); fallback_still = Load<K_R_Still>("mmaker.png");
} }
void Shutdown() { void Shutdown() {

View file

@ -283,6 +283,7 @@ namespace K::UI {
ImGui::SetCursorPos(view_pos + n.pos); ImGui::SetCursorPos(view_pos + n.pos);
ImGui::PushID(id); ImGui::PushID(id);
ImGui::PushStyleColor(ImGuiCol_ChildBg, selected ? 0xAA202040 : 0xAA080813); 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::BeginChild(n.node->name.c_str(), {}, ImGuiChildFlags_AlwaysAutoResize | ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_Border)) {
if (ImGui::BeginTable(n.node->name.c_str(), 3)) { if (ImGui::BeginTable(n.node->name.c_str(), 3)) {
ImGui::TableNextRow(ImGuiTableRowFlags_None, row_height); ImGui::TableNextRow(ImGuiTableRowFlags_None, row_height);
@ -290,11 +291,11 @@ namespace K::UI {
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Button(n.node->name.c_str(), {100.0f , 0.0f}); ImGui::Button(n.node->name.c_str(), {100.0f , 0.0f});
if (ImGui::IsItemClicked()) { if (ImGui::IsItemClicked()) {
n.old_pos = n.pos; old_pos = n.pos;
stat = true; stat = true;
} }
if (ImGui::IsItemActive()) { if (ImGui::IsItemActive()) {
n.pos = n.old_pos + ImGui::GetMouseDragDelta(); n.pos = old_pos + ImGui::GetMouseDragDelta();
} }
ImGui::TableNextColumn(); ImGui::TableNextColumn();
@ -1792,7 +1793,7 @@ namespace K::UI {
LogError("Unsupported Renderer"); LogError("Unsupported Renderer");
} }
bg.pg = Resource::LoadShaderProgram("Checkerboard")->pg; bg.pg = Resource::Load<Resource::K_R_ShaderProgram>("Checkerboard")->pg;
bg.AddUniform("f_hw", ShaderGraph::Type::T_XYZ); bg.AddUniform("f_hw", ShaderGraph::Type::T_XYZ);
} }

View file

@ -25,7 +25,7 @@ namespace K {
template <typename T> using Vector = std::vector<T>; template <typename T> using Vector = std::vector<T>;
template <typename T, typename U> using Dict = std::unordered_map<T, U>; template <typename T, typename U> using Dict = std::unordered_map<T, U>;
inline void LogBase(const String& s, u8 level) { inline void LogBase(const std::string_view& s, u8 level) {
static const char *levels[] = { static const char *levels[] = {
"Info", "Info",
"Warning", "Warning",
@ -33,10 +33,10 @@ namespace K {
}; };
level > 1 ? std::cerr : std::cout << "[" << levels[level] << "] Keishiki: " << s << std::endl; 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); LogBase(s, 0);
} }
inline void LogError(const String& s) { inline void LogError(const std::string_view& s) {
LogBase(s, 2); LogBase(s, 2);
} }

View file

@ -79,8 +79,9 @@ namespace K::Resource {
void Init(); void Init();
void Shutdown(); void Shutdown();
Resource<K_R_Still> *LoadStill(const std::filesystem::path& file); template <Type T>
Resource<K_R_ShaderProgram> *LoadShaderProgram(const String& shader_name); Resource<T> *Load(const std::filesystem::path& p);
bgfx::ProgramHandle FetchUnmanagedShaderProgram(const String& shader_name); bgfx::ProgramHandle FetchUnmanagedShaderProgram(const String& shader_name);
} }

View file

@ -1,12 +0,0 @@
#pragma once
#include "Common.h"
#include "ShaderGraph.h"
namespace K::Shaders {
ShaderGraph::Node transform = {
.name = "Transform",
.inputs = {},
.out = {},
.shader_template = ""
};
}