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(const gfx::frame_buffer::ptr& input, const gfx::frame_buffer::ptr& output)
33{
34 if(output)
35 {
36 return output;
37 }
38 // 1) Get the size & format from the input
39 auto input_sz = input->get_size();
40 auto input_format = input->get_texture(0)->info.format;
41 // This is presumably your engine’s method to get the bgfx::TextureFormat.
42 // If your engine uses something else, adapt accordingly.
43
44 // 2) Compare with our stored (lastWidth_, lastHeight_, lastFormat_)
45 bool needs_recreate = false;
46
47 if(!output_ || input_sz != output_->get_size() || input_format != output_->get_texture()->info.format)
48 {
49 needs_recreate = true;
50 }
51
52 // 3) If no changes, do nothing
53 if(!needs_recreate)
54 return output_;
55
56 // 4) Otherwise, destroy old outputFb_ if it exists
57 if(output_)
58 {
59 output_.reset(); // or however your engine releases a frame_buffer
60 }
61
62 // 5) Create a new texture with the same size/format
63 // Then create a new framebuffer that wraps it.
64 // Pseudocode: your engine might differ in how it creates textures/FBs.
65 auto output_tex =
66 std::make_shared<gfx::texture>(input_sz.width, input_sz.height, false, 1, input_format, BGFX_TEXTURE_RT);
67
68 // Potentially also create a depth buffer, if needed.
69 // If you just need color output, that might be optional.
70
71 output_ = std::make_shared<gfx::frame_buffer>();
72 output_->populate({output_tex});
73
74 return output_;
75}
76
78{
79 const auto& input = params.input;
80 auto output = create_or_update_output_fb(params.input, params.output);
81
82 const auto output_size = output->get_size();
83
84 gfx::render_pass pass("fxaa_pass");
85 pass.bind(output.get());
86
87 // For a typical post-processing pass, we do a full-screen quad in clip space.
88 // So set view & projection to identity (no camera transform).
89 pass.set_view_proj({}, {});
90
91 // Begin the FXAA program
92 if(fxaa_program_.program->begin())
93 {
94 // The texture we want to sample in FS is the color texture from our input buffer.
95 // Often you have something like input->get_color_texture(0).
96 // If your engine uses a different method, adapt accordingly.
97 const auto& color_tex = input->get_texture(0);
98
99 // Bind "s_input" sampler to slot 0
100 gfx::set_texture(fxaa_program_.s_input, 0, color_tex);
101
102 // Set scissor to the entire output area
103 irect32_t rect(0, 0, output_size.width, output_size.height);
105
106 // Draw a full-screen quad
107 // Typically, your engine might provide `clip_quad()` or something similar.
108 // We'll do the same approach as your atmospheric_pass example.
109 auto topology = gfx::clip_quad(1.0f);
110
111 // State: write RGBA. Depth test is optional;
112 // for a post pass, we usually don't need depth testing at all.
113 gfx::set_state(topology | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
114
115 // Submit the draw call using the pass ID
116 gfx::submit(pass.id, fxaa_program_.program->native_handle());
117
118 // Reset state (optional cleanup)
119 gfx::set_state(BGFX_STATE_DEFAULT);
120
121 // End usage of our GPU program
122 fxaa_program_.program->end();
123 }
124
125 // Discard is typically called at the end of the pass if your engine requires it.
126 gfx::discard();
127
128 return output;
129}
130
131} // 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
Definition fxaa_pass.cpp:12
auto run(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
Definition fxaa_pass.cpp:77
void submit(view_id _id, program_handle _handle, int32_t _depth, bool _preserveState)
Definition graphics.cpp:899
uint16_t set_scissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
Definition graphics.cpp:778
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
void set_view_proj(const float *v, const float *p)
gfx::view_id id
Definition render_pass.h:98
void bind(const frame_buffer *fb=nullptr) const
T width() const
T height() const