15 auto cs_histogram = am.
get_asset<
gfx::shader>(
"engine:/data/shaders/exposure/cs_luminance_histogram.sc");
16 auto cs_average = am.get_asset<
gfx::shader>(
"engine:/data/shaders/exposure/cs_histogram_average.sc");
18 if(!cs_histogram || !cs_average)
23 histogram_program_.program = std::make_shared<gpu_program>(cs_histogram);
24 histogram_program_.cache_uniforms();
26 average_program_.program = std::make_shared<gpu_program>(cs_average);
27 average_program_.cache_uniforms();
29 histogram_buffer_ = bgfx::createDynamicIndexBuffer(histogram_bins,
30 BGFX_BUFFER_COMPUTE_READ_WRITE | BGFX_BUFFER_INDEX32);
32 return histogram_program_.program->is_valid() &&
33 average_program_.program->is_valid() &&
34 bgfx::isValid(histogram_buffer_);
39 if(bgfx::isValid(histogram_buffer_))
41 bgfx::destroy(histogram_buffer_);
42 histogram_buffer_ = BGFX_INVALID_HANDLE;
63 exposure_tex = std::make_shared<gfx::texture>(1,
67 gfx::texture_format::R32F,
68 BGFX_TEXTURE_COMPUTE_WRITE);
75 const float initial_exposure = 1.0f;
90 const auto input_size =
input->get_size();
96 const uint32_t longest = std::max(input_size.width, input_size.height);
97 const float scale = (longest > max_metering_dim) ? (
float(max_metering_dim) /
float(longest)) : 1.0f;
98 const uint32_t meter_width = std::max(1u, uint32_t(
float(input_size.width) *
scale));
99 const uint32_t meter_height = std::max(1u, uint32_t(
float(input_size.height) *
scale));
103 histogram_program_.program->begin();
109 float log_range = max_log_lum - min_log_lum;
110 float inv_log_range = 1.0f / log_range;
111 float params[4] = {min_log_lum, inv_log_range, float(meter_width), float(meter_height)};
114 float metering_params[4] = {float(
static_cast<int>(config.metering_mode)), config.metering_area, 0.0f, 0.0f};
117 uint32_t groups_x = (meter_width + 15) / 16;
118 uint32_t groups_y = (meter_height + 15) / 16;
119 bgfx::dispatch(pass.id, histogram_program_.program->native_handle(), groups_x, groups_y, 1);
121 histogram_program_.program->end();
124void auto_exposure_pass::run_average(
gfx::render_view& rview,
const settings& config,
float dt)
126 auto exposure_tex = rview.
tex_get(
"AUTO_EXPOSURE");
134 average_program_.program->begin();
138 gfx::set_image(1, exposure_tex->native_handle(), 0, bgfx::Access::ReadWrite, gfx::texture_format::R32F);
140 float log_range = max_log_lum - min_log_lum;
141 float params0[4] = {min_log_lum, log_range, config.low_percentile, config.high_percentile};
144 float params1[4] = {config.min_ev, config.max_ev, config.compensation, 0.0f};
147 float effective_dt = dt;
151 effective_dt = 100.0f;
155 float params2[4] = {effective_dt, config.adaptation_speed_up, config.adaptation_speed_down, 0.0f};
158 bgfx::dispatch(pass.id, average_program_.program->native_handle(), 1, 1, 1);
160 average_program_.program->end();
167 ensure_resources(rview);
std::shared_ptr< texture > ptr
void tex_remove(const hpp::string_view &id)
auto tex_get(const hpp::string_view &id) const -> const texture::ptr &
auto tex_safe_get(const hpp::string_view &id) const -> const texture::ptr &
auto tex_get_or_emplace(const hpp::string_view &id) -> texture::ptr &
auto data_get_or_emplace(const hpp::string_view &id, uint32_t default_val=0) -> uint32_t &
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 shutdown() -> int32_t
auto init(rtti::context &ctx) -> bool
void run(gfx::render_view &rview, const run_params ¶ms)
Runs histogram + temporal average to produce a 1x1 adapted exposure texture.
void release_resources(gfx::render_view &rview)
auto get_exposure_texture(gfx::render_view &rview) const -> gfx::texture::ptr
void update_texture_2d(texture_handle _handle, uint16_t _layer, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const memory_view *_mem, uint16_t _pitch)
void set_buffer(uint8_t _stage, index_buffer_handle _handle, access _access)
auto needs_recreate(const gfx::frame_buffer::ptr &fbo, const usize32_t &size) -> bool
const memory_view * copy(const void *_data, uint32_t _size)
void set_image(uint8_t _stage, texture_handle _handle, uint8_t _mip, access _access, texture_format _format)
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
std::vector< float > scale
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
gfx::frame_buffer::ptr input