diff --git a/demo/tz/tz_triangle_demo.cpp b/demo/tz/tz_triangle_demo.cpp index 2f1adb301e..5c1ff37889 100644 --- a/demo/tz/tz_triangle_demo.cpp +++ b/demo/tz/tz_triangle_demo.cpp @@ -41,19 +41,12 @@ int tz_main() tz::gpu::graph_handle graph = tz::gpu::create_graph("Main Graph"); tz::gpu::graph_add_pass(graph, pass); - tz::gpu::graph_add_subgraph(graph, tz::imgui_render_graph()); + //tz::gpu::graph_add_subgraph(graph, tz::imgui_render_graph()); tz::gpu::graph_add_pass(graph, tz::gpu::present_pass); while(tz::os::window_is_open()) { tz::os::window_update(); - auto windims = static_cast(tz::v2u{tz::os::window_get_width(), tz::os::window_get_height()}); - ImGui::GetIO().DisplaySize = {windims[0], windims[1]}; - ImGui::NewFrame(); - ImGui::Begin("123"); - ImGui::Text("hello world"); - ImGui::End(); - ImGui::EndFrame(); tz::gpu::execute(graph); } diff --git a/src/tz/gpu/rhi_vulkan.cpp b/src/tz/gpu/rhi_vulkan.cpp index 60046063c8..1d471c59d6 100644 --- a/src/tz/gpu/rhi_vulkan.cpp +++ b/src/tz/gpu/rhi_vulkan.cpp @@ -2708,7 +2708,7 @@ namespace tz::gpu #if TOPAZ_DEBUG vkCmdEndDebugUtilsLabelEXT(frame.cmds); #endif - vkCmdSetEvent(frame.cmds, pass.on_finish, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); + vkCmdSetEvent(frame.cmds, pass.on_finish, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT); return ret; } diff --git a/src/tz/imgui.cpp b/src/tz/imgui.cpp index 24ae97c03c..9c1e9477e8 100644 --- a/src/tz/imgui.cpp +++ b/src/tz/imgui.cpp @@ -1,6 +1,8 @@ #include "tz/imgui.hpp" +#include "tz/core/trs.hpp" #include "tz/topaz.hpp" #include "tz/core/memory.hpp" +#include "tz/core/matrix.hpp" #include "tz/gpu/hardware.hpp" #include "tz/gpu/resource.hpp" #include "tz/gpu/shader.hpp" @@ -16,6 +18,13 @@ namespace tz { namespace detail { + struct shader_data + { + tz::m4f vp = tz::m4f::iden(); + std::uint32_t index_offset = 0; + std::uint32_t vertex_offset = 0; + std::uint32_t texture_id = 0; + }; // imgui backend initialise. struct render_data { @@ -25,6 +34,8 @@ namespace tz tz::gpu::resource_handle font_image = tz::nullhand; tz::gpu::shader_handle shader = tz::nullhand; + std::vector data_buffers = {}; + std::vector passes = {}; tz::gpu::graph_handle graph = tz::nullhand; } render; @@ -112,7 +123,6 @@ namespace tz return; } - impl_write_vertices_and_indices(draws); while(std::cmp_greater(draws->CmdListsCount, render.passes.size())) { impl_push_pass(); @@ -121,10 +131,13 @@ namespace tz { impl_pop_pass(); } + impl_write_vertices_and_indices(draws); } void impl_write_vertices_and_indices(ImDrawData* draw) { + ImGuiIO io = ImGui::GetIO(); + const auto req_idx_size = static_cast(draw->TotalIdxCount) * sizeof(ImDrawIdx); const auto req_vtx_size = static_cast(draw->TotalVtxCount) * sizeof(ImDrawVert); @@ -144,11 +157,27 @@ namespace tz for(std::size_t i = 0; std::cmp_less(i, draw->CmdListsCount); i++) { const ImDrawList* cmd = draw->CmdLists[i]; + + auto data = *reinterpret_cast(tz::gpu::resource_read(render.data_buffers[i]).data()); + data.vertex_offset = vtx_cursor; + data.index_offset = idx_cursor; + data.vp = tz::matrix_ortho( + -io.DisplaySize.x * 0.5f, + io.DisplaySize.x * 0.5f, + -io.DisplaySize.y * 0.5f, + io.DisplaySize.y * 0.5f, + -0.1f, + 0.1f + ); + tz::gpu::resource_write(render.data_buffers[i], std::as_bytes(std::span(&data, 1))); + std::copy(cmd->VtxBuffer.Data, cmd->VtxBuffer.Data + cmd->VtxBuffer.Size, new_vertices.data() + vtx_cursor); vtx_cursor += cmd->VtxBuffer.Size; std::copy(cmd->IdxBuffer.Data, cmd->IdxBuffer.Data + cmd->IdxBuffer.Size, new_indices.data() + idx_cursor); idx_cursor += cmd->IdxBuffer.Size; + + tz::gpu::pass_set_triangle_count(render.passes[i], cmd->IdxBuffer.Size / 3); } tz::gpu::resource_write(render.vertex_buffer, tz::view_bytes(new_vertices)); @@ -157,10 +186,20 @@ namespace tz void impl_push_pass() { + shader_data data; + std::span span(&data, 1); + std::string data_buf_name = std::format("ImGui Data Buffer {}", render.data_buffers.size()); + render.data_buffers.push_back(tz_must(tz::gpu::create_buffer + ({ + .data = std::as_bytes(span), + .name = data_buf_name.c_str(), + .flags = tz::gpu::buffer_flag::dynamic_access, + }))); tz::gpu::resource_handle resources[] = { render.vertex_buffer, render.index_buffer, + render.data_buffers[render.passes.size()], render.font_image }; @@ -175,6 +214,7 @@ namespace tz .graphics = { .colour_targets = colour_targets, + .culling = tz::gpu::cull::none, .flags = tz::gpu::graphics_flag::dont_clear | tz::gpu::graphics_flag::no_depth_test, }, .shader = render.shader, @@ -194,6 +234,8 @@ namespace tz void impl_pop_pass() { + tz::gpu::destroy_resource(render.data_buffers.back()); + render.data_buffers.pop_back(); tz::gpu::destroy_pass(render.passes.back()); render.passes.pop_back(); tz_error("todo: implement graph remove pass"); diff --git a/src/tz/imgui.fragment.tzsl b/src/tz/imgui.fragment.tzsl index ecc17e8f49..b0792940b2 100644 --- a/src/tz/imgui.fragment.tzsl +++ b/src/tz/imgui.fragment.tzsl @@ -1,6 +1,17 @@ shader(type = fragment); +input(id = 0) vec2 uv; +input(id = 1) vec4 col; +input(id = 2, flat) uint texid; + +output(id = 0) vec4 out::colour; + void main() { - + float texa = sample(texid, uv).a; + out::colour = col.rgba * texa; + if(texa < 0.1f) + { + discard; + } } \ No newline at end of file diff --git a/src/tz/imgui.vertex.tzsl b/src/tz/imgui.vertex.tzsl index 8d168dba3b..3454d3f0b7 100644 --- a/src/tz/imgui.vertex.tzsl +++ b/src/tz/imgui.vertex.tzsl @@ -1,6 +1,47 @@ shader(type = vertex); +struct ImGuiVertex +{ + float px, py; + float uvx, uvy; + uint col; +}; + +resource(id = 0) const buffer vertex +{ + ImGuiVertex data[]; +}; + +resource(id = 1) const buffer index +{ + uint data[]; +}; + +resource(id = 2) const buffer render +{ + mat4 vp; + uint index_offset; + uint vertex_offset; + uint texture_id; +}; + +output(id = 0) vec2 vtx_uv; +output(id = 1) vec4 vtx_col; +output(id = 2) uint vtx_texid; + void main() { - + uint index = index.data[in::vertex_id + render.index_offset]; + ImGuiVertex vtx = vertex.data[index + render.vertex_offset]; + out::position = render.vp * vec4(vtx.px, vtx.py, 0.0f, 1.0f); + + vtx_uv = vec2(vtx.uvx, vtx.uvy); + + vtx_col = vec4(0.0); + vtx_col.a = ((vtx.col >> 24) & 0xFF) / 255.0f; + vtx_col.b = ((vtx.col >> 16) & 0xFF) / 255.0f; + vtx_col.g = ((vtx.col >> 8) & 0xFF) / 255.0f; + vtx_col.r = ((vtx.col) & 0xFF) / 255.0f; + + vtx_texid = render.texture_id; } \ No newline at end of file