Skip to content

Commit

Permalink
[ren.quad] quad_renderer_flag::enable_layering has been added. layeri…
Browse files Browse the repository at this point in the history
…ng, when enabled, will draw the quads in order depending on their layer value. -100 is the furthest in the back and 100 is the closest to the front. if disabled, then the old behaviour remains - quads are drawn in the order they were specified i.e quad_handle.peek()
  • Loading branch information
harrand committed Nov 19, 2024
1 parent 9bace70 commit c481a7b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
21 changes: 21 additions & 0 deletions include/tz/ren/quad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace tz::ren
graph_present_after = 0b0010,
/// 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,
/// 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,
};

constexpr quad_renderer_flag operator|(quad_renderer_flag lhs, quad_renderer_flag rhs)
Expand Down Expand Up @@ -104,6 +106,8 @@ namespace tz::ren
std::uint32_t texture_id = -1;
/// Colour of the quad. If the quad has no texture, this will be the exact colour of the whole quad. If the quad *does* have a texture, then the sampled texture colour will be multiplied by this value (in which case you will often want to provide {1, 1, 1}). You can change this later via @ref set_quad_colour.
tz::v3f colour = tz::v3f::filled(1.0f);
/// Layer value. Has no effect if layering is not enabled (see @ref quad_renderer_flag::enable_layering for more details).
short layer = 0;
};

/**
Expand Down Expand Up @@ -137,6 +141,23 @@ namespace tz::ren
void set_quad_position(quad_renderer_handle renh, quad_handle quad, tz::v2f position);

/**
* @ingroup tz_ren_quad
* @brief Get the layer value of a given quad.
*
* Note that layer values only have an effect if layering (see @ref quad_renderer_flag::enable_layering) is enabled.
*/
short get_quad_layer(quad_renderer_handle renh, quad_handle quad);
/**
* @ingroup tz_ren_quad
* @brief Set the layer value of a given quad.
* @param layer New layer value. Should be between -100 and 100.
*
* Note that layer values only have an effect if layering (see @ref quad_renderer_flag::enable_layering) is enabled.
*/
void set_quad_layer(quad_renderer_handle renh, quad_handle quad, short layer);

/**
* @ingroup tz_ren_quad
* @brief Retrieve the scale factor of a quad, in both dimensions.
*/
Expand Down
29 changes: 27 additions & 2 deletions src/tz/ren/quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace tz::ren
tz::m4f model = tz::m4f::iden();
tz::v3f colour = {1.0f, 1.0f, 1.0f};
std::uint32_t texture_id = -1;
std::int32_t layer = 0;
std::uint32_t unused[3];
};

struct camera_data
Expand Down Expand Up @@ -95,14 +97,20 @@ namespace tz::ren
ren.camera_buffer,
ren.settings_buffer
};
tz::gpu::resource_handle depth_target = tz::nullhand;
if(info.flags & quad_renderer_flag::enable_layering)
{
depth_target = tz::gpu::window_resource;
}
auto maybe_pass = tz::gpu::create_pass
({
.graphics =
{
.clear_colour = info.clear_colour,
.colour_targets = colour_targets,
.depth_target = depth_target,
.culling = (info.flags & quad_renderer_flag::allow_negative_scale) ? tz::gpu::cull::none : tz::gpu::cull::back,
.flags = tz::gpu::graphics_flag::no_depth_test
.flags = (info.flags & quad_renderer_flag::enable_layering) ? static_cast<tz::gpu::graphics_flag>(0) : tz::gpu::graphics_flag::no_depth_test
},
.shader = main_pass_shader,
.resources = resources,
Expand Down Expand Up @@ -172,6 +180,7 @@ namespace tz::ren
new_data.model = internal.transform.matrix();
new_data.colour = info.colour;
new_data.texture_id = info.texture_id;
new_data.layer = info.layer;

if(info.texture_id != static_cast<unsigned int>(-1))
{
Expand Down Expand Up @@ -237,6 +246,22 @@ namespace tz::ren
tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::m4f>(&model, 1)), sizeof(quad_data) * quad.peek() + offsetof(quad_data, model));
}

short get_quad_layer(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::int32_t*>(quad_data_array.data() + (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, layer));
}

void set_quad_layer(quad_renderer_handle renh, quad_handle quad, short layer)
{
std::int32_t layer_value = std::clamp(static_cast<std::int32_t>(layer), -100, 100);
auto& ren = renderers[renh.peek()];
std::size_t offset = (sizeof(quad_data) * quad.peek()) + offsetof(quad_data, layer);

tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const std::int32_t>(&layer_value, 1)), offset);
}

tz::v2f get_quad_scale(quad_renderer_handle renh, quad_handle quad)
{
const auto& ren = renderers[renh.peek()];
Expand All @@ -248,7 +273,7 @@ namespace tz::ren
{
auto& ren = renderers[renh.peek()];
auto& internal = ren.internals[quad.peek()];
internal.transform.scale = {scale[0], scale[1]};
internal.transform.scale = {scale[0], scale[1], 1.0f};

tz::m4f model = internal.transform.matrix();
tz::gpu::resource_write(ren.data_buffer, std::as_bytes(std::span<const tz::m4f>(&model, 1)), sizeof(quad_data) * quad.peek() + offsetof(quad_data, model));
Expand Down
2 changes: 1 addition & 1 deletion src/tz/ren/quad.fragment.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main()
if(texture_id != -1)
{
vec4 texcol = sample(texture_id, uv);
if(alpha_clipping == 1 && texcol.a < 0.05f)
if(alpha_clipping > 0 && texcol.a < 0.05f)
{
discard;
}
Expand Down
11 changes: 10 additions & 1 deletion src/tz/ren/quad.vertex.tzsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ struct quad_data
mat4 model;
vec3 colour;
uint texture_id;
int layer;
uint unused[3];
};

buffer(id = 0) const quad
Expand Down Expand Up @@ -43,7 +45,14 @@ void main()
vec2 local_pos = quad_positions[in::vertex_id % 6];
quad_data cur_quad = quad.data[quad_id];

out::position = camera.projection * cur_quad.model * vec4(local_pos, 0, 1);
uint layering_enabled = settings.value & 0x08;

float zcoord = 0.0;
if(layering_enabled > 0)
{
zcoord = -1.0 + ((cur_quad.layer + 100) / 200.0);
}
out::position = camera.projection * cur_quad.model * vec4(local_pos, zcoord, 1);
out::tint = vec3(cur_quad.colour);
out::uv = quad_texcoords[in::vertex_id % 6];
out::texture_id = cur_quad.texture_id;
Expand Down

0 comments on commit c481a7b

Please sign in to comment.