Skip to content

Commit

Permalink
Add callbacks to override render state for flat renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Aug 26, 2024
1 parent c9735e0 commit 93fa4b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
22 changes: 20 additions & 2 deletions renderer/flat_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ void FlatRenderer::begin()
queue.set_shader_suites(suite);
}

void FlatRenderer::set_opaque_state_callback(std::function<void (Vulkan::CommandBuffer &)> cb)
{
opaque_state_cb = std::move(cb);
}

void FlatRenderer::set_transparent_state_callback(std::function<void (Vulkan::CommandBuffer &)> cb)
{
transparent_state_cb = std::move(cb);
}

void FlatRenderer::flush(Vulkan::CommandBuffer &cmd, const vec3 &camera_pos, const vec3 &camera_size)
{
struct GlobalData
Expand All @@ -108,13 +118,21 @@ void FlatRenderer::flush(Vulkan::CommandBuffer &cmd, const vec3 &camera_pos, con

queue.sort();

cmd.set_opaque_sprite_state();
if (opaque_state_cb)
opaque_state_cb(cmd);
else
cmd.set_opaque_sprite_state();

CommandBufferSavedState state = {};
cmd.save_state(COMMAND_BUFFER_SAVED_SCISSOR_BIT | COMMAND_BUFFER_SAVED_VIEWPORT_BIT | COMMAND_BUFFER_SAVED_RENDER_STATE_BIT, state);
queue.dispatch(Queue::Opaque, cmd, &state);
queue.dispatch(Queue::OpaqueEmissive, cmd, &state);

cmd.set_transparent_sprite_state();
if (transparent_state_cb)
transparent_state_cb(cmd);
else
cmd.set_transparent_sprite_state();

cmd.save_state(COMMAND_BUFFER_SAVED_SCISSOR_BIT | COMMAND_BUFFER_SAVED_VIEWPORT_BIT | COMMAND_BUFFER_SAVED_RENDER_STATE_BIT, state);
queue.dispatch(Queue::Transparent, cmd, &state);
}
Expand Down
7 changes: 7 additions & 0 deletions renderer/flat_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "renderer.hpp"
#include "sprite.hpp"
#include <vector>
#include <functional>

namespace Granite
{
Expand Down Expand Up @@ -99,6 +100,9 @@ class FlatRenderer : public EventHandler
void push_scissor(const vec2 &offset, const vec2 &size);
void pop_scissor();

void set_opaque_state_callback(std::function<void (Vulkan::CommandBuffer &)> cb);
void set_transparent_state_callback(std::function<void (Vulkan::CommandBuffer &)> cb);

Vulkan::Device &get_device();

private:
Expand All @@ -109,6 +113,9 @@ class FlatRenderer : public EventHandler
RenderQueue queue;
ShaderSuite suite[Util::ecast(RenderableType::Count)];

std::function<void (Vulkan::CommandBuffer &)> opaque_state_cb;
std::function<void (Vulkan::CommandBuffer &)> transparent_state_cb;

struct Scissor
{
vec2 offset;
Expand Down
5 changes: 5 additions & 0 deletions ui/ui_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class UIManager final : public EventHandler, public UIManagerInterface
void reset_children();
void remove_child(Widget *widget);

inline FlatRenderer &get_flat_renderer()
{
return renderer;
}

private:
FlatRenderer renderer;
std::vector<WidgetHandle> widgets;
Expand Down

0 comments on commit 93fa4b6

Please sign in to comment.