good progress today

This commit is contained in:
Asuro
2025-02-26 01:20:31 +01:00
parent 60f1e72144
commit cde37a2289
10 changed files with 264 additions and 56 deletions

View File

@@ -9,7 +9,9 @@
#include "backends/imgui_impl_sdl3.h"
#include "bgfx/defines.h"
#include "bx/bx.h"
#include "bx/constants.h"
#include "bx/filepath.h"
#include "bx/math.h"
#include "bx/timer.h"
#include <bgfx/bgfx.h>
#include <bimg/bimg.h>
@@ -191,6 +193,109 @@ namespace Game
return handle;
}
void DitherGen(DitherData& data)
{
constexpr int32_t MaxRecursion = 1;
data.Points[0] = {0.0f, 0.0f};
data.Points[1] = {0.5f, 0.5f};
data.Points[2] = {0.5f, 0.0f};
data.Points[3] = {0.0f, 0.5f};
data.PointCount = 4;
for (int32_t recursionLevel = 0; recursionLevel < MaxRecursion; ++recursionLevel)
{
int32_t startCount = data.PointCount;
float offset = bx::pow(0.5f, recursionLevel + 1);
for (int32_t i = 0; i < 4; ++i)
{
for (int32_t j = 0; j < startCount; ++j)
{
data.Points[data.PointCount] = data.Points[j] + data.Points[i] * offset;
data.PointCount++;
}
}
}
uint64_t dotsPerSide = bx::round(bx::pow(2, MaxRecursion));
data.DitherTexDepth = dotsPerSide * dotsPerSide;
data.DitherTexWH = 16 * dotsPerSide;
uint64_t texPixelCount = data.DitherTexWH * data.DitherTexWH * data.DitherTexDepth;
if (BX_COUNTOF(DitherData::DitherTex) < texPixelCount)
{
Log("Too many pixels: %llu", texPixelCount);
return;
}
float invRes = 1.0f / data.DitherTexWH;
for (int32_t z = 0; z < data.DitherTexDepth; ++z)
{
int32_t dotCount = z + 1;
float dotArea = 0.5f / dotCount;
float dotRadius = bx::sqrt(dotArea / bx::kPi);
int zOffset = z * data.DitherTexWH * data.DitherTexWH;
for (int32_t y = 0; y < data.DitherTexWH; ++y)
{
int32_t yOffset = y * data.DitherTexWH;
for (int32_t x = 0; x < data.DitherTexWH; ++x)
{
Vec2 point{(x + 0.5f) * invRes, (y + 0.5f) * invRes};
float dist = bx::kFloatInfinity;
for (int32_t i = 0; i < dotCount; ++i)
{
Vec2 vec = point - data.Points[i];
Vec2 wrappedVec{bx::mod(vec.x + 0.5f, 1.0f) - 0.5f, bx::mod(vec.y + 0.5f, 1.0f) - 0.5f};
float curDist = wrappedVec.Magnitude();
dist = bx::min(dist, curDist);
}
dist = dist / (dotRadius * 2.4f);
float val = bx::clamp(1.0f - dist, 0.0f, 1.0f);
data.DitherTex[x + yOffset + zOffset] = Vec4{val, val, val, 1.0f};
// data.DitherTex[x + yOffset + zOffset] = Vec4{1.0, 0.0f, 0.0f, 1.0f};
int32_t bucket = bx::clamp(uint32_t(val * DitherData::BrightnessBucketCount),
0,
DitherData::BrightnessBucketCount - 1);
}
}
}
// TODO: brightness ramp
if (isValid(data.PreviewTex))
{
bgfx::destroy(data.PreviewTex);
data.PreviewTex = BGFX_INVALID_HANDLE;
}
if (isValid(data.FinalTex))
{
bgfx::destroy(data.FinalTex);
data.FinalTex = BGFX_INVALID_HANDLE;
}
const bgfx::Memory* memPreview = bgfx::makeRef(data.DitherTex, texPixelCount * sizeof(Vec4));
const bgfx::Memory* memFinal = bgfx::makeRef(data.DitherTex, texPixelCount * sizeof(Vec4));
data.PreviewTex = bgfx::createTexture2D(data.DitherTexWH,
data.DitherTexWH * data.DitherTexDepth,
false,
false,
bgfx::TextureFormat::RGBA32F,
0,
memPreview);
data.FinalTex = bgfx::createTexture3D(data.DitherTexWH,
data.DitherTexWH,
data.DitherTexDepth,
false,
bgfx::TextureFormat::RGBA32F,
0,
memFinal);
if (!isValid(data.Sampler))
{
data.Sampler = bgfx::createUniform("s_ditherSampler", bgfx::UniformType::Sampler);
}
}
GameRendering* Instance = nullptr;
} // namespace
@@ -269,6 +374,11 @@ namespace Game
{
ImGui::LoadIniSettingsFromMemory(inst.ImguiIni, inst.ImguiIniSize);
}
else
{
ImGui::LoadIniSettingsFromDisk("imgui.ini");
}
DitherGen(DitherTextures);
}
void GameRendering::Update()
@@ -308,10 +418,12 @@ namespace Game
if (isValid(newProgram))
{
Materials[0].Shader = newProgram;
LastShaderLoadTime = GetInstance().Time.Now;
}
else
{
Log("Failed to load shader!");
LastShaderLoadTime = -1.0f;
}
}
}
@@ -325,16 +437,48 @@ namespace Game
{
if (ImGui::Begin("Rendering"))
{
if (LastShaderLoadTime >= 0.0f)
{
ImGui::TextColored({0.2f, 0.9f, 0.2f, 1.0f},
"Shader loaded %.0f seconds ago",
GetInstance().Time.Now - LastShaderLoadTime);
}
else
{
ImGui::TextColored({0.9f, 0.2f, 0.2f, 1.0f}, "Shader load Failiure!");
}
if (ImGui::Button("Reload Meshes"))
{
LoadModels(Models, ModelCount);
}
ImGui::SameLine();
if (ImGui::Button("Reload Level"))
{
auto& lvl = GetInstance().GameLevel;
lvl = {};
lvl.Setup(shared.Game);
}
if (ImGui::Button("Dithergen"))
{
DitherGen(DitherTextures);
}
ImGui::Text(
"%ux%ux%u", DitherTextures.DitherTexWH, DitherTextures.DitherTexWH, DitherTextures.DitherTexDepth);
if (!isValid(DitherTextures.PreviewTex))
{
ImGui::Text("Invalid Texture");
}
else
{
ImGui::Image(DitherTextures.PreviewTex.idx,
{(float)DitherTextures.DitherTexWH,
(float)DitherTextures.DitherTexWH * DitherTextures.DitherTexDepth});
}
ImGui::Text("Brightness Buckets (TODO)");
for (int32_t i = 0; i < DitherTextures.BrightnessBucketCount; ++i)
{
ImGui::Text("%u", DitherTextures.BrightnessBuckets[i]);
}
}
ImGui::End();
}
@@ -366,6 +510,7 @@ namespace Game
void GameRendering::Shutdown()
{
Log("--- RENDERING_SHUTDOWN ---");
ImGui::SaveIniSettingsToDisk("imgui.ini");
const char* iniData = ImGui::SaveIniSettingsToMemory(reinterpret_cast<uint64_t*>(&GetInstance().ImguiIniSize));
assert(GetInstance().ImguiIniSize <= BX_COUNTOF(GameInstance::ImguiIni));
bx::memCopy(

View File

@@ -4,6 +4,9 @@
#include <bx/string.h>
#include <cstdint>
#include "../Global.h"
#include "imgui.h"
union SDL_Event;
namespace Game
@@ -62,6 +65,21 @@ namespace Game
Debug,
};
struct DitherData
{
static constexpr uint32_t BrightnessBucketCount = 256;
Vec2 Points[1024];
uint32_t PointCount = 0;
Vec4 DitherTex[64 * 64 * 16];
uint32_t DitherTexWH = 0;
uint32_t DitherTexDepth = 0;
int32_t BrightnessBuckets[BrightnessBucketCount];
bgfx::TextureHandle PreviewTex = BGFX_INVALID_HANDLE;
bgfx::TextureHandle FinalTex = BGFX_INVALID_HANDLE;
bgfx::UniformHandle Sampler = BGFX_INVALID_HANDLE;
ImTextureID PreviewID = 0;
};
class GameRendering
{
public:
@@ -70,6 +88,7 @@ namespace Game
public:
UIVisibilityState UIVisible = UIVisibilityState::Game;
DitherData DitherTextures;
private:
bgfx::UniformHandle DefaultSampler;
@@ -81,6 +100,7 @@ namespace Game
int32_t LastHeight = 0;
uint32_t ResetFlags = BGFX_RESET_VSYNC;
uint16_t MainViewID = 10;
float LastShaderLoadTime = 0.0f;
public:
void Setup();