Skip to content

Commit

Permalink
[gpu.vulkan] extra EC when submitting scratch commands
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Oct 31, 2024
1 parent 09b2823 commit 3b0e31d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/tz/ren/quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace tz::ren
void set_quad_texture(quad_renderer_handle renh, quad_handle quad, std::uint32_t texture_id);

tz::gpu::graph_handle quad_renderer_graph(quad_renderer_handle renh);
void quad_renderer_update(quad_renderer_handle renh);
}

#endif // TOPAZ_REN_QUAD_HPP
3 changes: 2 additions & 1 deletion src/tz/gpu/rhi_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2324,7 +2324,8 @@ namespace tz::gpu
.pSignalSemaphores = nullptr
};
// todo: check errors.
vkQueueSubmit(graphics_compute_queue, 1, &submit, scratch_fence);
VkResult res = vkQueueSubmit(graphics_compute_queue, 1, &submit, scratch_fence);
tz_assert(res == VK_SUCCESS, "ruh roh");
vkWaitForFences(current_device, 1, &scratch_fence, VK_TRUE, std::numeric_limits<std::uint64_t>::max());
vkResetFences(current_device, 1, &scratch_fence);
}
Expand Down
30 changes: 28 additions & 2 deletions src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tz/ren/quad.hpp"
#include "tz/core/matrix.hpp"
#include "tz/topaz.hpp"
#include "tz/gpu/resource.hpp"
#include "tz/gpu/pass.hpp"
Expand All @@ -14,6 +15,7 @@ namespace tz::ren
{
quad_renderer_info info = {};
tz::gpu::resource_handle data_buffer = tz::nullhand;
tz::gpu::resource_handle camera_buffer = tz::nullhand;
tz::gpu::pass_handle main_pass = tz::nullhand;
tz::gpu::graph_handle graph = tz::nullhand;
std::size_t quad_count = 0;
Expand All @@ -27,6 +29,11 @@ namespace tz::ren
std::uint32_t texture_id = -1;
};

struct camera_data
{
tz::m4f projection = tz::m4f::iden();
};

constexpr std::size_t initial_quad_capacity = 1024;

std::vector<quad_renderer_data> renderers;
Expand All @@ -47,17 +54,25 @@ namespace tz::ren
ren.data_buffer = tz_must(tz::gpu::create_buffer
({
.data = std::as_bytes(std::span<const quad_data>(initial_quad_data)),
.name = "Quad Renderer Main Pass",
.name = "Quad Renderer Quad Buffer",
.flags = tz::gpu::buffer_flag::dynamic_access
}));

camera_data initial_camera_data;
ren.camera_buffer = tz_must(tz::gpu::create_buffer
({
.data = std::as_bytes(std::span<const camera_data>(&initial_camera_data, 1)),
.name = "Quad Renderer Camera Buffer"
}));

tz::gpu::resource_handle colour_targets[] =
{
tz::gpu::window_resource
};
tz::gpu::resource_handle resources[] =
{
ren.data_buffer
ren.data_buffer,
ren.camera_buffer
};
auto maybe_pass = tz::gpu::create_pass
({
Expand Down Expand Up @@ -91,6 +106,11 @@ namespace tz::ren
auto& ren = renderers[renh.peek()];
tz::gpu::destroy_pass(ren.main_pass);
auto ret = tz::gpu::destroy_resource(ren.data_buffer);
if(ret != tz::error_code::success)
{
return ret;
}
ret = tz::gpu::destroy_resource(ren.camera_buffer);
ren = {};
return ret;
}
Expand Down Expand Up @@ -195,4 +215,10 @@ namespace tz::ren
{
return renderers[renh.peek()].graph;
}

void quad_renderer_update(quad_renderer_handle renh)
{
auto& ren = renderers[renh.peek()];
(void)ren;
}
}
7 changes: 6 additions & 1 deletion src/tz/ren/quad.vertex.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ resource(id = 0) const buffer quad
quad_data data[];
};

resource(id = 1) const buffer camera
{
mat4 projection;
};

output(id = 0) vec3 out::tint;
output(id = 1) vec2 out::uv;
output(id = 2) uint out::texture_id;
Expand All @@ -36,7 +41,7 @@ void main()

vec2 position_worldspace = cur_quad.pos_scale.xy + (local_pos * cur_quad.pos_scale.zw);

out::position = vec4(position_worldspace, 0, 1);
out::position = camera.projection * vec4(position_worldspace, 0, 1);
out::tint = vec3(cur_quad.tint_red, cur_quad.tint_green, cur_quad.tint_blue);
out::uv = quad_texcoords[in::vertex_id % 6];
out::texture_id = cur_quad.texture_id;
Expand Down

0 comments on commit 3b0e31d

Please sign in to comment.