Skip to content

Commit

Permalink
[ren.quad] added ability to specify extra data per quad. this is gett…
Browse files Browse the repository at this point in the history
…ing very complicated but necessary to make it truly extensible
  • Loading branch information
harrand committed Dec 8, 2024
1 parent 68bab70 commit 91c8cea
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions include/tz/gpu/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace tz::gpu
colour_target = 0b0001,
/// Image can be used as a depth target by a graphics pass.
depth_target = 0b0010,
/// Image will be automatically resized to match the dimensions of the window. In addition, @ref tz::gpu::image_info::width and @ref tz::gpu::image_info::height are ignored.
resize_to_match_window_resource = 0b0100,
};

constexpr image_flag operator|(image_flag lhs, image_flag rhs)
Expand Down
5 changes: 5 additions & 0 deletions include/tz/ren/quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ namespace tz::ren
default_targets[0] = tz::gpu::window_resource;
return {std::begin(default_targets), std::end(default_targets)};
}();
std::span<const tz::gpu::resource_handle> extra_resources = {};
/// Any extra optional flags to specify?
quad_renderer_flag flags = static_cast<quad_renderer_flag>(0);
/// Custom fragment shader. Unless @ref quad_renderer_flag::custom_fragment_shader is specified, this value is ignored and a default fragment shader is used.
std::string_view custom_fragment_shader = {};
/// How much extraneous GPU memory should be allocated per quad? If you want to store extra data per quad, you should put the size of that data here.
std::size_t extra_data_per_quad = 0;
};

/**
Expand Down Expand Up @@ -131,6 +134,8 @@ namespace tz::ren
std::expected<quad_handle, tz::error_code> quad_renderer_create_quad(quad_renderer_handle renh, quad_info info);
tz::error_code quad_renderer_destroy_quad(quad_renderer_handle renh, quad_handle quad);

void quad_renderer_set_quad_extra_data(quad_renderer_handle renh, quad_handle quad, std::span<const std::byte> data);

/**
* @ingroup tz_ren_quad
* @brief Associate an existing image resource with the provided quad renderer, allowing quads to use it as a texture.
Expand Down
20 changes: 20 additions & 0 deletions src/tz/gpu/rhi_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,11 @@ namespace tz::gpu
}
auto hanval = resources.size();
resource_info& res = resources.emplace_back();
if(info.flags & image_flag::resize_to_match_window_resource)
{
info.width = swapchain_width;
info.height = swapchain_height;
}
res.res = info;
std::string_view name = info.name;
if(name.empty())
Expand Down Expand Up @@ -3264,6 +3269,21 @@ namespace tz::gpu
{
tz_must(res);
}

// go through all resources.
for(std::size_t i = 0; i < resources.size(); i++)
{
const auto& res = resources[i];
if(res.is_image())
{
if(std::get<image_info>(res.res).flags & image_flag::resize_to_match_window_resource)
{
image_resize(static_cast<tz::hanval>(i), swapchain_width, swapchain_height);
}
}
}

// sanity check all passes
for(auto& pass : passes)
{
tz_must(impl_validate_colour_targets(pass.info, pass));
Expand Down
22 changes: 20 additions & 2 deletions src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace tz::ren
tz::gpu::resource_handle data_buffer = tz::nullhand;
tz::gpu::resource_handle camera_buffer = tz::nullhand;
tz::gpu::resource_handle settings_buffer = tz::nullhand;
tz::gpu::resource_handle extra_data_buffer = tz::nullhand;
tz::gpu::pass_handle main_pass = tz::nullhand;
tz::gpu::graph_handle graph = tz::nullhand;
std::vector<tz::gpu::resource_handle> colour_targets = {};
Expand Down Expand Up @@ -98,12 +99,23 @@ namespace tz::ren
.name = "Quad Renderer Settings Buffer"
}));

tz::gpu::resource_handle resources[] =
std::vector<std::byte> extra_data_buffer_initial_data;
extra_data_buffer_initial_data.resize(initial_quad_capacity * info.extra_data_per_quad);
ren.extra_data_buffer = tz_must(tz::gpu::create_buffer
({
.data = std::as_bytes(std::span<const std::byte>(extra_data_buffer_initial_data)),
.name = "Extra Data Buffer"
}));

std::vector<tz::gpu::resource_handle> resources =
{
ren.data_buffer,
ren.camera_buffer,
ren.settings_buffer
ren.settings_buffer,
ren.extra_data_buffer,
};
std::copy(info.extra_resources.begin(), info.extra_resources.end(), std::back_inserter(resources));

tz::gpu::resource_handle depth_target = tz::nullhand;
if(info.flags & quad_renderer_flag::enable_layering)
{
Expand Down Expand Up @@ -225,6 +237,12 @@ namespace tz::ren
return tz::error_code::success;
}

void quad_renderer_set_quad_extra_data(quad_renderer_handle renh, quad_handle quad, std::span<const std::byte> data)
{
auto& ren = renderers[renh.peek()];
tz_must(tz::gpu::resource_write(ren.extra_data_buffer, data, ren.info.extra_data_per_quad * quad.peek()));
}

std::expected<std::uint32_t, tz::error_code> quad_renderer_add_texture(quad_renderer_handle renh, tz::gpu::resource_handle image)
{
auto& ren = renderers[renh.peek()];
Expand Down
5 changes: 3 additions & 2 deletions src/tz/ren/quad.fragment.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ shader(type = fragment);

input(id = 0) vec3 tint;
input(id = 1) vec2 uv;
input(id = 2, flat) uint texture_id;
input(id = 3, flat) uint alpha_clipping;
input(id = 2, flat) uint quad_id;
input(id = 3, flat) uint texture_id;
input(id = 4, flat) uint alpha_clipping;
output(id = 0) vec4 colour;

void main()
Expand Down
6 changes: 4 additions & 2 deletions src/tz/ren/quad.vertex.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ buffer(id = 2) const settings

output(id = 0) vec3 out::tint;
output(id = 1) vec2 out::uv;
output(id = 2) uint out::texture_id;
output(id = 3) uint out::alpha_clipping;
output(id = 2) uint out::quad_id;
output(id = 3) uint out::texture_id;
output(id = 4) uint out::alpha_clipping;

vec2 quad_positions[6] = vec2[](
vec2(1.0, -1.0), vec2(1.0, 1.0), vec2(-1.0, -1.0),
Expand All @@ -42,6 +43,7 @@ vec2 quad_texcoords[6] = vec2[](
void main()
{
const uint quad_id = in::vertex_id / 6;
out::quad_id = quad_id;
vec2 local_pos = quad_positions[in::vertex_id % 6];
quad_data cur_quad = quad.data[quad_id];

Expand Down

0 comments on commit 91c8cea

Please sign in to comment.