texture loading!

This commit is contained in:
Asuro
2025-03-28 04:28:06 +01:00
parent f56ffdaa13
commit 365805537c
26 changed files with 244 additions and 84 deletions

View File

@@ -8,6 +8,7 @@
#include "SDL3/SDL_events.h"
#include "backends/imgui_impl_sdl3.h"
#include "bgfx/c99/bgfx.h"
#include "bgfx/defines.h"
#include "bgfx/platform.h"
#include "bx/bx.h"
@@ -53,6 +54,10 @@ namespace Game
long fileSizeX = appendZero ? fileSize + 1 : fileSize;
void* rawMem = AllocateScratch(fileSizeX);
if (rawMem == nullptr)
{
LOG_ERROR("Failed to load file, exceeded scratch memory! %s", path);
}
const bgfx::Memory* mem = bgfx::makeRef(rawMem, fileSizeX);
fread(mem->data, 1, fileSize, file);
if (appendZero)
@@ -64,6 +69,7 @@ namespace Game
return mem;
}
LOG_WARN("File inaccessible! %s", path);
return nullptr;
}
@@ -126,7 +132,7 @@ namespace Game
bx::Error err;
const bgfx::Memory* data = loadFile(_filePath.getCPtr());
if (data == nullptr)
if (data == nullptr || data->data == nullptr || data->size == 0)
{
LOG_WARN("Failed to find image %s", _filePath.getCPtr());
return handle;
@@ -367,14 +373,11 @@ namespace Game
{
LOG("BGFX setup succeded!");
}
// bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(MainViewID, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x3399FFff, 1.0f, 0);
bgfx::setViewRect(MainViewID, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
DefaultSampler = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
Textures[0].Handle = loadTexture(
bx::FilePath{"models/body.dds"}, BGFX_TEXTURE_NONE | BGFX_SAMPLER_NONE, 0, &Textures[0].Info, nullptr);
Textures[0].SamplerHandle = DefaultSampler;
LoadTextures();
LoadModels(Models, ModelCount);
ReloadShaders();
@@ -586,6 +589,22 @@ namespace Game
ImGui::TextWrapped("%s", GetShared().Dev.ShaderLog);
}
ImGui::End();
if (ImGui::Begin("Textures"))
{
if (ImGui::Button("Reload"))
{
LoadTextures();
}
for (int32_t i = 0; i < MaxTextures; ++i)
{
if (!isValid(Textures[i].RenderHandle)) continue;
ImGui::Text("%i", i);
float width = bx::min<float>(ImGui::GetContentRegionAvail().x, Textures[i].Info.width);
float height = bx::min<float>(ImGui::GetContentRegionAvail().x, Textures[i].Info.height);
ImGui::Image(Textures[i].RenderHandle.idx, {width, height});
}
}
ImGui::End();
if (ImGui::Begin("Puzzles"))
{
if (ImGui::Button("Add"))
@@ -660,6 +679,7 @@ namespace Game
}
Tools::ModelDropdown(card.ModelHandle);
Tools::TextureDropdown(card.BoardTextureHandle);
for (int8_t y = 0; y < Puzzle::Config::CardSize; ++y)
{
ImGui::PushID(y);
@@ -687,12 +707,72 @@ namespace Game
ImGui::End();
}
void GameRendering::LoadTextures()
{
for (int32_t i = 0; i < MaxTextures; ++i)
{
Textures[i] = {};
}
bx::Error err;
bx::DirectoryReader reader{};
if (!reader.open("textures", &err) || !err.isOk())
{
LOG_ERROR("Failed to read textures dir: %s", err.getMessage());
}
bx::FileInfo info;
int32_t textureFilePathCount = 0;
bx::FilePath textureFilePaths[GameRendering::MaxTextures];
while (err.isOk())
{
int32_t res = reader.read(&info, sizeof(info), &err);
if (res == 0) break; // EOF
if (res != sizeof(info))
{
LOG_ERROR("Dir iter error: %s", err.getMessage());
break;
}
const bx::StringView ext = info.filePath.getExt();
bool isDDS = bx::strCmp(ext, ".dds") == 0;
bool isKTX = bx::strCmp(ext, ".ktx") == 0;
if ((isDDS || isKTX) && !ext.isEmpty() && info.type == bx::FileType::File)
{
if (textureFilePathCount >= GameRendering::MaxTextures)
{
LOG_ERROR("Texture limit reached!");
break;
}
textureFilePaths[textureFilePathCount] = info.filePath;
textureFilePathCount++;
}
}
LOG("Found %u textures!", textureFilePathCount);
for (int32_t i = 0; i < textureFilePathCount; ++i)
{
bx::FilePath fullPath{"textures"};
fullPath.join(textureFilePaths[i]);
Textures[i].RenderHandle =
loadTexture(fullPath, BGFX_TEXTURE_NONE | BGFX_SAMPLER_NONE, 0, &Textures[i].Info, nullptr);
Textures[i].SamplerHandle = DefaultSampler;
Textures[i].TexHandle.TextureIdx = i;
Textures[i].TexHandle.Asset.Idx = CrcPath(fullPath.getCPtr());
auto& debug = GetInstance().DebugData;
if (debug.AssetCount < debug.MaxAssets)
{
debug.AssetHandles[debug.AssetCount] = Textures[i].TexHandle.Asset;
bx::strCopy(debug.AssetHandlePaths[debug.AssetCount],
sizeof(debug.AssetHandlePaths[debug.AssetCount]),
fullPath.getCPtr());
++debug.AssetCount;
}
}
}
void GameRendering::ReloadShaders()
{
Materials[(uint16_t)EMaterial::Default] = Material::LoadFromShader(
"dither/vert", "dither/frag", MainViewID, Textures[0].Handle, Textures[0].SamplerHandle);
Materials[(uint16_t)EMaterial::UI] = Material::LoadFromShader(
"normal/vert", "normal/frag", MainViewID, Textures[0].Handle, Textures[0].SamplerHandle);
Materials[(uint16_t)EMaterial::Default] = Material::LoadFromShader("dither/vert", "dither/frag", MainViewID);
Materials[(uint16_t)EMaterial::UI] = Material::LoadFromShader("normal/vert", "normal/frag", MainViewID);
}
void GameRendering::Update()
@@ -733,7 +813,7 @@ namespace Game
ImGui::End();
GetInstance().GameLevel.Update();
GetInstance().GameLevel.Render(MainViewID, Models, Materials);
GetInstance().GameLevel.Render(MainViewID, Models, Materials, Textures);
// Finish Frame
{
@@ -771,8 +851,7 @@ namespace Game
Instance = nullptr;
}
Material Material::LoadFromShader(
const char* vertPath, const char* fragPath, uint16_t view, bgfx::TextureHandle tex, bgfx::UniformHandle sampler)
Material Material::LoadFromShader(const char* vertPath, const char* fragPath, uint16_t view)
{
BX_ASSERT(vertPath != nullptr && fragPath != nullptr, "Invalid shader path!");
bgfx::ShaderHandle vertexShader = loadShader(vertPath);
@@ -786,8 +865,6 @@ namespace Game
mat.Uniforms[Material::UDotColor] = bgfx::createUniform("u_testColor", bgfx::UniformType::Vec4);
mat.Uniforms[Material::UTexInfo] = bgfx::createUniform("u_texInfo", bgfx::UniformType::Vec4);
mat.Uniforms[Material::UBaseColor] = bgfx::createUniform("u_baseColor", bgfx::UniformType::Vec4);
mat.Textures[0].Handle = tex;
mat.Textures[0].SamplerHandle = sampler;
mat.ViewID = view;
return mat;
}