Skip to content

Commit

Permalink
Refactor Mesh, Drawable.
Browse files Browse the repository at this point in the history
- Remove `VertexBufferCache`, allocate scratch on each draw.
- Remove `RenderDevice` from `Drawable` constructors.
  • Loading branch information
karnkaul committed Jan 18, 2024
1 parent 6d8f973 commit ea39a22
Show file tree
Hide file tree
Showing 28 changed files with 133 additions and 222 deletions.
3 changes: 1 addition & 2 deletions example/flappy/src/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ using bave::random_in_range;
using bave::Seconds;
using bave::Shader;

Background::Background(NotNull<bave::RenderDevice*> render_device, NotNull<Config const*> config)
: quad(render_device), cloud(render_device), m_config(config), m_top(config->background_rgba_top), m_bottom(config->background_rgba_bottom) {
Background::Background(NotNull<Config const*> config) : m_config(config), m_top(config->background_rgba_top), m_bottom(config->background_rgba_bottom) {
create_quad();
create_clouds();
}
Expand Down
2 changes: 1 addition & 1 deletion example/flappy/src/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Background {
public:
explicit Background(bave::NotNull<bave::RenderDevice*> render_device, bave::NotNull<Config const*> config);
explicit Background(bave::NotNull<Config const*> config);

void tick(bave::Seconds dt);
void draw(bave::Shader& shader) const;
Expand Down
12 changes: 4 additions & 8 deletions example/flappy/src/flappy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ using bave::Texture;

// bave will reset delta time after using game factory, so time spent in this constructor will not bloat up the first tick's dt.
// it will still halt the app until complete though; taking too long might trigger an ANR (App Not Responding) on Android.
Flappy::Flappy(App& app)
: Driver(app), m_game_view(app.get_render_device().render_view), m_score_bg(&app.get_render_device()), m_score_text(&app.get_render_device()),
m_game_over_text(&app.get_render_device()), m_restart_text(&app.get_render_device()) {
Flappy::Flappy(App& app) : Driver(app), m_game_view(app.get_render_device().render_view) {
// we use a custom / fixed viewport so that the same game world is visible regardless of screen / framebuffer size.
setup_viewport();
// this example loads most assets on the main thread, but all of them can be loaded asynchronously if desired.
Expand Down Expand Up @@ -194,19 +192,17 @@ void Flappy::load_assets() {
}

void Flappy::create_entities() {
auto& render_device = get_app().get_render_device();

// explode animation.
m_explode = SpriteAnim{&render_device, m_config.explode_atlas};
m_explode = SpriteAnim{m_config.explode_atlas};
if (m_config.explode_animation) { m_explode->animation = *m_config.explode_animation; }
m_explode->repeat = false;

// player sprite.
m_player = Player{&get_app(), &m_config};
// background (sky gradient, scrolling clouds).
m_background = Background{&render_device, &m_config};
m_background = Background{&m_config};
// pipes.
m_pipes = Pipes{&render_device, &m_config};
m_pipes = Pipes{&m_config};
}

void Flappy::setup_hud() {
Expand Down
8 changes: 2 additions & 6 deletions example/flappy/src/pipes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ void Pipes::Pipe::translate(float const distance) {
bottom.transform.position.x += distance;
}

Pipes::Pipes(NotNull<bave::RenderDevice*> render_device, NotNull<Config const*> config)
: m_render_device(render_device), m_config(config), m_next_spawn(m_config->pipe_period) {}
Pipes::Pipes(NotNull<Config const*> config) : m_config(config), m_next_spawn(m_config->pipe_period) {}

auto Pipes::tick(Seconds dt) -> bool {
m_next_spawn -= dt;
Expand Down Expand Up @@ -58,10 +57,7 @@ void Pipes::restart() {
}

auto Pipes::make_pipe() const -> Pipe {
auto ret = Pipe{
.top = Sprite9Slice{m_render_device},
.bottom = Sprite9Slice{m_render_device},
};
auto ret = Pipe{};
ret.top.transform.scale.y = -1.0f;
ret.top.set_texture_9slice(m_config->pipe_texture);
ret.bottom.set_texture_9slice(m_config->pipe_texture);
Expand Down
7 changes: 3 additions & 4 deletions example/flappy/src/pipes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Pipes {
public:
explicit Pipes(bave::NotNull<bave::RenderDevice*> render_device, bave::NotNull<Config const*> config);
explicit Pipes(bave::NotNull<Config const*> config);

auto tick(bave::Seconds dt) -> bool;
void draw(bave::Shader& shader) const;
Expand All @@ -16,8 +16,8 @@ class Pipes {

private:
struct Pipe {
bave::Sprite9Slice top;
bave::Sprite9Slice bottom;
bave::Sprite9Slice top{};
bave::Sprite9Slice bottom{};
bool active{};

void translate(float distance);
Expand All @@ -27,7 +27,6 @@ class Pipes {
auto get_next_pipe() -> Pipe&;
void spawn_pipe();

bave::NotNull<bave::RenderDevice*> m_render_device;
bave::NotNull<Config const*> m_config;
std::vector<Pipe> m_pipes{};
bave::Seconds m_next_spawn{};
Expand Down
2 changes: 1 addition & 1 deletion example/flappy/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using bave::NotNull;
using bave::Seconds;
using bave::Shader;

Player::Player(NotNull<App const*> app, NotNull<Config const*> config) : sprite(&app->get_render_device()), m_app(app), m_config(config) {
Player::Player(NotNull<App const*> app, NotNull<Config const*> config) : m_app(app), m_config(config) {
sprite.set_texture(config->player_texture);
sprite.set_size(config->player_size);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#pragma once
#include <bave/graphics/detail/buffering.hpp>
#include <bave/graphics/detail/render_resource.hpp>
#include <bave/logger.hpp>
#include <vulkan/vulkan_hash.hpp>
#include <unordered_map>
#include <vector>

namespace bave::detail {
class ScratchBufferCache {
class BufferCache {
public:
explicit ScratchBufferCache(NotNull<RenderDevice*> render_device) : m_render_device(render_device) {}
explicit BufferCache(NotNull<RenderDevice*> render_device) : m_render_device(render_device) {}

auto allocate(vk::BufferUsageFlags usage) -> RenderBuffer&;
auto get_empty(vk::BufferUsageFlags usage) -> RenderBuffer const&;
auto or_empty(Ptr<RenderBuffer const> buffer, vk::BufferUsageFlags usage) -> RenderBuffer const& { return buffer == nullptr ? get_empty(usage) : *buffer; }

auto next_frame() -> void;
auto clear() -> void;
Expand All @@ -24,6 +26,7 @@ class ScratchBufferCache {

using Map = std::unordered_map<vk::BufferUsageFlags, Pool>;

Logger m_log{"BufferCache"};
NotNull<RenderDevice*> m_render_device;
std::unordered_map<vk::BufferUsageFlags, RenderBuffer> m_empty_buffers{};
Buffered<Map> m_maps{};
Expand Down
27 changes: 0 additions & 27 deletions lib/include/bave/graphics/detail/vertex_buffer_cache.hpp

This file was deleted.

2 changes: 0 additions & 2 deletions lib/include/bave/graphics/drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
namespace bave {
class Drawable : public Polymorphic {
public:
explicit Drawable(NotNull<RenderDevice*> render_device);

void draw(Shader& shader) const;

[[nodiscard]] auto get_bounds() const -> Rect<>;
Expand Down
29 changes: 8 additions & 21 deletions lib/include/bave/graphics/mesh.hpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
#pragma once
#include <bave/graphics/geometry.hpp>
#include <bave/graphics/render_device.hpp>
#include <memory>

namespace bave {
class Mesh {
public:
Mesh(Mesh const&) = delete;
auto operator=(Mesh const&) -> Mesh& = delete;

Mesh(Mesh&&) = default;
auto operator=(Mesh&&) -> Mesh& = default;

explicit Mesh(NotNull<RenderDevice*> render_device);

~Mesh();

[[nodiscard]] auto get_render_device() const -> RenderDevice const& { return *m_render_device; }
struct Data {
std::span<std::byte const> bytes{};
std::size_t ibo_offset{};
};

[[nodiscard]] auto is_empty() const -> bool { return m_bytes.empty(); }
[[nodiscard]] auto get_vertex_count() const -> std::uint32_t { return m_verts; }
[[nodiscard]] auto get_index_count() const -> std::uint32_t { return m_indices; }

void write(Geometry const& geometry);

private:
[[nodiscard]] auto get_buffer() const -> Ptr<detail::RenderBuffer>;
void draw(vk::CommandBuffer command_buffer, std::uint32_t instance_count = 1) const;
[[nodiscard]] auto get_data() const -> Data { return {.bytes = m_bytes, .ibo_offset = m_ibo_offset}; }

NotNull<RenderDevice*> m_render_device;
std::shared_ptr<detail::VertexBuffer> m_vbo{};
std::vector<std::byte> m_data{};
private:
std::vector<std::byte> m_bytes{};
std::uint32_t m_verts{};
std::uint32_t m_indices{};
std::size_t m_ibo_offset{};

friend class Shader;
};
} // namespace bave
9 changes: 3 additions & 6 deletions lib/include/bave/graphics/render_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
#include <bave/core/ptr.hpp>
#include <bave/core/scoped_resource.hpp>
#include <bave/font/detail/font_library.hpp>
#include <bave/graphics/detail/buffer_cache.hpp>
#include <bave/graphics/detail/buffering.hpp>
#include <bave/graphics/detail/defer.hpp>
#include <bave/graphics/detail/device_blocker.hpp>
#include <bave/graphics/detail/image_cache.hpp>
#include <bave/graphics/detail/sampler_cache.hpp>
#include <bave/graphics/detail/scratch_buffer_cache.hpp>
#include <bave/graphics/detail/swapchain.hpp>
#include <bave/graphics/detail/vertex_buffer_cache.hpp>
#include <bave/graphics/detail/wsi.hpp>
#include <bave/graphics/extent_scaler.hpp>
#include <bave/graphics/render_view.hpp>
Expand Down Expand Up @@ -66,8 +65,7 @@ class RenderDevice {
auto recreate_surface() -> bool;

[[nodiscard]] auto get_defer_queue() -> detail::DeferQueue& { return m_defer_queue; }
[[nodiscard]] auto get_vertex_buffer_cache() const -> detail::VertexBufferCache& { return *m_vbo_cache; }
[[nodiscard]] auto get_scratch_buffer_cache() const -> detail::ScratchBufferCache& { return *m_sb_cache; }
[[nodiscard]] auto get_buffer_cache() const -> detail::BufferCache& { return *m_buffer_cache; }
[[nodiscard]] auto get_image_cache() const -> detail::ImageCache& { return *m_image_cache; }
[[nodiscard]] auto get_sampler_cache() const -> detail::SamplerCache& { return *m_sampler_cache; }
[[nodiscard]] auto get_font_library() const -> detail::FontLibrary& { return *m_font_library; }
Expand Down Expand Up @@ -97,8 +95,7 @@ class RenderDevice {
Gpu m_gpu{};
vk::Queue m_queue{};
detail::Swapchain m_swapchain{};
std::unique_ptr<detail::VertexBufferCache> m_vbo_cache{};
std::unique_ptr<detail::ScratchBufferCache> m_sb_cache{};
std::unique_ptr<detail::BufferCache> m_buffer_cache{};
std::unique_ptr<detail::ImageCache> m_image_cache{};
std::unique_ptr<detail::SamplerCache> m_sampler_cache{};
std::unique_ptr<detail::FontLibrary> m_font_library{detail::FontLibrary::make()};
Expand Down
2 changes: 1 addition & 1 deletion lib/include/bave/graphics/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Shader {
Ptr<detail::RenderBuffer> ssbo{};
};

[[nodiscard]] auto allocate_scratch(vk::BufferUsageFlagBits usage) const -> detail::RenderBuffer&;
[[nodiscard]] auto allocate_scratch(vk::BufferUsageFlags usage) const -> detail::RenderBuffer&;

void set_viewport();
[[nodiscard]] auto get_scissor(Rect<> n_rect) const -> vk::Rect2D;
Expand Down
6 changes: 0 additions & 6 deletions lib/include/bave/graphics/shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@
namespace bave {
class BasicShape : public Drawable {
public:
using Drawable::Drawable;

void set_texture(std::shared_ptr<Texture const> texture) { Drawable::set_texture(std::move(texture)); }
[[nodiscard]] auto get_texture() const -> std::shared_ptr<Texture const> const& { return textures.front(); }
};

class CustomShape : public BasicShape {
public:
using BasicShape::BasicShape;

void set_geometry(Geometry geometry) { Drawable::set_geometry(std::move(geometry)); }
};

template <typename ShapeT>
class Shape : public BasicShape {
public:
explicit Shape(NotNull<RenderDevice*> render_device, ShapeT const& shape = ShapeT{}) : BasicShape(render_device) { set_shape(shape); }

void set_shape(ShapeT const& shape) {
m_shape = shape;
set_geometry(m_shape.to_geometry());
Expand Down
4 changes: 0 additions & 4 deletions lib/include/bave/graphics/sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
namespace bave {
class Sprite : public QuadShape {
public:
explicit Sprite(NotNull<RenderDevice*> render_device) : QuadShape(render_device) {}

void set_size(glm::vec2 size);
void set_auto_size(glm::vec2 max_size);

Expand All @@ -25,8 +23,6 @@ class Sprite : public QuadShape {

class Sprite9Slice : public NineQuadShape {
public:
explicit Sprite9Slice(NotNull<RenderDevice*> render_device) : NineQuadShape(render_device) {}

void set_texture_9slice(std::shared_ptr<Texture9Slice const> texture);
void set_size(glm::vec2 size);

Expand Down
2 changes: 1 addition & 1 deletion lib/include/bave/graphics/sprite_anim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SpriteAnim : public Sprite {
[[nodiscard]] auto get_tile_at(Seconds timestamp) const -> std::string_view;
};

explicit SpriteAnim(NotNull<RenderDevice*> render_device, std::shared_ptr<TextureAtlas> atlas = {}, Seconds duration = 1s);
explicit SpriteAnim(std::shared_ptr<TextureAtlas> atlas = {}, Seconds duration = 1s);

void tick(Seconds dt);

Expand Down
2 changes: 1 addition & 1 deletion lib/include/bave/graphics/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Text : public Drawable {

using Height = TextHeight;

explicit Text(NotNull<RenderDevice*> render_device, std::shared_ptr<Font> font = {});
explicit Text(std::shared_ptr<Font> font = {});

auto set_font(std::shared_ptr<Font> font) -> Text&;
auto set_string(std::string text) -> Text&;
Expand Down
2 changes: 0 additions & 2 deletions lib/src/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ namespace {
}
} // namespace

Drawable::Drawable(NotNull<RenderDevice*> render_device) : m_mesh(render_device) {}

void Drawable::draw(Shader& shader) const {
bake_instances();
update_textures(shader);
Expand Down
Loading

0 comments on commit ea39a22

Please sign in to comment.