Unravel Engine C++ Reference
Loading...
Searching...
No Matches
taa_pass.cpp
Go to the documentation of this file.
1#include "taa_pass.h"
2
5#include <graphics/graphics.h>
7#include <graphics/texture.h>
8
9#include <bgfx/bgfx.h>
10
11namespace unravel
12{
13namespace
14{
16constexpr std::uint32_t k_taa_sampler_flags = BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP;
17} // namespace
18
19auto taa_pass::init(rtti::context& ctx) -> bool
20{
21 auto& am = ctx.get_cached<asset_manager>();
22 auto vs = am.get_asset<gfx::shader>("engine:/data/shaders/vs_clip_quad.sc");
23 auto fs = am.get_asset<gfx::shader>("engine:/data/shaders/taa/fs_taa.sc");
24 program_.program = std::make_unique<gpu_program>(vs, fs);
25 program_.cache_uniforms();
26 return program_.program->is_valid();
27}
28
29auto taa_pass::create_or_update_history_tex(gfx::render_view& rview,
30 const gfx::frame_buffer::ptr& reference_color) -> gfx::texture::ptr
31{
32 const auto sz = reference_color->get_size();
33 const auto fmt = reference_color->get_texture(0)->info.format;
34 auto& history_tex = rview.tex_get_or_emplace("TAA_HISTORY");
35 if(gfx::needs_recreate(history_tex, sz, fmt))
36 {
37 history_tex.reset();
38 history_tex = std::make_shared<gfx::texture>(sz.width,
39 sz.height,
40 false,
41 1,
42 fmt,
43 BGFX_TEXTURE_RT | BGFX_TEXTURE_BLIT_DST);
44 }
45 return history_tex;
46}
47
48auto taa_pass::create_or_update_temp_fb(gfx::render_view& rview,
49 const gfx::frame_buffer::ptr& reference_color) -> gfx::frame_buffer::ptr
50{
51 const auto sz = reference_color->get_size();
52 const auto fmt = reference_color->get_texture(0)->info.format;
53 auto& out_tex = rview.tex_get_or_emplace("TAA_TEMP");
54 if(gfx::needs_recreate(out_tex, sz, fmt))
55 {
56 out_tex.reset();
57 out_tex = std::make_shared<gfx::texture>(sz.width, sz.height, false, 1, fmt, BGFX_TEXTURE_RT);
58 }
59 auto& fbo = rview.fbo_get_or_emplace("TAA_TEMP");
60 if(gfx::needs_recreate(fbo, sz))
61 {
62 fbo.reset();
63 fbo = std::make_shared<gfx::frame_buffer>();
64 fbo->populate({out_tex});
65 }
66 return fbo;
67}
68
70{
71 const auto& input = params.input;
72 if(!program_.program || !program_.program->is_valid() || !input || !params.cam || !params.g_buffer)
73 {
74 return input;
75 }
76
77 auto old_history = rview.tex_safe_get("TAA_HISTORY");
78 auto history_tex = create_or_update_history_tex(rview, input);
79 auto temp_fbo = create_or_update_temp_fb(rview, input);
80
81 if(history_tex != old_history)
82 {
83 gfx::render_pass init_pass("TAA/History Init");
84 gfx::blit(init_pass.id,
85 history_tex->native_handle(),
86 0,
87 0,
88 input->get_texture(0)->native_handle(),
89 0,
90 0);
91 return input;
92 }
93
94 APP_SCOPE_PERF("Rendering/TAA Pass");
95
96 gfx::render_pass pass("TAA/Resolve Pass");
97 pass.bind(temp_fbo.get());
98 pass.set_view_proj(params.cam->get_view(), params.cam->get_projection());
99
100 if(!program_.program->begin())
101 {
102 return input;
103 }
104
105 gfx::set_texture(program_.s_curr, 0, input->get_texture(0), k_taa_sampler_flags);
106 gfx::set_texture(program_.s_history, 1, history_tex, k_taa_sampler_flags);
107 gfx::set_texture(program_.s_depth, 2, params.g_buffer->get_texture(4), k_taa_sampler_flags);
108
109 const auto prev_vp = params.cam->get_taa_prev_view_projection();
110 gfx::set_uniform(program_.u_prev_view_proj, prev_vp.get_matrix());
111
112 const float taa_params[4] = {params.config.history_blend,
113 params.config.sharpen,
114 params.config.depth_reject_scale,
115 params.config.variance_clip_scale};
116 gfx::set_uniform(program_.u_taa_params, taa_params);
117
118 const auto topology = gfx::clip_quad(1.0f);
119 gfx::set_state(topology | BGFX_STATE_DEPTH_TEST_NEVER | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
120 gfx::submit(pass.id, program_.program->native_handle());
121
122 gfx::set_state(BGFX_STATE_DEFAULT);
123 program_.program->end();
124 gfx::discard();
125
126 gfx::render_pass hist_pass("TAA/History Blit");
127 gfx::blit(hist_pass.id,
128 history_tex->native_handle(),
129 0,
130 0,
131 temp_fbo->get_texture(0)->native_handle(),
132 0,
133 0);
134
135 if(params.output)
136 {
137 gfx::render_pass out_pass("TAA/Output Blit");
138 gfx::blit(out_pass.id,
139 params.output->get_texture(0)->native_handle(),
140 0,
141 0,
142 temp_fbo->get_texture(0)->native_handle(),
143 0,
144 0);
145 return params.output;
146 }
147
148 return temp_fbo;
149}
150
152{
153 rview.tex_remove("TAA_HISTORY");
154 rview.fbo_remove("TAA_TEMP");
155 rview.tex_remove("TAA_TEMP");
156}
157
158} // namespace unravel
void tex_remove(const hpp::string_view &id)
void fbo_remove(const hpp::string_view &id)
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
Definition taa_pass.cpp:19
void release_resources(gfx::render_view &rview)
Definition taa_pass.cpp:151
auto run(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
Definition taa_pass.cpp:69
Definition cache.hpp:11
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 blit(view_id _id, texture_handle _dst, uint16_t _dstX, uint16_t _dstY, texture_handle _src, uint16_t _srcX, uint16_t _srcY, uint16_t _width, uint16_t _height)
void discard(uint8_t _flags)
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:977
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)
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
Definition profiler.h:675
void set_view_proj(const float *v, const float *p)
gfx::view_id id
void bind(const frame_buffer *fb=nullptr) const