Skip to content

Commit

Permalink
Refactor primitive topology.
Browse files Browse the repository at this point in the history
- Remove `Shader::topology`.
- Store `Topology` in `Geometry`, pass to shader through `RenderPrimitive`.
- Add `LineRectShape`.
- Fixup tools.
  • Loading branch information
karnkaul committed Feb 2, 2024
1 parent b8e1b12 commit f2348fe
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 30 deletions.
2 changes: 2 additions & 0 deletions lib/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
- Added bave::KeyState and bave::App::get_key_state().
- Added bave::EnumFlags and bave::EnumArray.
- Added bave::ParticleSystem.
- Added bave::LineRectShape (specialization of bave::Shape).

### Changes

- Unified all bit flags to be bave::EnumFlags.
- Renamed `particle_emitter.?pp` to `particle_system.?pp`.
- Removed primitive topology from bave::Shader. It is now stored in bave::Geometry, the shader obtains it from passed bave::RenderPrimitive during bave::Shader::draw.
1 change: 1 addition & 0 deletions lib/include/bave/graphics/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Drawable : public IDrawable {
std::uint32_t verts{};
std::uint32_t indices{};
std::size_t ibo_offset{};
Topology topology{};

void write(Geometry const& geometry);
void clear();
Expand Down
6 changes: 5 additions & 1 deletion lib/include/bave/graphics/geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <bave/core/radians.hpp>
#include <bave/graphics/rect.hpp>
#include <bave/graphics/rgba.hpp>
#include <bave/graphics/topology.hpp>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
Expand All @@ -26,6 +27,7 @@ struct LineRect;
struct Geometry {
std::vector<Vertex> vertices{};
std::vector<std::uint32_t> indices{};
Topology topology{Topology::eTriangleStrip};

auto append(std::span<Vertex const> vs, std::span<std::uint32_t const> is) -> Geometry&;

Expand Down Expand Up @@ -112,5 +114,7 @@ struct NineQuad {
auto operator==(NineQuad const&) const -> bool = default;
};

struct LineRect : Quad {};
struct LineRect : Quad {
[[nodiscard]] auto to_geometry() const -> Geometry { return Geometry::from(*this); }
};
} // namespace bave
2 changes: 2 additions & 0 deletions lib/include/bave/graphics/render_instance.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <bave/graphics/rgba.hpp>
#include <bave/graphics/topology.hpp>
#include <bave/graphics/transform.hpp>
#include <span>

Expand All @@ -26,6 +27,7 @@ struct RenderPrimitive {
std::size_t ibo_offset{};
std::uint32_t vertices{};
std::uint32_t indices{};
Topology topology{Topology::eTriangleStrip};
};

inline auto RenderInstance::bake() const -> Baked { return Baked{.transform = transform.matrix(), .rgba = Rgba::to_linear(tint.to_vec4())}; }
Expand Down
8 changes: 3 additions & 5 deletions lib/include/bave/graphics/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ class Shader {
public:
static constexpr auto max_textures_v = detail::SetLayout::max_textures_v;

explicit Shader(NotNull<class Renderer const*> renderer, vk::ShaderModule vertex, vk::ShaderModule fragment);
inline static auto default_line_width{1.0f}; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

void set_line_strip(float line_width);
void set_tri_strip();
explicit Shader(NotNull<class Renderer const*> renderer, vk::ShaderModule vertex, vk::ShaderModule fragment);

auto update_texture(CombinedImageSampler cis, std::uint32_t binding = 0) -> bool;
void update_textures(std::span<CombinedImageSampler const, max_textures_v> cis);
Expand All @@ -24,8 +23,7 @@ class Shader {

void draw(RenderPrimitive const& primitive, std::span<RenderInstance::Baked const> instances);

float line_width{1.0f};
vk::PrimitiveTopology topology{vk::PrimitiveTopology::eTriangleList};
float line_width{default_line_width};
vk::PolygonMode polygon_mode{vk::PolygonMode::eFill};

private:
Expand Down
3 changes: 2 additions & 1 deletion lib/include/bave/graphics/shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CustomShape : public BasicShape {
void set_geometry(Geometry geometry) { Drawable::set_geometry(std::move(geometry)); }
};

/// \brief Class template for all shape specs.
/// \brief Class template for all shapes.
template <typename ShapeT>
class Shape : public BasicShape {
public:
Expand All @@ -45,4 +45,5 @@ using QuadShape = Shape<Quad>;
using CircleShape = Shape<Circle>;
using RoundedQuadShape = Shape<RoundedQuad>;
using NineQuadShape = Shape<NineQuad>;
using LineRectShape = Shape<LineRect>;
} // namespace bave
6 changes: 6 additions & 0 deletions lib/include/bave/graphics/topology.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

namespace bave {
/// \brief Primitive topology.
enum class Topology : int { eTriangleStrip, eLineStrip };
} // namespace bave
4 changes: 3 additions & 1 deletion lib/src/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ void Drawable::Primitive::write(Geometry const& geometry) {

verts = static_cast<std::uint32_t>(geometry.vertices.size());
indices = static_cast<std::uint32_t>(geometry.indices.size());
topology = geometry.topology;
}

void Drawable::Primitive::clear() {
bytes.clear();
ibo_offset = 0;
verts = indices = 0;
topology = Topology::eTriangleStrip;
}

Drawable::Primitive::operator RenderPrimitive() const {
return RenderPrimitive{.bytes = bytes, .ibo_offset = ibo_offset, .vertices = verts, .indices = indices};
return RenderPrimitive{.bytes = bytes, .ibo_offset = ibo_offset, .vertices = verts, .indices = indices, .topology = topology};
}

void Drawable::draw(Shader& shader) const {
Expand Down
1 change: 1 addition & 0 deletions lib/src/graphics/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct QuadWriter {
0, 1, 2, 3, 0,
};
out.append(vertices, indices);
out.topology = Topology::eLineStrip;
}

[[nodiscard]] static auto make_vertices(Quad const& quad) -> std::array<Vertex, 4> {
Expand Down
15 changes: 8 additions & 7 deletions lib/src/graphics/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ auto make_buffer_bindings(detail::BufferCache& scratch_buffer_cache, Ptr<detail:
BufferBinding{.resource = custom_ssbo, .binding = 1},
};
}

[[nodiscard]] constexpr auto to_topology(Topology const in) {
switch (in) {
case Topology::eLineStrip: return vk::PrimitiveTopology::eLineStrip;
default: return vk::PrimitiveTopology::eTriangleStrip;
}
}
} // namespace

Shader::Shader(NotNull<Renderer const*> renderer, vk::ShaderModule vertex, vk::ShaderModule fragment) : m_renderer(renderer), m_vert(vertex), m_frag(fragment) {
set_viewport();
}

void Shader::set_line_strip(float const line_width) {
this->line_width = line_width;
topology = vk::PrimitiveTopology::eLineStrip;
}

void Shader::set_tri_strip() { topology = vk::PrimitiveTopology::eTriangleStrip; }

auto Shader::update_texture(CombinedImageSampler const cis, std::uint32_t binding) -> bool {
if (binding >= m_sets.cis.size()) { return false; }

Expand Down Expand Up @@ -164,6 +164,7 @@ void Shader::draw(RenderPrimitive const& primitive, std::span<RenderInstance::Ba
if (!command_buffer || primitive.bytes.empty() || instances.empty()) { return; }

auto& pipeline_cache = m_renderer->get_pipeline_cache();
auto const topology = to_topology(primitive.topology);
auto const pipeline_state = detail::PipelineCache::State{.line_width = line_width, .topology = topology, .polygon_mode = polygon_mode};
auto pipeline = pipeline_cache.load_pipeline({.vertex = m_vert, .fragment = m_frag}, pipeline_state);
if (!pipeline) { return; }
Expand Down
7 changes: 4 additions & 3 deletions tools/src/tools/animator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void Animator::tick() {

auto outline = LineRect{};
outline.size = glm::vec2{tile->get_size()} * size_ratio;
m_rect.set_geometry(Geometry::from(outline));
m_rect.set_shape(outline);

auto const top_left = (glm::vec2{glm::ivec2{tile->image_rect.lt.x, -tile->image_rect.lt.y} + glm::ivec2{-tex_size.x / 2, tex_size.y / 2}}) * size_ratio;
auto const centre = top_left + 0.5f * glm::vec2{outline.size.x, -outline.size.y};
Expand Down Expand Up @@ -60,7 +60,6 @@ void Animator::render(Shader& shader) const {

render_view.transform = m_top_view;
render_view.n_scissor = top_scissor;
shader.set_line_strip(2.0f);
m_rect.draw(shader);
}

Expand Down Expand Up @@ -256,7 +255,9 @@ void Animator::new_timeline() {
m_sprite.set_timeline(m_timeline);
m_sprite.set_uv(uv_rect_v);

m_rect.set_geometry({});
auto rect = LineRect{};
rect.size = {};
m_rect.set_shape(rect);
m_unsaved = false;

state->animator.last_timeline.clear();
Expand Down
2 changes: 1 addition & 1 deletion tools/src/tools/animator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Animator : public Applet {
Logger m_log{"Animator"};
Loader m_loader;

CustomShape m_rect{};
LineRectShape m_rect{};
QuadShape m_separator{};
QuadShape m_image_quad{};
SpriteAnim m_sprite{};
Expand Down
20 changes: 9 additions & 11 deletions tools/src/tools/tiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
namespace bave::tools {
namespace fs = std::filesystem;

// TODO: colliders
Tiler::Tiler(App& app, NotNull<std::shared_ptr<State>> const& state)
: Applet(app, state), m_loader(&get_app().get_data_store(), &get_app().get_render_device()) {
load_previous();

Shader::default_line_width = 3.0f;
}

void Tiler::tick() {
Expand All @@ -32,35 +33,32 @@ void Tiler::tick() {
void Tiler::render(Shader& shader) const {
m_sprite.draw(shader);

shader.set_line_strip(m_outline_width);

glm::ivec2 const size = m_sprite.get_size();
auto const origin_offset = 0.5f * glm::vec2{-size.x, size.y};

auto tile_rect = CustomShape{};
tile_rect.tint = m_tile_rgba;
auto collider_rect = CustomShape{};
collider_rect.tint = m_collider_rgba;
auto outline = LineRectShape{};
outline.tint = m_tile_rgba;

for (auto const& block : m_blocks) {
auto local_origin = 0.5f * glm::vec2{block.tile.image_rect.lt + block.tile.image_rect.rb};
local_origin.y = -local_origin.y;
auto rect = LineRect{};
rect.origin = origin_offset + local_origin;
rect.size = glm::vec2{block.tile.image_rect.rb - block.tile.image_rect.lt};
tile_rect.set_geometry(rect.to_geometry());
tile_rect.draw(shader);
outline.set_shape(rect);
outline.draw(shader);
}

outline.tint = m_collider_rgba;
for (auto const& block : m_blocks) {
for (auto const& collider : block.tile.colliders) {
auto local_origin = 0.5f * glm::vec2{collider.lt + collider.rb};
local_origin.y = -local_origin.y;
auto rect = LineRect{};
rect.origin = origin_offset + local_origin;
rect.size = glm::vec2{collider.rb - collider.lt};
collider_rect.set_geometry(rect.to_geometry());
collider_rect.draw(shader);
outline.set_shape(rect);
outline.draw(shader);
}
}
}
Expand Down

0 comments on commit f2348fe

Please sign in to comment.