working composite

This commit is contained in:
lachrymaLF 2024-05-23 14:16:34 -04:00
parent 2de32140be
commit 87a686d090
6 changed files with 83 additions and 56 deletions

View file

@ -436,9 +436,9 @@ namespace K::Graphics {
static f32 pack[4]{};
pack[0] = static_cast<f32>(mode);
bgfx::touch(K::Graphics::K_VIEW_DRAW);
bgfx::setViewMode(K::Graphics::K_VIEW_DRAW, bgfx::ViewMode::Sequential);
bgfx::setViewClear(K::Graphics::K_VIEW_DRAW, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x00000000, 1.0f, 0);
bgfx::touch(K::Graphics::K_VIEW_COMP_COMPOSITE);
bgfx::setViewMode(K::Graphics::K_VIEW_COMP_COMPOSITE, bgfx::ViewMode::Sequential);
bgfx::setViewClear(K::Graphics::K_VIEW_COMP_COMPOSITE, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x00000000, 1.0f, 0);
bgfx::setViewFrameBuffer(K::Graphics::K_VIEW_COMP_COMPOSITE, fb);
bgfx::setViewTransform(K::Graphics::K_VIEW_COMP_COMPOSITE, composite_view, proj);
bgfx::setViewRect(K::Graphics::K_VIEW_COMP_COMPOSITE, 0, 0, w, h);
@ -447,7 +447,7 @@ namespace K::Graphics {
bgfx::setVertexBuffer(0, vbh);
bgfx::setIndexBuffer(ibh);
bgfx::setTexture(0, composite_A, composite);
bgfx::setTexture(0, composite_B, from);
bgfx::setTexture(1, composite_B, from);
bgfx::setUniform(composite_mode, pack);
bgfx::setState((BGFX_STATE_DEFAULT | BGFX_STATE_BLEND_ALPHA) & ~(BGFX_STATE_WRITE_Z | BGFX_STATE_DEPTH_TEST_LESS));
bgfx::submit(K::Graphics::K_VIEW_COMP_COMPOSITE, composite_pg);

View file

@ -95,8 +95,10 @@ namespace K {
UI::Draw(state);
const bgfx::Stats* stat = bgfx::getStats();
const bgfx::Caps* caps = bgfx::getCaps();
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, window_height/16 - 3, 0xf8, " %u FPS", stat->cpuTimerFreq / stat->cpuTimeFrame);
bgfx::dbgTextPrintf(0, window_height/16 - 4, 0xf8, " %s %u FPS", caps->supported & BGFX_CAPS_RENDERER_MULTITHREADED ? "Multithreaded" : "Singlethreaded", stat->cpuTimerFreq / stat->cpuTimeFrame);
bgfx::dbgTextPrintf(0, window_height/16 - 3, 0xf8, " Max Views: %u :: Max FBs %u :: Max Texture Samplers %u", caps->limits.maxViews, caps->limits.maxFrameBuffers, caps->limits.maxTextureSamplers);
bgfx::dbgTextPrintf(0, window_height/16 - 2, 0xf8, " Project Keishiki :: %s :: Build %s %s", BX_COMPILER_NAME, __DATE__, __TIME__);
bgfx::dbgTextPrintf(0, window_height/16 - 1, 0xf8, " SDL %i.%i.%i :: bgfx 1.%i :: Dear ImGui %s :: %s", SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL, BGFX_API_VERSION, ImGui::GetVersion(), bgfx::getRendererName(bgfx::getRendererType()));

View file

@ -12,7 +12,11 @@ namespace {
bool draw_nodes = true;
// Viewport
bgfx::FrameBufferHandle composite_fb = BGFX_INVALID_HANDLE, render_fb = BGFX_INVALID_HANDLE;
bgfx::TextureHandle composite = BGFX_INVALID_HANDLE,
composite_blit = BGFX_INVALID_HANDLE,
render = BGFX_INVALID_HANDLE;
bgfx::FrameBufferHandle composite_fb = BGFX_INVALID_HANDLE,
render_fb = BGFX_INVALID_HANDLE;
K::VisualTrack bg{};
f32 proj[16], transform[16];
}
@ -53,8 +57,14 @@ namespace K::UI {
}
void SetupViewport(CompState& s) {
composite_fb = bgfx::createFrameBuffer(s.width, s.height, bgfx::TextureFormat::RGBA8);
render_fb = bgfx::createFrameBuffer(s.width, s.height, bgfx::TextureFormat::RGBA8);
composite = bgfx::createTexture2D(s.width, s.height, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT);
composite_blit = bgfx::createTexture2D(s.width, s.height, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_BLIT_DST);
render = bgfx::createTexture2D(s.width, s.height, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT);
bgfx::Attachment comp{}, ren{};
comp.init(composite, bgfx::Access::ReadWrite);
ren.init(render, bgfx::Access::ReadWrite);
composite_fb = bgfx::createFrameBuffer(1, &comp);
render_fb = bgfx::createFrameBuffer(1, &ren);
bg.pg = K::Graphics::load_shader_program("Checkerboard");
bg.add_uniform("f_hw", ShaderGraph::Type::T_XYZ);
@ -69,7 +79,10 @@ namespace K::UI {
void DestroyViewport(CompState& s) {
bgfx::destroy(composite_fb);
bgfx::destroy(composite);
bgfx::destroy(composite_blit);
bgfx::destroy(render_fb);
bgfx::destroy(render);
bgfx::destroy(bg.pg);
bgfx::destroy(bg.uniforms.begin()->second.first);
@ -88,11 +101,13 @@ namespace K::UI {
if (ImGui::Begin("Viewport", &draw_viewport)) {
ImTextureID idx = nullptr;
bgfx::TextureHandle composite = bg.get_frame(composite_fb, s.width, s.height);
for (auto& layer : s.layers) {
if (!layer.enabled) continue;
Graphics::Composite(composite_fb, composite, layer.track.get_frame(render_fb, s.width, s.height),
&layer == &s.layers.front() ? Graphics::K_V_AlphaOver : layer.mode, s.width, s.height, proj, transform);
bg.get_frame(composite_fb, s.width, s.height);
bgfx::blit(Graphics::K_VIEW_COMP_COMPOSITE, composite_blit, 0, 0, composite);
for (auto it = s.layers.rbegin(); it != s.layers.rend(); it++) {
if (!it->enabled) continue;
Graphics::Composite(composite_fb, composite_blit, it->track.get_frame(render_fb, s.width, s.height),
it == s.layers.rbegin() ? Graphics::K_V_AlphaOver : it->mode, s.width, s.height, proj, transform);
bgfx::blit(Graphics::K_VIEW_COMP_COMPOSITE, composite_blit, 0, 0, composite);
}
idx = ImGui::toId(composite, 0, 0);
if (idx != nullptr)
@ -347,7 +362,7 @@ namespace K::UI {
ImGui::InputFloat3(("##" + it->first).c_str(), &arg.x);
}, it->second.second);
ImGui::TableNextColumn();
if (ImGui::Button("Delete Uniform")) {
if (ImGui::Button(("Delete Uniform##" + it->first).c_str())) {
bgfx::destroy(it->second.first);
it = s.layers[s.active].track.uniforms.erase(it);
}

View file

@ -7,9 +7,9 @@
namespace K::Graphics {
enum VIEW_ID {
K_VIEW_TOP = 0,
K_VIEW_COMP_COMPOSITE,
K_VIEW_DRAW
K_VIEW_DRAW,
K_VIEW_TOP,
};
bool Init(u16 width, u16 height);

View file

@ -24,17 +24,17 @@ namespace K {
Dict<String, std::pair<bgfx::UniformHandle, ShaderGraph::T_Map<ShaderGraph::T_Count>::type>> uniforms;
// Vector<String> samplers;
bgfx::TextureHandle get_frame(bgfx::FrameBufferHandle fb, u32 w, u32 h) const {
bgfx::touch(K::Graphics::K_VIEW_DRAW);
bgfx::setViewMode(K::Graphics::K_VIEW_DRAW, bgfx::ViewMode::Sequential);
bgfx::setViewClear(K::Graphics::K_VIEW_DRAW, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x00000000, 1.0f, 0);
bgfx::setViewFrameBuffer(K::Graphics::K_VIEW_DRAW, fb);
bgfx::touch(K::Graphics::K_VIEW_COMP_COMPOSITE);
bgfx::setViewMode(K::Graphics::K_VIEW_COMP_COMPOSITE, bgfx::ViewMode::Sequential);
bgfx::setViewClear(K::Graphics::K_VIEW_COMP_COMPOSITE, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x00000000, 1.0f, 0);
bgfx::setViewFrameBuffer(K::Graphics::K_VIEW_COMP_COMPOSITE, fb);
float view[16];
bx::mtxTranslate(view, 0.f, 0.f, 1.0f);
float proj[16];
bx::mtxOrtho(proj, 0.0f, static_cast<f32>(w), 0.0f, static_cast<f32>(h),
0.1f, 100.0f, 0.f, bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(K::Graphics::K_VIEW_DRAW, view, proj);
bgfx::setViewRect(K::Graphics::K_VIEW_DRAW, 0, 0, w, h);
bgfx::setViewTransform(K::Graphics::K_VIEW_COMP_COMPOSITE, view, proj);
bgfx::setViewRect(K::Graphics::K_VIEW_COMP_COMPOSITE, 0, 0, w, h);
// TODO Find a better way to pack...
for (auto& [_name, u] : uniforms) {
f32 pack[4]{};
@ -59,7 +59,7 @@ namespace K {
bgfx::setUniform(u.first, pack);
}
Graphics::DrawTexture(K::Graphics::K_VIEW_DRAW, Graphics::mmaker->tx, 0, 0, w, h,
Graphics::DrawTexture(K::Graphics::K_VIEW_COMP_COMPOSITE, Graphics::mmaker->tx, 0, 0, w, h,
(BGFX_STATE_DEFAULT | BGFX_STATE_BLEND_ALPHA) &
~(BGFX_STATE_WRITE_Z | BGFX_STATE_DEPTH_TEST_LESS), pg);
return bgfx::getTexture(fb);
@ -72,36 +72,46 @@ namespace K {
std::ofstream f("temp.frag");
f << shader;
f.close();
if (std::system(
#if BX_PLATFORM_WINDOWS
"./ext/bgfx/cmake/bgfx/shaderc "
String s{};
switch (bgfx::getRendererType()) {
case bgfx::RendererType::Noop:
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12: s = "./ext/bgfx/cmake/bgfx/shaderc "
"-f temp.frag "
"--type fragment "
"--platform windows "
"--profile " "ps_5_0" " "
"--varyingdef temp.varying.def.sc "
"-i ./ "
"-o shaders/" "dx11" "/temp.frag.bin"
#elif BX_PLATFORM_LINUX
"./ext/bgfx/cmake/bgfx/shaderc "
"-f temp.frag "
"--type fragment "
"--platform windows "
"--profile " "spirv" " "
"--varyingdef temp.varying.def.sc "
"-i ./ "
"-o shaders/" "spirv" "/temp.frag.bin"
#elif BX_PLATFORM_OSX
"./ext/bgfx/cmake/bgfx/shaderc "
"-o shaders/" "dx11" "/temp.frag.bin"; break;
case bgfx::RendererType::Metal: s = "./ext/bgfx/cmake/bgfx/shaderc "
"-f temp.frag "
"--type fragment "
"--platform osx "
"--profile " "metal" " "
"--varyingdef temp.varying.def.sc "
"-i ./ "
"-o shaders/" "metal" "/temp.frag.bin"
#endif
) == 0) {
"-o shaders/" "metal" "/temp.frag.bin"; break;
case bgfx::RendererType::OpenGL: s = "./ext/bgfx/cmake/bgfx/shaderc "
"-f temp.frag "
"--type fragment "
"--platform windows "
"--profile " "140" " "
"--varyingdef temp.varying.def.sc "
"-i ./ "
"-o shaders/" "glsl" "/temp.frag.bin"; break;
case bgfx::RendererType::Vulkan: s = "./ext/bgfx/cmake/bgfx/shaderc "
"-f temp.frag "
"--type fragment "
"--platform windows "
"--profile " "spirv" " ""--varyingdef temp.varying.def.sc "
"-i ./ "
"-o shaders/" "spirv" "/temp.frag.bin"; break;
default: LogError("Unsupported renderer"); break;
}
if (std::system(s.c_str()) == 0) {
if (isValid(pg)) bgfx::destroy(pg);
pg = Graphics::load_shader_program("temp");
}

View file

@ -5,7 +5,7 @@ $input v_texcoord0
uniform vec4 f_mode;
SAMPLER2D(v_A, 0);
SAMPLER2D(v_B, 0);
SAMPLER2D(v_B, 1);
void main()
{