Skip to content

Commit

Permalink
[imgui] imgui backend WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Nov 14, 2024
1 parent a4c9e11 commit f504dd3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ topaz_add_library(
src/tz/io/image.cpp
src/tz/ren/quad.cpp
SHADERS
src/tz/imgui.vertex.tzsl
src/tz/imgui.fragment.tzsl
src/tz/ren/quad.vertex.tzsl
src/tz/ren/quad.fragment.tzsl
DEFINE_PACKAGE
Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ add_library(imgui STATIC
imgui/misc/cpp/imgui_stdlib.cpp
)
target_include_directories(imgui PUBLIC "${PROJECT_SOURCE_DIR}/lib/imgui")
target_compile_definitions(imgui PRIVATE "-DIMGUI_USER_CONFIG=\"${PROJECT_SOURCE_DIR}/lib/topaz_imgui_config.hpp\"")
target_compile_definitions(imgui PUBLIC "-DIMGUI_USER_CONFIG=\"${PROJECT_SOURCE_DIR}/lib/topaz_imgui_config.hpp\"")

add_library(stb_image INTERFACE)
target_include_directories(stb_image INTERFACE stb)
8 changes: 8 additions & 0 deletions src/tz/gpu/rhi_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,14 @@ namespace tz::gpu
pass.resources.reserve(info.resources.size());
for(resource_handle resh : info.resources)
{
if(resh == tz::nullhand)
{
UNERR(tz::error_code::invalid_value, "One of the resources passed into creation of pass {} was the null resource. You cannot use the null resource within a pass.", info.name);
}
if(resh == tz::gpu::window_resource)
{
UNERR(tz::error_code::invalid_value, "One of the resources passed into creation of pass {} was the window resource. You cannot use the window resource within a pass.", info.name);
}
// todo: assert not widnow resouces or nullhand
const auto& res = resources[resh.peek()];
if(res.is_buffer())
Expand Down
71 changes: 69 additions & 2 deletions src/tz/imgui.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
#include "tz/imgui.hpp"
#include "tz/topaz.hpp"
#include "tz/gpu/resource.hpp"
#include "tz/core/memory.hpp"
#include "tz/gpu/hardware.hpp"
#include "tz/gpu/resource.hpp"
#include "tz/gpu/shader.hpp"
#include "tz/gpu/pass.hpp"
#include <span>
#include <array>

#include ImportedShaderHeader(imgui, vertex)
#include ImportedShaderHeader(imgui, fragment)

namespace tz::detail
{
// imgui backend initialise.
struct render_data
{
tz::gpu::resource_handle vertex_buffer = tz::nullhand;
tz::gpu::resource_handle index_buffer = tz::nullhand;

tz::gpu::resource_handle font_image = tz::nullhand;
tz::gpu::shader_handle shader = tz::nullhand;

tz::gpu::pass_handle pass = tz::nullhand;
} render;

void impl_write_vertices_and_indices(ImDrawData* draw);

void imgui_initialise()
{
ImGui::CreateContext();
Expand All @@ -30,18 +45,70 @@ namespace tz::detail
io.Fonts->GetTexDataAsRGBA32(&fontptr, &fontw, &fonth);
std::span<unsigned char> fontdata{fontptr, static_cast<std::size_t>(fontw * fonth * 4u)};

// reserve initially 1024 vertices (and indices).
static_assert(sizeof(tz::gpu::index_t) == sizeof(ImDrawIdx));
std::array<tz::gpu::index_t, 1024> initial_indices;
std::array<ImDrawVert, 1024> initial_vertices;
render.vertex_buffer = tz_must(tz::gpu::create_buffer({
.data = tz::view_bytes(initial_vertices),
.name = "ImGui Vertex Buffer"
}));

render.index_buffer = tz_must(tz::gpu::create_buffer({
.data = tz::view_bytes(initial_indices),
.name = "ImGui Index Buffer"
}));

render.font_image = tz_must(tz::gpu::create_image
({
.width = static_cast<unsigned int>(fontw),
.height = static_cast<unsigned int>(fonth),
.data = std::as_bytes(fontdata),
.name = "ImGui Font image"
}));

render.shader = tz_must(tz::gpu::create_graphics_shader(
ImportedShaderSource(imgui, vertex),
ImportedShaderSource(imgui, fragment)
));

tz::gpu::resource_handle resources[] =
{
render.vertex_buffer,
render.index_buffer,
render.font_image
};

tz::gpu::resource_handle colour_targets[] =
{
tz::gpu::window_resource
};

render.pass = tz_must(tz::gpu::create_pass({
.graphics =
{
.colour_targets = colour_targets,
.flags = tz::gpu::graphics_flag::dont_clear | tz::gpu::graphics_flag::no_depth_test,
},
.shader = render.shader,
.resources = resources,
}));
}

void imgui_terminate()
{

tz::gpu::destroy_pass(render.pass);
tz_must(tz::gpu::destroy_resource(render.font_image));
tz_must(tz::gpu::destroy_resource(render.index_buffer));
tz_must(tz::gpu::destroy_resource(render.vertex_buffer));
tz::gpu::destroy_shader(render.shader);
ImGui::DestroyContext();
}

// impl bits

void impl_write_vertices_and_indices(ImDrawData* draw)
{
(void)draw;
}
}
6 changes: 6 additions & 0 deletions src/tz/imgui.fragment.tzsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
shader(type = fragment);

void main()
{

}
6 changes: 6 additions & 0 deletions src/tz/imgui.vertex.tzsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
shader(type = vertex);

void main()
{

}

0 comments on commit f504dd3

Please sign in to comment.