Unravel Engine C++ Reference
Loading...
Searching...
No Matches
fxaa_pass.cpp
Go to the documentation of this file.
1#include "fxaa_pass.h"
2
3// These includes match your engine’s style. Adapt as needed:
4#include <bx/math.h>
7#include <graphics/texture.h>
8
9namespace unravel
10{
11
13{
14 auto& am = ctx.get_cached<asset_manager>();
15
16 // Load your FXAA shaders (compiled from vs_fxaa.sc / fs_fxaa.sc).
17 // Adjust these paths to where your shaders actually reside.
18 auto vs_clip_quad_ex = am.get_asset<gfx::shader>("engine:/data/shaders/vs_clip_quad.sc");
19 auto fs_fxaa = am.get_asset<gfx::shader>("engine:/data/shaders/fxaa/fs_fxaa.sc");
20
21 // Create the GPU program
22 fxaa_program_.program = std::make_unique<gpu_program>(vs_clip_quad_ex, fs_fxaa);
23
24 // Cache the uniforms (only "s_input" for FXAA).
25 fxaa_program_.cache_uniforms();
26
27 // Return true if all is valid.
28 return fxaa_program_.program->is_valid();
29}
30
31auto fxaa_pass::create_or_update_output_fb(gfx::render_view& rview,
33 const gfx::frame_buffer::ptr& output)
35{
36 if(output)
37 {
38 return output;
39 }
40 auto input_sz = input->get_size();
41 auto input_format = input->get_texture(0)->info.format;
42 // This is presumably your engine’s method to get the bgfx::TextureFormat.
43 // If your engine uses something else, adapt accordingly.
44
45 auto& output_tex = rview.tex_get_or_emplace("FXAA_OUTPUT");
46 if(gfx::needs_recreate(output_tex, input_sz, input_format))
47 {
48 output_tex.reset();
49 output_tex =
50 std::make_shared<gfx::texture>(input_sz.width, input_sz.height, false, 1, input_format, BGFX_TEXTURE_RT);
51 }
52 auto& output_fbo = rview.fbo_get_or_emplace("FXAA_OUTPUT");
53 if(gfx::needs_recreate(output_fbo, input_sz))
54 {
55 output_fbo.reset();
56 output_fbo = std::make_shared<gfx::frame_buffer>();
57 output_fbo->populate({output_tex});
58 }
59 return output_fbo;
60}
61
63{
64 const auto& input = params.input;
65 auto output = create_or_update_output_fb(rview, params.input, params.output);
66
67 const auto output_size = output->get_size();
68
69 gfx::render_pass pass("FXAA/Pass");
70 pass.bind(output.get());
71
72 // For a typical post-processing pass, we do a full-screen quad in clip space.
73 // So set view & projection to identity (no camera transform).
74 pass.set_view_proj({}, {});
75
76 // Begin the FXAA program
77 if(fxaa_program_.program->begin())
78 {
79 // The texture we want to sample in FS is the color texture from our input buffer.
80 // Often you have something like input->get_color_texture(0).
81 // If your engine uses a different method, adapt accordingly.
82 const auto& color_tex = input->get_texture(0);
83
84 // Bind "s_input" sampler to slot 0
85 gfx::set_texture(fxaa_program_.s_input, 0, color_tex);
86
87 // Set scissor to the entire output area
88 irect32_t rect(0, 0, output_size.width, output_size.height);
90
91 // Draw a full-screen quad
92 // Typically, your engine might provide `clip_quad()` or something similar.
93 // We'll do the same approach as your atmospheric_pass example.
94 auto topology = gfx::clip_quad(1.0f);
95
96 // State: write RGBA. Depth test is optional;
97 // for a post pass, we usually don't need depth testing at all.
98 gfx::set_state(topology | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
99
100 // Submit the draw call using the pass ID
101 gfx::submit(pass.id, fxaa_program_.program->native_handle());
102
103 // Reset state (optional cleanup)
104 gfx::set_state(BGFX_STATE_DEFAULT);
105
106 // End usage of our GPU program
107 fxaa_program_.program->end();
108 }
109
110 // Discard is typically called at the end of the pass if your engine requires it.
111 gfx::discard();
112
113 return output;
114}
115
117{
118 rview.fbo_remove("FXAA_OUTPUT");
119 rview.tex_remove("FXAA_OUTPUT");
120}
121
122} // 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 fxaa_pass.cpp:12
auto run(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
Definition fxaa_pass.cpp:62
void release_resources(gfx::render_view &rview)
void submit(view_id _id, program_handle _handle, int32_t _depth, bool _preserveState)
uint16_t set_scissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
Definition graphics.cpp:952
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)
void set_view_proj(const float *v, const float *p)
gfx::view_id id
void bind(const frame_buffer *fb=nullptr) const
T width() const
T height() const