From 87a686d0905e06fa2f803e2b773f7f4bd6dc5b94 Mon Sep 17 00:00:00 2001 From: lachrymaLF Date: Thu, 23 May 2024 14:16:34 -0400 Subject: [PATCH] working composite --- Keishiki/Graphics.cpp | 8 +- Keishiki/Keishiki.cpp | 8 +- Keishiki/UI.cpp | 33 ++++++-- Keishiki/include/Graphics.h | 4 +- Keishiki/include/VisualTrack.h | 84 +++++++++++-------- .../BinaryComposite/BinaryComposite.frag | 2 +- 6 files changed, 83 insertions(+), 56 deletions(-) diff --git a/Keishiki/Graphics.cpp b/Keishiki/Graphics.cpp index c23be9e..af18012 100644 --- a/Keishiki/Graphics.cpp +++ b/Keishiki/Graphics.cpp @@ -436,9 +436,9 @@ namespace K::Graphics { static f32 pack[4]{}; pack[0] = static_cast(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); diff --git a/Keishiki/Keishiki.cpp b/Keishiki/Keishiki.cpp index 5ee6c3e..9891217 100644 --- a/Keishiki/Keishiki.cpp +++ b/Keishiki/Keishiki.cpp @@ -95,10 +95,12 @@ 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 - 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())); + 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())); bgfx::frame(); } diff --git a/Keishiki/UI.cpp b/Keishiki/UI.cpp index 31dc82c..0e61a7a 100644 --- a/Keishiki/UI.cpp +++ b/Keishiki/UI.cpp @@ -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); } diff --git a/Keishiki/include/Graphics.h b/Keishiki/include/Graphics.h index 3c2fee8..fbff7b2 100644 --- a/Keishiki/include/Graphics.h +++ b/Keishiki/include/Graphics.h @@ -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); diff --git a/Keishiki/include/VisualTrack.h b/Keishiki/include/VisualTrack.h index d954947..2d2435b 100644 --- a/Keishiki/include/VisualTrack.h +++ b/Keishiki/include/VisualTrack.h @@ -24,17 +24,17 @@ namespace K { Dict::type>> uniforms; // Vector 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(w), 0.0f, static_cast(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 " - "-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 " - "-f temp.frag " - "--type fragment " - "--platform osx " - "--profile " "metal" " " - "--varyingdef temp.varying.def.sc " - "-i ./ " - "-o shaders/" "metal" "/temp.frag.bin" -#endif - ) == 0) { + + 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"; 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"; 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"); } diff --git a/Keishiki/shaders/BinaryComposite/BinaryComposite.frag b/Keishiki/shaders/BinaryComposite/BinaryComposite.frag index c11ec15..5b4ed26 100644 --- a/Keishiki/shaders/BinaryComposite/BinaryComposite.frag +++ b/Keishiki/shaders/BinaryComposite/BinaryComposite.frag @@ -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() {