1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Optimize glow and tonemap gather step in the mobile renderer

Mobile devices are typically bandwidth bound which means we need to do as few texture samples as possible.

They typically use TBDR GPUs which means that all rendering takes place on special optimized tiles. As a side effect, reading back memory from tile to VRAM is really slow, especially on Mali devices.

This commit uses a technique where you do a small blur while downsampling, and then another small blur while upsampling to get really high quality glow. While this doesn't reduce the renderpass count very much, it does reduce the texture read bandwidth by almost 10 times. Overall glow was more texture-read bound than memory write, bound, so this was a huge win.

A side effect of this new technique is that we can gather the glow as we upsample instead of gathering the glow in the final tonemap pass. Doing so allows us to significantly reduce the cost of the tonemap pass as well.
This commit is contained in:
clayjohn
2025-09-01 14:43:37 -07:00
parent 084d5d407e
commit 2e59cb41f4
22 changed files with 1524 additions and 519 deletions

View File

@@ -32,6 +32,7 @@
#include "servers/rendering/renderer_rd/pipeline_cache_rd.h"
#include "servers/rendering/renderer_rd/shaders/effects/tonemap.glsl.gen.h"
#include "servers/rendering/renderer_rd/shaders/effects/tonemap_mobile.glsl.gen.h"
#include "servers/rendering/rendering_server.h"
@@ -39,35 +40,68 @@ namespace RendererRD {
class ToneMapper {
private:
bool using_mobile_version = false;
enum TonemapMode {
TONEMAP_MODE_NORMAL,
TONEMAP_MODE_BICUBIC_GLOW_FILTER,
TONEMAP_MODE_1D_LUT,
TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT,
TONEMAP_MODE_SUBPASS,
TONEMAP_MODE_SUBPASS_1D_LUT,
TONEMAP_MODE_NORMAL_MULTIVIEW,
TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW,
TONEMAP_MODE_1D_LUT_MULTIVIEW,
TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW,
TONEMAP_MODE_SUBPASS_MULTIVIEW,
TONEMAP_MODE_SUBPASS_1D_LUT_MULTIVIEW,
TONEMAP_MODE_MAX
};
enum {
enum TonemapModeMobile {
TONEMAP_MOBILE_MODE_NORMAL,
TONEMAP_MOBILE_MODE_1D_LUT,
TONEMAP_MOBILE_MODE_SUBPASS,
TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT,
TONEMAP_MOBILE_MODE_NORMAL_MULTIVIEW,
TONEMAP_MOBILE_MODE_1D_LUT_MULTIVIEW,
TONEMAP_MOBILE_MODE_SUBPASS_MULTIVIEW,
TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT_MULTIVIEW,
TONEMAP_MOBILE_MODE_MAX
};
enum Flags {
TONEMAP_FLAG_USE_BCS = (1 << 0),
TONEMAP_FLAG_USE_GLOW = (1 << 1),
TONEMAP_FLAG_USE_AUTO_EXPOSURE = (1 << 2),
TONEMAP_FLAG_USE_COLOR_CORRECTION = (1 << 3),
TONEMAP_FLAG_USE_FXAA = (1 << 4),
TONEMAP_FLAG_USE_8_BIT_DEBANDING = (1 << 5),
TONEMAP_FLAG_USE_10_BIT_DEBANDING = (1 << 6),
TONEMAP_FLAG_CONVERT_TO_SRGB = (1 << 7),
};
enum FlagsMobile {
TONEMAP_MOBILE_FLAG_USE_BCS = (1 << 0),
TONEMAP_MOBILE_FLAG_USE_GLOW = (1 << 1),
TONEMAP_MOBILE_FLAG_USE_GLOW_MAP = (1 << 2),
TONEMAP_MOBILE_FLAG_USE_COLOR_CORRECTION = (1 << 3),
TONEMAP_MOBILE_FLAG_USE_FXAA = (1 << 4),
TONEMAP_MOBILE_FLAG_USE_8_BIT_DEBANDING = (1 << 5),
TONEMAP_MOBILE_FLAG_USE_10_BIT_DEBANDING = (1 << 6),
TONEMAP_MOBILE_FLAG_CONVERT_TO_SRGB = (1 << 7),
TONEMAP_MOBILE_FLAG_TONEMAPPER_LINEAR = (1 << 8),
TONEMAP_MOBILE_FLAG_TONEMAPPER_REINHARD = (1 << 9),
TONEMAP_MOBILE_FLAG_TONEMAPPER_FILMIC = (1 << 10),
TONEMAP_MOBILE_FLAG_TONEMAPPER_ACES = (1 << 11),
TONEMAP_MOBILE_FLAG_TONEMAPPER_AGX = (1 << 12),
TONEMAP_MOBILE_FLAG_GLOW_MODE_ADD = (1 << 13),
TONEMAP_MOBILE_FLAG_GLOW_MODE_SCREEN = (1 << 14),
TONEMAP_MOBILE_FLAG_GLOW_MODE_SOFTLIGHT = (1 << 15),
TONEMAP_MOBILE_FLAG_GLOW_MODE_REPLACE = (1 << 16),
TONEMAP_MOBILE_FLAG_GLOW_MODE_MIX = (1 << 17),
};
struct TonemapPushConstant {
float bcs[3]; // 12 - 12
uint32_t flags; // 4 - 16
@@ -89,6 +123,19 @@ private:
float luminance_multiplier; // 4 - 96
};
struct TonemapPushConstantMobile {
float bcs[3]; // 12 - 12
float luminance_multiplier; // 4 - 16
float src_pixel_size[2]; // 8 - 24
float dest_pixel_size[2]; // 8 - 32
float glow_intensity; // 4 - 36
float glow_map_strength; // 4 - 40
float exposure; // 4 - 44
float white; // 4 - 48
};
/* tonemap actually writes to a framebuffer, which is
* better to do using the raster pipeline rather than
* compute, as that framebuffer might be in different formats
@@ -100,21 +147,20 @@ private:
PipelineCacheRD pipelines[TONEMAP_MODE_MAX];
} tonemap;
struct TonemapMobile {
TonemapPushConstantMobile push_constant;
TonemapMobileShaderRD shader;
RID shader_version;
PipelineCacheRD pipelines[TONEMAP_MOBILE_MODE_MAX];
} tonemap_mobile;
public:
ToneMapper();
ToneMapper(bool p_use_mobile_version);
~ToneMapper();
struct TonemapSettings {
bool use_glow = false;
enum GlowMode {
GLOW_MODE_ADD,
GLOW_MODE_SCREEN,
GLOW_MODE_SOFTLIGHT,
GLOW_MODE_REPLACE,
GLOW_MODE_MIX
};
GlowMode glow_mode = GLOW_MODE_SCREEN;
RS::EnvironmentGlowBlendMode glow_mode = RS::ENV_GLOW_BLEND_MODE_SCREEN;
float glow_intensity = 0.3;
float glow_map_strength = 0.0f;
float glow_levels[7] = { 1.0, 0.8, 0.4, 0.1, 0.0, 0.0, 0.0 };
@@ -149,13 +195,15 @@ public:
};
DebandingMode debanding_mode = DEBANDING_MODE_DISABLED;
Vector2i texture_size;
Vector2i dest_texture_size;
uint32_t view_count = 1;
bool convert_to_srgb = false;
};
void tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings);
void tonemapper(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings);
void tonemapper_mobile(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings);
void tonemapper_subpass(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings);
};
} // namespace RendererRD