Skip to content

Commit

Permalink
Refactor bounding rect, vertex buffer flow. (#13)
Browse files Browse the repository at this point in the history
* Refactor `get_bounds()`.

- Store `Geometry` in `Drawable`, use that + transform to find bounding rect of all vertices.

* Refactor `Mesh`, `Drawable`.

- Remove `VertexBufferCache`, allocate scratch on each draw.
- Remove `RenderDevice` from `Drawable` constructors.

* Remove `Mesh`.

* Add `BufferType` as wrapper for buffer usage flags.
  • Loading branch information
karnkaul authored Jan 20, 2024
1 parent 4712c3b commit 5004029
Show file tree
Hide file tree
Showing 40 changed files with 269 additions and 326 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
6 changes: 3 additions & 3 deletions example/flappy/src/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

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;

bave::CustomShape quad;
bave::Sprite cloud;
bave::CustomShape quad{};
bave::Sprite cloud{};

private:
struct CloudInstance {
Expand Down
16 changes: 6 additions & 10 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 All @@ -49,7 +47,7 @@ void Flappy::tick() {
if (m_pipes->tick(dt)) { ++m_score; }

// check for player collision with pipes.
auto const hitbox = Rect<>::from_extent(m_config.player_hitbox, m_player->sprite.transform.position);
auto const hitbox = Rect<>::from_size(m_config.player_hitbox, m_player->sprite.transform.position);
if (m_pipes->is_colliding(hitbox)) { game_over(); }

// also check for player crossing the top/bottom edges of the game world / screen.
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 All @@ -226,7 +222,7 @@ void Flappy::setup_hud() {
m_score_text.set_height(m_config.score_text_height);

auto const score_bounds = m_score_text.get_bounds();
m_score_bg.set_shape(bave::Quad{.size = {m_config.world_space.x, 1.5f * score_bounds.extent().y}});
m_score_bg.set_shape(bave::Quad{.size = {m_config.world_space.x, 1.5f * score_bounds.size().y}});
m_score_bg.tint = m_config.score_bg_rgba;
m_score_bg.transform.position = score_bounds.centre();

Expand Down
8 changes: 4 additions & 4 deletions example/flappy/src/flappy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class Flappy : public bave::Driver {
std::optional<Pipes> m_pipes{};
std::optional<bave::SpriteAnim> m_explode{};

bave::QuadShape m_score_bg;
bave::Text m_score_text;
bave::Text m_game_over_text;
bave::Text m_restart_text;
bave::QuadShape m_score_bg{};
bave::Text m_score_text{};
bave::Text m_game_over_text{};
bave::Text m_restart_text{};

int m_score{};
bool m_paused{};
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
2 changes: 1 addition & 1 deletion example/flappy/src/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Player {
void stop_jump();
void restart();

bave::Sprite sprite;
bave::Sprite sprite{};

private:
std::optional<bave::Seconds> m_jump_elapsed{};
Expand Down
40 changes: 40 additions & 0 deletions lib/include/bave/graphics/detail/buffer_cache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#include <bave/graphics/detail/buffer_type.hpp>
#include <bave/graphics/detail/buffering.hpp>
#include <bave/graphics/detail/render_resource.hpp>
#include <bave/logger.hpp>
#include <array>
#include <vector>

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

auto allocate(BufferType type) -> RenderBuffer&;

[[nodiscard]] auto get_empty(BufferType type) const -> RenderBuffer const& { return m_empty_buffers.at(static_cast<std::size_t>(type)); }

[[nodiscard]] auto or_empty(Ptr<RenderBuffer const> buffer, BufferType type) const -> RenderBuffer const& {
return buffer == nullptr ? get_empty(type) : *buffer;
}

auto next_frame() -> void;
auto clear() -> void;

private:
struct Pool {
std::vector<RenderBuffer> buffers{};
std::size_t next{};
};

static constexpr auto types_count_v = static_cast<std::size_t>(BufferType::eCOUNT_);

using Map = std::array<Pool, types_count_v>;

Logger m_log{"BufferCache"};
NotNull<RenderDevice*> m_render_device;
std::array<RenderBuffer, types_count_v> m_empty_buffers;
Buffered<Map> m_maps{};
};
} // namespace bave::detail
5 changes: 5 additions & 0 deletions lib/include/bave/graphics/detail/buffer_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace bave::detail {
enum class BufferType : int { eVertexIndex, eUniform, eStorage, eCOUNT_ };
}
31 changes: 0 additions & 31 deletions lib/include/bave/graphics/detail/scratch_buffer_cache.hpp

This file was deleted.

27 changes: 0 additions & 27 deletions lib/include/bave/graphics/detail/vertex_buffer_cache.hpp

This file was deleted.

24 changes: 20 additions & 4 deletions lib/include/bave/graphics/drawable.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <bave/core/polymorphic.hpp>
#include <bave/graphics/geometry.hpp>
#include <bave/graphics/shader.hpp>
#include <bave/graphics/texture.hpp>
#include <memory>
Expand All @@ -8,25 +9,40 @@
namespace bave {
class Drawable : public Polymorphic {
public:
explicit Drawable(NotNull<RenderDevice*> render_device);

void draw(Shader& shader) const;

[[nodiscard]] auto get_bounds() const -> Rect<>;
[[nodiscard]] auto get_geometry() const -> Geometry const& { return m_geometry; }

Transform transform{};
Rgba tint{};
std::array<std::shared_ptr<Texture const>, SetLayout::max_textures_v> textures{};

std::vector<RenderInstance> instances{};

protected:
void set_geometry(Geometry const& geometry) { m_mesh.write(geometry); }
void set_geometry(Geometry geometry);
void set_texture(std::shared_ptr<Texture const> texture) { textures.front() = std::move(texture); }

private:
struct Primitive {
std::vector<std::byte> bytes{};
std::uint32_t verts{};
std::uint32_t indices{};
std::size_t ibo_offset{};

void write(Geometry const& geometry);
void clear();

[[nodiscard]] operator RenderPrimitive() const;
};

void bake_instances() const;
void update_textures(Shader& out_shader) const;

Mesh m_mesh;
Geometry m_geometry{};
Primitive m_primitive{};

mutable std::vector<RenderInstance::Baked> m_baked_instances{};
};
} // namespace bave
4 changes: 0 additions & 4 deletions lib/include/bave/graphics/geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct Quad {
Rgba rgba{white_v};
glm::vec2 origin{};

[[nodiscard]] constexpr auto get_bounds() const -> Rect<> { return Rect<>::from_extent(size, origin); }
[[nodiscard]] auto to_geometry() const -> Geometry { return Geometry::from(*this); }

auto operator==(Quad const&) const -> bool = default;
Expand All @@ -64,8 +63,6 @@ struct Circle {
Rgba rgba{white_v};
glm::vec2 origin{};

[[nodiscard]] constexpr auto get_bounds() const -> Rect<> { return Rect<>::from_extent(glm::vec2{diameter}, origin); }

[[nodiscard]] auto to_geometry() const -> Geometry { return Geometry::from(*this); }

auto operator==(Circle const&) const -> bool = default;
Expand Down Expand Up @@ -103,7 +100,6 @@ struct NineQuad {
Rgba rgba{white_v};
glm::vec2 origin{};

[[nodiscard]] constexpr auto get_bounds() const -> Rect<> { return Rect<>::from_extent(size.current, origin); }
[[nodiscard]] auto to_geometry() const -> Geometry { return Geometry::from(*this); }

auto operator==(NineQuad const&) const -> bool = default;
Expand Down
39 changes: 0 additions & 39 deletions lib/include/bave/graphics/mesh.hpp

This file was deleted.

Loading

0 comments on commit 5004029

Please sign in to comment.