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(const gfx::frame_buffer::ptr& input, const gfx::frame_buffer::ptr& output)
30{
31 // If the caller provided an output framebuffer, just return it.
32 if(output)
33 {
34 return output;
35 }
36
37 // Otherwise, we need to create or recreate our own internal FB to match `input`.
38 auto input_sz = input->get_size();
39 auto input_tex = input->get_texture(); // returns a gfx::texture::ptr
40 auto input_format = input_tex->info.format; // gfx::texture_format
41
42 bool needs_recreate = false;
43 if(!output_)
44 {
45 needs_recreate = true;
46 }
47 else
48 {
49 auto out_sz = output_->get_size();
50 auto out_format = output_->get_texture()->info.format;
51 if(out_sz != input_sz || out_format != input_format)
52 {
53 needs_recreate = true;
54 }
55 }
56
57 if(!needs_recreate)
58 {
59 return output_;
60 }
61
62 // Destroy old output_ if it existed:
63 if(output_)
64 {
65 output_.reset();
66 }
67
68 // Create a new texture with same size/format as input
69 // Note: we pass BGFX_TEXTURE_RT to mark it as a render target.
70 auto output_tex = std::make_shared<gfx::texture>(input_sz.width,
71 input_sz.height,
72 false, // no generate mips
73 1, // one layer
74 input_format, // same format as input
75 BGFX_TEXTURE_RT // render‐target flag
76 );
77
78 // Create a new framebuffer wrapping that texture.
79 output_ = std::make_shared<gfx::frame_buffer>();
80 output_->populate({output_tex});
81
82 return output_;
83}
84
86{
87 // 1) Ensure we have a valid input FB
88 const auto& input_fb = params.input;
89 if(!input_fb)
90 {
91 return nullptr;
92 }
93
94 // 2) Either use the provided output FB, or create/match one internally
95 auto actual_output = create_or_update_output_fb(input_fb, params.output);
96
97 // 3) Begin a named render pass for clarity/debug (optional)
98 gfx::render_pass pass("blit_pass");
99 pass.bind(actual_output.get());
100
101 // 4) Bind our GPU program and set the source texture
102 blit_program_.program->begin();
103 gfx::set_texture(blit_program_.s_input, 0, input_fb->get_texture());
104
105 // 5) Draw a fullscreen quad. The helper `gfx::clip_quad()` returns
106 // the appropriate “topology” for a single‐triangle fullscreen quad,
107 // taking care of originBottomLeft if needed.
108 // (We pass 1.0f as the depth—meaning no depth test is used.)
109 auto topology = gfx::clip_quad(1.0f);
110
111 // 6) Configure render state: write RGB + A, no depth, no blending.
112 gfx::set_state(topology | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
113
114 // 7) Submit to the current view (render_pass::bind will have set the view ID)
115 gfx::submit(pass.id, blit_program_.program->native_handle());
116
117 // 8) Reset to default state (optional but good practice)
118 gfx::set_state(BGFX_STATE_DEFAULT);
119
120 blit_program_.program->end();
121
122 // 9) Unbind/discard any transient state (optional).
123 gfx::discard();
124
125 return actual_output;
126}
127
128} // namespace unravel
Manages assets, including loading, unloading, and storage.
auto get_asset(const std::string &key, load_flags flags=load_flags::standard) -> 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(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:85
void submit(view_id _id, program_handle _handle, int32_t _depth, bool _preserveState)
Definition graphics.cpp:899
void set_state(uint64_t _state, uint32_t _rgba)
Definition graphics.cpp:763
void discard(uint8_t _flags)
Definition graphics.cpp:968
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)
Definition graphics.cpp:889
gfx::view_id id
Definition render_pass.h:98
void bind(const frame_buffer *fb=nullptr) const