diff --git a/include/tz/ren/quad.hpp b/include/tz/ren/quad.hpp index 90f2c9c73d..75a4a4a8ad 100644 --- a/include/tz/ren/quad.hpp +++ b/include/tz/ren/quad.hpp @@ -14,9 +14,25 @@ namespace tz::ren using quad_handle = tz::handle; using quad_renderer_handle = tz::handle; - 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(static_cast(lhs) | static_cast(rhs)); + } + + constexpr bool operator&(quad_renderer_flag lhs, quad_renderer_flag& rhs) { + return static_cast(lhs) & static_cast(rhs); + } + struct quad_renderer_info + { + tz::v4f clear_colour = {0.0f, 0.0f, 0.0f, 1.0f}; + quad_renderer_flag flags = static_cast(0); }; std::expected create_quad_renderer(quad_renderer_info info); diff --git a/src/tz/ren/quad.cpp b/src/tz/ren/quad.cpp index a6e4515792..fbf95a56c2 100644 --- a/src/tz/ren/quad.cpp +++ b/src/tz/ren/quad.cpp @@ -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 internals = {}; @@ -75,6 +76,13 @@ namespace tz::ren .name = "Quad Renderer Camera Buffer" })); + auto settings_val = static_cast(info.flags); + ren.settings_buffer = tz_must(tz::gpu::create_buffer + ({ + .data = std::as_bytes(std::span(&settings_val, 1)), + .name = "Quad Renderer Settings Buffer" + })); + tz::gpu::resource_handle colour_targets[] = { tz::gpu::window_resource @@ -82,12 +90,14 @@ namespace tz::ren 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 }, diff --git a/src/tz/ren/quad.fragment.tzsl b/src/tz/ren/quad.fragment.tzsl index c608bd08dd..47abcb3be4 100644 --- a/src/tz/ren/quad.fragment.tzsl +++ b/src/tz/ren/quad.fragment.tzsl @@ -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() @@ -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); diff --git a/src/tz/ren/quad.vertex.tzsl b/src/tz/ren/quad.vertex.tzsl index 60febd4972..e0e239a49a 100644 --- a/src/tz/ren/quad.vertex.tzsl +++ b/src/tz/ren/quad.vertex.tzsl @@ -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), @@ -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; } \ No newline at end of file