diff --git a/include/tz/ren/quad.hpp b/include/tz/ren/quad.hpp index adfb718ed2..0c735ec29e 100644 --- a/include/tz/ren/quad.hpp +++ b/include/tz/ren/quad.hpp @@ -26,15 +26,25 @@ namespace tz::ren tz::v2f position = tz::v2f::zero(); float rotation = 0.0f; tz::v2f scale = tz::v2f::filled(1.0f); + std::uint32_t texture_id = -1; + tz::v3f colour = tz::v3f::filled(1.0f); }; std::expected quad_renderer_create_quad(quad_renderer_handle renh, quad_info info); + std::expected quad_renderer_add_texture(quad_renderer_handle renh, tz::gpu::resource_handle image); + tz::v2f get_quad_position(quad_renderer_handle renh, quad_handle quad); void set_quad_position(quad_renderer_handle renh, quad_handle quad, tz::v2f position); tz::v2f get_quad_scale(quad_renderer_handle renh, quad_handle quad); void set_quad_scale(quad_renderer_handle renh, quad_handle quad, tz::v2f scale); + tz::v3f get_quad_colour(quad_renderer_handle renh, quad_handle quad); + void set_quad_colour(quad_renderer_handle renh, quad_handle quad, tz::v3f colour); + + std::uint32_t get_quad_texture(quad_renderer_handle renh, quad_handle quad); + 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); } diff --git a/src/tz/gpu/rhi_vulkan.cpp b/src/tz/gpu/rhi_vulkan.cpp index a7161fe00b..43b3dfba65 100644 --- a/src/tz/gpu/rhi_vulkan.cpp +++ b/src/tz/gpu/rhi_vulkan.cpp @@ -1106,7 +1106,7 @@ namespace tz::gpu std::size_t ret_id = passes.size(); auto& pass = passes.emplace_back(); std::size_t buffer_count = 0; - pass.resources.resize(info.resources.size()); + pass.resources.reserve(info.resources.size()); for(resource_handle resh : info.resources) { // todo: assert not widnow resouces or nullhand diff --git a/src/tz/ren/quad.cpp b/src/tz/ren/quad.cpp index 7a0c707ec9..5d73dda4df 100644 --- a/src/tz/ren/quad.cpp +++ b/src/tz/ren/quad.cpp @@ -17,6 +17,7 @@ namespace tz::ren tz::gpu::pass_handle main_pass = tz::nullhand; tz::gpu::graph_handle graph = tz::nullhand; std::size_t quad_count = 0; + std::size_t texture_count = 0; }; struct quad_data @@ -98,15 +99,36 @@ namespace tz::ren { quad_data new_data; new_data.pos_scale = {info.position[0], info.position[1], info.scale[0], info.scale[1]}; - (void)info; + new_data.colour_tint = info.colour; + new_data.texture_id = info.texture_id; auto& ren = renderers[renh.peek()]; + + if(info.texture_id != static_cast(-1)) + { + if(info.texture_id >= ren.texture_count) + { + UNERR(tz::error_code::invalid_value, "attempt to create quad with texture-id {}, but this is not a valid texture -- the quad renderer only has {} registered textures", info.texture_id, ren.texture_count); + } + } + tz::gpu::pass_set_triangle_count(ren.main_pass, (ren.quad_count + 1) * 2); tz_must(tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&new_data, 1)), sizeof(quad_data) * ren.quad_count)); return static_cast(ren.quad_count++); } + std::expected quad_renderer_add_texture(quad_renderer_handle renh, tz::gpu::resource_handle image) + { + auto& ren = renderers[renh.peek()]; + tz::error_code err = tz::gpu::pass_add_image_resource(ren.main_pass, image); + if(err != tz::error_code::success) + { + return std::unexpected(err); + } + return ren.texture_count++; + } + tz::v2f get_quad_position(quad_renderer_handle renh, quad_handle quad) { const auto& ren = renderers[renh.peek()]; @@ -139,6 +161,36 @@ namespace tz::ren tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&scale, 1)), offset); } + tz::v3f get_quad_colour(quad_renderer_handle renh, quad_handle quad) + { + const auto& ren = renderers[renh.peek()]; + auto quad_data_array = tz::gpu::resource_read(ren.data_buffer); + return *reinterpret_cast(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour_tint)); + } + + void set_quad_colour(quad_renderer_handle renh, quad_handle quad, tz::v3f colour) + { + auto& ren = renderers[renh.peek()]; + std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, colour_tint); + + tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&colour, 1)), offset); + } + + std::uint32_t get_quad_texture(quad_renderer_handle renh, quad_handle quad) + { + const auto& ren = renderers[renh.peek()]; + auto quad_data_array = tz::gpu::resource_read(ren.data_buffer); + return *reinterpret_cast(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, texture_id)); + } + + void set_quad_texture(quad_renderer_handle renh, quad_handle quad, std::uint32_t texture_id) + { + auto& ren = renderers[renh.peek()]; + std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, texture_id); + + tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span(&texture_id, 1)), offset); + } + tz::gpu::graph_handle quad_renderer_graph(quad_renderer_handle renh) { return renderers[renh.peek()].graph; diff --git a/src/tz/ren/quad.fragment.tzsl b/src/tz/ren/quad.fragment.tzsl index 76c386c23e..c608bd08dd 100644 --- a/src/tz/ren/quad.fragment.tzsl +++ b/src/tz/ren/quad.fragment.tzsl @@ -1,7 +1,8 @@ shader(type = fragment); input(id = 0) vec3 tint; -input(id = 1, flat) uint texture_id; +input(id = 1) vec2 uv; +input(id = 2, flat) uint texture_id; output(id = 0) vec4 colour; void main() @@ -10,7 +11,7 @@ void main() vec3 frag_colour = tint; if(texture_id != -1) { - frag_colour *= sample(texture_id, vec2(0.0)).xyz; + frag_colour *= sample(texture_id, uv).xyz; } colour = vec4(frag_colour, 1.0f); diff --git a/src/tz/ren/quad.vertex.tzsl b/src/tz/ren/quad.vertex.tzsl index c5a1202a6b..3230f397f5 100644 --- a/src/tz/ren/quad.vertex.tzsl +++ b/src/tz/ren/quad.vertex.tzsl @@ -15,7 +15,8 @@ resource(id = 0) const buffer quad }; output(id = 0) vec3 out::tint; -output(id = 1) uint out::texture_id; +output(id = 1) vec2 out::uv; +output(id = 2) uint out::texture_id; vec2 quad_positions[6] = vec2[]( vec2(1.0, -1.0), vec2(1.0, 1.0), vec2(-1.0, -1.0), @@ -37,5 +38,6 @@ void main() out::position = 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; } \ No newline at end of file diff --git a/test/tz/ren_quad_test.cpp b/test/tz/ren_quad_test.cpp index 4222e11422..1268321131 100644 --- a/test/tz/ren_quad_test.cpp +++ b/test/tz/ren_quad_test.cpp @@ -1,3 +1,4 @@ + #include "tz/topaz.hpp" #include "tz/ren/quad.hpp" #include "tz/os/window.hpp" @@ -14,10 +15,9 @@ int main() tz::ren::quad_renderer_handle ren = tz_must(tz::ren::create_quad_renderer({})); + tz::ren::quad_handle quad1 = tz_must(tz::ren::quad_renderer_create_quad(ren, {.position = tz::v2f::zero(), .scale = {0.2f, 0.2f}, .colour = {0.0f, 1.0f, 0.25f}})); - tz::ren::quad_handle quad1 = tz_must(tz::ren::quad_renderer_create_quad(ren, {.position = tz::v2f::zero(), .scale = {0.2f, 0.2f}})); - - tz_must(tz::ren::quad_renderer_create_quad(ren, {.position = {-0.5f, -0.5f}, .scale = {0.15f, 0.15f}})); + tz_must(tz::ren::quad_renderer_create_quad(ren, {.position = {-0.5f, -0.5f}, .scale = {0.15f, 0.15f}, .colour = {0.5f, 0.1f, 0.85f}})); while(tz::os::window_is_open()) {