Unravel Engine C++ Reference
Loading...
Searching...
No Matches
blit_pass.cpp
Go to the documentation of this file.
1// blit_pass.cpp
2#include "blit_pass.h"
5#include <graphics/texture.h>
6
7namespace unravel
8{
9
11{
12 // 1) Retrieve the asset manager (holds shader assets)
13 auto& am = ctx.get_cached<asset_manager>();
14
15 // 2) Load a fullscreen‐quad vertex shader and a simple blit fragment shader
16 // vs_clip_quad.sc is assumed to output a full‐screen triangle/quad.
17 // fs_blit.sc should sample a 2D texture “s_input” and output it unchanged.
18 auto vs_fullscreen = am.get_asset<gfx::shader>("engine:/data/shaders/vs_clip_quad.sc");
19 auto fs_blit = am.get_asset<gfx::shader>("engine:/data/shaders/fs_blit.sc");
20
21 // 3) Create our GPU program
22 blit_program_.program = std::make_unique<gpu_program>(vs_fullscreen, fs_blit);
23 blit_program_.cache_uniforms();
24
25 return true;
26}
27
28auto blit_pass::create_or_update_output_fb(gfx::render_view& rview,
30 const gfx::frame_buffer::ptr& output)
32{
33 if(output)
34 {
35 return output;
36 }
37 auto input_sz = input->get_size();
38 auto input_tex = input->get_texture();
39 auto input_format = input_tex->info.format;
40 auto& output_tex = rview.tex_get_or_emplace("BLIT_OUTPUT");
41 if(gfx::needs_recreate(output_tex, input_sz, input_format))
42 {
43 output_tex.reset();
44 output_tex = std::make_shared<gfx::texture>(input_sz.width,
45 input_sz.height,
46 false,
47 1,
48 input_format,
49 BGFX_TEXTURE_RT);
50 }
51 auto& output_fbo = rview.fbo_get_or_emplace("BLIT_OUTPUT");
52 if(gfx::needs_recreate(output_fbo, input_sz))
53 {
54 output_fbo.reset();
55 output_fbo = std::make_shared<gfx::frame_buffer>();
56 output_fbo->populate({output_tex});
57 }
58 return output_fbo;
59}
60
62{
63 const auto& input_fb = params.input;
64 if(!input_fb)
65 {
66 return nullptr;
67 }
68 auto actual_output = create_or_update_output_fb(rview, input_fb, params.output);
69
70 // 3) Begin a named render pass for clarity/debug (optional)
71 gfx::render_pass pass("Blit/Pass");
72 pass.bind(actual_output.get());
73
74 // 4) Bind our GPU program and set the source texture
75 blit_program_.program->begin();
76 gfx::set_texture(blit_program_.s_input, 0, input_fb->get_texture());
77
78 // 5) Draw a fullscreen quad. The helper `gfx::clip_quad()` returns
79 // the appropriate “topology” for a single‐triangle fullscreen quad,
80 // taking care of originBottomLeft if needed.
81 // (We pass 1.0f as the depth—meaning no depth test is used.)
82 auto topology = gfx::clip_quad(1.0f);
83
84 // 6) Configure render state: write RGB + A, no depth, no blending.
85 gfx::set_state(topology | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
86
87 // 7) Submit to the current view (render_pass::bind will have set the view ID)
88 gfx::submit(pass.id, blit_program_.program->native_handle());
89
90 // 8) Reset to default state (optional but good practice)
91 gfx::set_state(BGFX_STATE_DEFAULT);
92
93 blit_program_.program->end();
94
95 // 9) Unbind/discard any transient state (optional).
97
98 return actual_output;
99}
100
101} // namespace unravel
Manages assets, including loading, unloading, and storage.
auto get_asset(const std::string &key, load_flags flags=load_flags::standard, load_mode mode=load_mode::immediate) -> asset_handle< T >
Gets an asset by its key.
auto init(rtti::context &ctx) -> bool
Must be called once (after bgfx::init() and after asset_manager is registered in context).
Definition blit_pass.cpp:10
auto run(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
Executes the blit: copies params.input → params.output. Returns the actual output framebuffer.
Definition blit_pass.cpp:61
void submit(view_id _id, program_handle _handle, int32_t _depth, bool _preserveState)
void set_state(uint64_t _state, uint32_t _rgba)
Definition graphics.cpp:937
auto needs_recreate(const gfx::frame_buffer::ptr &fbo, const usize32_t &size) -> bool
void discard(uint8_t _flags)
auto clip_quad(float depth, float width, float height) -> uint64_t
void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
gfx::view_id id
void bind(const frame_buffer *fb=nullptr) const