Skip to content

Commit

Permalink
[ren.quad] api for adding textures and get/set current texture for a …
Browse files Browse the repository at this point in the history
…quad
  • Loading branch information
harrand committed Oct 31, 2024
1 parent 36fae7d commit 8ecf61b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
10 changes: 10 additions & 0 deletions include/tz/ren/quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_handle, tz::error_code> quad_renderer_create_quad(quad_renderer_handle renh, quad_info info);

std::expected<std::uint32_t, tz::error_code> 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);
}

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 @@ -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
Expand Down
54 changes: 53 additions & 1 deletion src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<unsigned int>(-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<const quad_data>(&new_data, 1)), sizeof(quad_data) * ren.quad_count));

return static_cast<tz::hanval>(ren.quad_count++);
}

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()];
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()];
Expand Down Expand Up @@ -139,6 +161,36 @@ namespace tz::ren
tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::v2f>(&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<const tz::v3f*>(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<const tz::v3f>(&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<const std::uint32_t*>(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<const std::uint32_t>(&texture_id, 1)), offset);
}

tz::gpu::graph_handle quad_renderer_graph(quad_renderer_handle renh)
{
return renderers[renh.peek()].graph;
Expand Down
5 changes: 3 additions & 2 deletions src/tz/ren/quad.fragment.tzsl
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/tz/ren/quad.vertex.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
}
6 changes: 3 additions & 3 deletions test/tz/ren_quad_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#include "tz/topaz.hpp"
#include "tz/ren/quad.hpp"
#include "tz/os/window.hpp"
Expand All @@ -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())
{
Expand Down

0 comments on commit 8ecf61b

Please sign in to comment.