Skip to content

Commit

Permalink
[ren.quad] quad_renderer can now be configured to use alpha clipping,…
Browse files Browse the repository at this point in the history
… and also its clear colour can be set at creation time
  • Loading branch information
harrand committed Nov 1, 2024
1 parent 74f07a0 commit 26a4c0e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
18 changes: 17 additions & 1 deletion include/tz/ren/quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,25 @@ namespace tz::ren
using quad_handle = tz::handle<detail::quad_t>;
using quad_renderer_handle = tz::handle<detail::quadren_t>;

struct quad_renderer_info
enum quad_renderer_flag
{
alpha_clipping = 0b0001,
};

constexpr quad_renderer_flag operator|(quad_renderer_flag lhs, quad_renderer_flag rhs)
{
return static_cast<quad_renderer_flag>(static_cast<int>(lhs) | static_cast<int>(rhs));
}

constexpr bool operator&(quad_renderer_flag lhs, quad_renderer_flag& rhs)
{
return static_cast<int>(lhs) & static_cast<int>(rhs);
}

struct quad_renderer_info
{
tz::v4f clear_colour = {0.0f, 0.0f, 0.0f, 1.0f};
quad_renderer_flag flags = static_cast<quad_renderer_flag>(0);
};

std::expected<quad_renderer_handle, tz::error_code> create_quad_renderer(quad_renderer_info info);
Expand Down
12 changes: 11 additions & 1 deletion src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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::resource_handle settings_buffer = tz::nullhand;
tz::gpu::pass_handle main_pass = tz::nullhand;
tz::gpu::graph_handle graph = tz::nullhand;
std::vector<quad_internal_data> internals = {};
Expand Down Expand Up @@ -75,19 +76,28 @@ namespace tz::ren
.name = "Quad Renderer Camera Buffer"
}));

auto settings_val = static_cast<std::uint32_t>(info.flags);
ren.settings_buffer = tz_must(tz::gpu::create_buffer
({
.data = std::as_bytes(std::span<const std::uint32_t>(&settings_val, 1)),
.name = "Quad Renderer Settings Buffer"
}));

tz::gpu::resource_handle colour_targets[] =
{
tz::gpu::window_resource
};
tz::gpu::resource_handle resources[] =
{
ren.data_buffer,
ren.camera_buffer
ren.camera_buffer,
ren.settings_buffer
};
auto maybe_pass = tz::gpu::create_pass
({
.graphics =
{
.clear_colour = info.clear_colour,
.colour_targets = colour_targets,
.flags = tz::gpu::graphics_flag::no_depth_test
},
Expand Down
8 changes: 7 additions & 1 deletion src/tz/ren/quad.fragment.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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;
output(id = 0) vec4 colour;

void main()
Expand All @@ -11,7 +12,12 @@ void main()
vec3 frag_colour = tint;
if(texture_id != -1)
{
frag_colour *= sample(texture_id, uv).xyz;
vec4 texcol = sample(texture_id, uv);
if(alpha_clipping == 1 && texcol.a < 0.05f)
{
discard;
}
frag_colour *= texcol.xyz;
}

colour = vec4(frag_colour, 1.0f);
Expand Down
7 changes: 7 additions & 0 deletions src/tz/ren/quad.vertex.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ resource(id = 1) const buffer camera
mat4 projection;
};

resource(id = 2) const buffer settings
{
uint value;
};

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;

vec2 quad_positions[6] = vec2[](
vec2(1.0, -1.0), vec2(1.0, 1.0), vec2(-1.0, -1.0),
Expand All @@ -41,4 +47,5 @@ void main()
out::tint = vec3(cur_quad.colour);
out::uv = quad_texcoords[in::vertex_id % 6];
out::texture_id = cur_quad.texture_id;
out::alpha_clipping = settings.value & 0x01;
}

0 comments on commit 26a4c0e

Please sign in to comment.