Unravel Engine C++ Reference
Loading...
Searching...
No Matches
tonemapping_pass.cpp
Go to the documentation of this file.
1#include "tonemapping_pass.h"
4#include <graphics/texture.h>
5
6namespace unravel
7{
8
10{
11 auto& am = ctx.get_cached<asset_manager>();
12
13 auto vs_clip_quad_ex = am.get_asset<gfx::shader>("engine:/data/shaders/vs_clip_quad.sc");
14 auto fs_atmospherics = am.get_asset<gfx::shader>("engine:/data/shaders/tonemapping/fs_tonemapping.sc");
15
16 tonemapping_program_.program = std::make_unique<gpu_program>(vs_clip_quad_ex, fs_atmospherics);
17 tonemapping_program_.cache_uniforms();
18
19 return true;
20}
21
22auto tonemapping_pass::create_or_update_output_fb(const gfx::frame_buffer::ptr& input,
24{
25 if(output)
26 {
27 return output;
28 }
29 // 1) Get the size & format from the input
30 auto input_sz = input->get_size();
31 auto input_format = input->get_texture(0)->info.format;
32 // This is presumably your engine’s method to get the bgfx::TextureFormat.
33 // If your engine uses something else, adapt accordingly.
34
35 // 2) Compare with our stored (lastWidth_, lastHeight_, lastFormat_)
36 bool needs_recreate = false;
37
38 if(!output_ || input_sz != output_->get_size() || input_format != output_->get_texture()->info.format)
39 {
40 needs_recreate = true;
41 }
42
43 // 3) If no changes, do nothing
44 if(!needs_recreate)
45 return output_;
46
47 // 4) Otherwise, destroy old outputFb_ if it exists
48 if(output_)
49 {
50 output_.reset(); // or however your engine releases a frame_buffer
51 }
52
53 // 5) Create a new texture with the same size/format
54 // Then create a new framebuffer that wraps it.
55 // Pseudocode: your engine might differ in how it creates textures/FBs.
56 auto output_tex = std::make_shared<gfx::texture>(input_sz.width,
57 input_sz.height,
58 false,
59 1,
60 gfx::texture_format::RGBA8,
61 BGFX_TEXTURE_RT);
62
63 // Potentially also create a depth buffer, if needed.
64 // If you just need color output, that might be optional.
65
66 output_ = std::make_shared<gfx::frame_buffer>();
67 output_->populate({output_tex});
68
69 return output_;
70}
71
73{
74 // {
75 // {
76 // // Calculate luminance.
77 // setOffsets2x2Lum(u_offset, 128, 128);
78 // bgfx::setTexture(0, s_texColor, m_fbtextures[0]);
79 // bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
80 // screenSpaceQuad(m_caps->originBottomLeft);
81 // bgfx::submit(hdrLuminance, m_lumProgram);
82
83 // // Downscale luminance 0.
84 // setOffsets4x4Lum(u_offset, 128, 128);
85 // bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[0]) );
86 // bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
87 // screenSpaceQuad(m_caps->originBottomLeft);
88 // bgfx::submit(hdrLumScale0, m_lumAvgProgram);
89
90 // // Downscale luminance 1.
91 // setOffsets4x4Lum(u_offset, 64, 64);
92 // bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[1]) );
93 // bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
94 // screenSpaceQuad(m_caps->originBottomLeft);
95 // bgfx::submit(hdrLumScale1, m_lumAvgProgram);
96
97 // // Downscale luminance 2.
98 // setOffsets4x4Lum(u_offset, 16, 16);
99 // bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[2]) );
100 // bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
101 // screenSpaceQuad(m_caps->originBottomLeft);
102 // bgfx::submit(hdrLumScale2, m_lumAvgProgram);
103
104 // // Downscale luminance 3.
105 // setOffsets4x4Lum(u_offset, 4, 4);
106 // bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_lum[3]) );
107 // bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
108 // screenSpaceQuad(m_caps->originBottomLeft);
109 // bgfx::submit(hdrLumScale3, m_lumAvgProgram);
110
111 // // m_bright pass m_threshold is tonemap[3].
112 // setOffsets4x4Lum(u_offset, m_width/2, m_height/2);
113 // bgfx::setTexture(0, s_texColor, m_fbtextures[0]);
114 // bgfx::setTexture(1, s_texLum, bgfx::getTexture(m_lum[4]) );
115 // bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
116 // bgfx::setUniform(u_tonemap, tonemap);
117 // screenSpaceQuad(m_caps->originBottomLeft);
118 // bgfx::submit(hdrBrightness, m_brightProgram);
119
120 // // m_blur m_bright pass vertically.
121 // bgfx::setTexture(0, s_texColor, bgfx::getTexture(m_bright) );
122 // bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
123 // bgfx::setUniform(u_tonemap, tonemap);
124 // screenSpaceQuad(m_caps->originBottomLeft);
125 // bgfx::submit(hdrVBlur, m_blurProgram);
126 // }
127 // }
128 const auto& input = params.input;
129 auto output = create_or_update_output_fb(params.input, params.output);
130
131 gfx::render_pass pass("output_buffer_fill");
132 pass.bind(output.get());
133
134 const auto output_size = output->get_size();
135
136 tonemapping_program_.program->begin();
137
138 float tonemap[4] = {params.config.exposure, static_cast<float>(params.config.method), 0.0f, 0.0f};
139 gfx::set_uniform(tonemapping_program_.u_tonemapping, tonemap);
140
141 gfx::set_texture(tonemapping_program_.s_input, 0, input->get_texture());
142 irect32_t rect(0, 0, irect32_t::value_type(output_size.width), irect32_t::value_type(output_size.height));
144 auto topology = gfx::clip_quad(1.0f);
145 gfx::set_state(topology | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A);
146 gfx::submit(pass.id, tonemapping_program_.program->native_handle());
147 gfx::set_state(BGFX_STATE_DEFAULT);
148 tonemapping_program_.program->end();
149
150 gfx::discard();
151
152 return output;
153}
154} // 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 run(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
auto init(rtti::context &ctx) -> bool
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
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:803
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
T width() const
std::int32_t value_type
T height() const