Skip to content

Commit

Permalink
[gpu.vulkan] fixed a bug where set/wait events would not match their …
Browse files Browse the repository at this point in the history
…srcStages, causing a validation error if you ever had dependencies. the imgui renderer is very broken and will almost certainly require multi draw via draw indirect buffer
  • Loading branch information
harrand committed Nov 16, 2024
1 parent dd1d3bb commit 7f523bf
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
9 changes: 1 addition & 8 deletions demo/tz/tz_triangle_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::v2f>(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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/tz/gpu/rhi_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
44 changes: 43 additions & 1 deletion src/tz/imgui.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
{
Expand All @@ -25,6 +34,8 @@ namespace tz
tz::gpu::resource_handle font_image = tz::nullhand;
tz::gpu::shader_handle shader = tz::nullhand;

std::vector<tz::gpu::resource_handle> data_buffers = {};

std::vector<tz::gpu::pass_handle> passes = {};
tz::gpu::graph_handle graph = tz::nullhand;
} render;
Expand Down Expand Up @@ -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();
Expand All @@ -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<std::size_t>(draw->TotalIdxCount) * sizeof(ImDrawIdx);
const auto req_vtx_size = static_cast<std::size_t>(draw->TotalVtxCount) * sizeof(ImDrawVert);

Expand All @@ -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<const shader_data*>(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<const shader_data>(&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));
Expand All @@ -157,10 +186,20 @@ namespace tz

void impl_push_pass()
{
shader_data data;
std::span<const shader_data> 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
};

Expand All @@ -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,
Expand All @@ -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");
Expand Down
13 changes: 12 additions & 1 deletion src/tz/imgui.fragment.tzsl
Original file line number Diff line number Diff line change
@@ -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;
}
}
43 changes: 42 additions & 1 deletion src/tz/imgui.vertex.tzsl
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7f523bf

Please sign in to comment.