Skip to content

Commit

Permalink
[ren.quad] quad_renderer can now be provided a custom fragment shader
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Dec 8, 2024
1 parent c6be473 commit 66285bc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
12 changes: 8 additions & 4 deletions include/tz/ren/quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ namespace tz::ren
enum quad_renderer_flag : std::int64_t
{
/// If the alpha-component of any fragment in a quad (after texture sampling) is very low (<0.05f), then that fragment will be discarded.
alpha_clipping = 0b0001,
alpha_clipping = 0b00000001,
/// Sets @ref tz::gpu::graph_flag::present_after on the graph representing the quad renderer.
graph_present_after = 0b0010,
graph_present_after = 0b00000010,
/// Normally if a quad has a negative scale in any dimension, the triangles are no longer in the correct winding order and will thus be invisible. Setting this flags disables face culling, meaning triangles scaled negatively (and thus in the wrong winding order) will still display as normal.
allow_negative_scale = 0b0100,
allow_negative_scale = 0b00000100,
/// Enables the use of the layer property of a quad. By default, everything is on layer 0. Layer values are between -100 and 100. A quad with a higher layer will be drawn over a quad with a lower layer.
enable_layering = 0b1000,
enable_layering = 0b00001000,
/// Enables the use of @ref quad_renderer_info::custom_fragment_shader.
custom_fragment_shader = 0b00010000,
};

constexpr quad_renderer_flag operator|(quad_renderer_flag lhs, quad_renderer_flag rhs)
Expand All @@ -74,6 +76,8 @@ namespace tz::ren
tz::gpu::resource_handle colour_target = tz::gpu::window_resource;
/// Any extra optional flags to specify?
quad_renderer_flag flags = static_cast<quad_renderer_flag>(0);
/// Custom fragment shader.
std::string custom_fragment_shader = {};
};

/**
Expand Down
7 changes: 6 additions & 1 deletion src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ namespace tz::ren
{
if(main_pass_shader == tz::nullhand)
{
main_pass_shader = tz_must(tz::gpu::create_graphics_shader(ImportedShaderSource(quad, vertex), ImportedShaderSource(quad, fragment)));
std::string_view fragment_source = ImportedShaderSource(quad, fragment);
if(info.flags & quad_renderer_flag::custom_fragment_shader)
{
fragment_source = info.custom_fragment_shader;
}
main_pass_shader = tz_must(tz::gpu::create_graphics_shader(ImportedShaderSource(quad, vertex), fragment_source));
}

std::size_t id = renderers.size();
Expand Down
10 changes: 5 additions & 5 deletions src/tz/ren/quad.fragment.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ output(id = 0) vec4 colour;

void main()
{
//colour = vec4(1.0f, 0.0f, 1.0f, 1.0f);
vec3 frag_colour = tint;
vec4 frag_colour = vec4(tint, 0.0f);
if(texture_id != -1)
{
vec4 texcol = sample(texture_id, uv);
frag_colour.a = texcol.a;
if(alpha_clipping > 0 && texcol.a < 0.05f)
{
discard;
}
frag_colour *= texcol.xyz;
frag_colour.xyz *= texcol.xyz;
}

colour = vec4(frag_colour, 1.0f);
}
colour = frag_colour;
}

0 comments on commit 66285bc

Please sign in to comment.