Skip to content

Commit 0825eed

Browse files
allenwpbeicause
andcommitted
Always perform color correction and debanding on nonlinear sRGB values.
Fixes #107730 Co-authored-by: LuoZhihao <[email protected]>
1 parent 4d1f26e commit 0825eed

18 files changed

+107
-23
lines changed

doc/classes/ProjectSettings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,9 +2744,9 @@
27442744
[b]Note:[/b] This property is only read when the project starts. There is currently no way to change this setting at run-time.
27452745
</member>
27462746
<member name="rendering/anti_aliasing/quality/use_debanding" type="bool" setter="" getter="" default="false">
2747-
If [code]true[/code], uses a fast post-processing filter to make banding significantly less visible in 3D. 2D rendering is [i]not[/i] affected by debanding unless the [member Environment.background_mode] is [constant Environment.BG_CANVAS].
2747+
If [code]true[/code], uses a fast post-processing filter to make banding significantly less visible. This setting affects both [member Viewport.use_debanding] and [member RenderingServer.use_global_debanding] and behaves slightly differently based on [member rendering/viewport/hdr_2d]: When [member rendering/viewport/hdr_2d] is [code]false[/code], [Viewport] debanding will be used. When [member rendering/viewport/hdr_2d] is [code]true[/code], global debanding will be used.
27482748
In some cases, debanding may introduce a slightly noticeable dithering pattern. It's recommended to enable debanding only when actually needed since the dithering pattern will make lossless-compressed screenshots larger.
2749-
[b]Note:[/b] This property is only read when the project starts. To set debanding at runtime, set [member Viewport.use_debanding] on the root [Viewport] instead, or use [method RenderingServer.viewport_set_use_debanding].
2749+
[b]Note:[/b] This property is only read when the project starts. To set debanding at runtime, set [member Viewport.use_debanding] on the root [Viewport] if this viewport has [member Viewport.use_hdr_2d] set to [code]false[/code] or set [member RenderingServer.use_global_debanding] if [member Viewport.use_hdr_2d] of the root [Viewport] is [code]true[/code].
27502750
</member>
27512751
<member name="rendering/anti_aliasing/quality/use_taa" type="bool" setter="" getter="" default="false">
27522752
Enables temporal antialiasing for the default screen [Viewport]. TAA works by jittering the camera and accumulating the images of the last rendered frames, motion vector rendering is used to account for camera and object motion. Enabling TAA can make the image blurrier, which is partially counteracted by automatically using a negative mipmap LOD bias (see [member rendering/textures/default_filters/texture_mipmap_bias]).

doc/classes/RenderingServer.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4232,7 +4232,7 @@
42324232
<param index="0" name="viewport" type="RID" />
42334233
<param index="1" name="enable" type="bool" />
42344234
<description>
4235-
If [code]true[/code], enables debanding on the specified viewport. Equivalent to [member ProjectSettings.rendering/anti_aliasing/quality/use_debanding] or [member Viewport.use_debanding].
4235+
If [code]true[/code], enables debanding on the specified viewport. Equivalent to [member Viewport.use_debanding]. See also [member ProjectSettings.rendering/anti_aliasing/quality/use_debanding].
42364236
</description>
42374237
</method>
42384238
<method name="viewport_set_use_hdr_2d">
@@ -4450,6 +4450,12 @@
44504450
<member name="render_loop_enabled" type="bool" setter="set_render_loop_enabled" getter="is_render_loop_enabled">
44514451
If [code]false[/code], disables rendering completely, but the engine logic is still being processed. You can call [method force_draw] to draw a frame even with rendering disabled.
44524452
</member>
4453+
<member name="use_global_debanding" type="bool" setter="set_use_global_debanding" getter="get_use_global_debanding">
4454+
If [code]true[/code] and [member Viewport.use_hdr_2d] of the root [Viewport] is [code]true[/code], uses a fast post-processing filter to make banding significantly less visible. All 3D and 2D rendering, including canvas items, is affected by global debanding.
4455+
In some cases, debanding may introduce a slightly noticeable dithering pattern. It's recommended to enable debanding only when actually needed since the dithering pattern will make lossless-compressed screenshots larger.
4456+
See also [member ProjectSettings.rendering/anti_aliasing/quality/use_debanding] and [method RenderingServer.viewport_set_use_debanding].
4457+
[b]Note:[/b] [member use_global_debanding] will only have an effect when [member Viewport.use_hdr_2d] of the root [Viewport] is [code]true[/code]. To enable debanding when [member Viewport.use_hdr_2d] is [code]false[/code], configure debanding in your project settings with [member ProjectSettings.rendering/anti_aliasing/quality/use_debanding] or use [member Viewport.use_debanding].
4458+
</member>
44534459
</members>
44544460
<signals>
44554461
<signal name="frame_post_draw">

doc/classes/Viewport.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,10 @@
444444
If [code]true[/code], the viewport should render its background as transparent.
445445
</member>
446446
<member name="use_debanding" type="bool" setter="set_use_debanding" getter="is_using_debanding" default="false">
447-
If [code]true[/code], uses a fast post-processing filter to make banding significantly less visible in 3D. 2D rendering is [i]not[/i] affected by debanding unless the [member Environment.background_mode] is [constant Environment.BG_CANVAS].
447+
If [code]true[/code] and [member use_hdr_2d] is [code]false[/code], uses a fast post-processing filter to make banding significantly less visible in 3D. 2D rendering is [i]not[/i] affected by debanding unless the [member Environment.background_mode] is [constant Environment.BG_CANVAS].
448448
In some cases, debanding may introduce a slightly noticeable dithering pattern. It's recommended to enable debanding only when actually needed since the dithering pattern will make lossless-compressed screenshots larger.
449449
See also [member ProjectSettings.rendering/anti_aliasing/quality/use_debanding] and [method RenderingServer.viewport_set_use_debanding].
450+
[b]Note:[/b] [member use_debanding] will only have an effect when [member use_hdr_2d] is [code]false[/code]. To enable debanding when [member use_hdr_2d] is [code]true[/code], configure debanding in your project settings with [member ProjectSettings.rendering/anti_aliasing/quality/use_debanding] or use [member RenderingServer.use_global_debanding].
450451
</member>
451452
<member name="use_hdr_2d" type="bool" setter="set_use_hdr_2d" getter="is_using_hdr_2d" default="false">
452453
If [code]true[/code], 2D rendering will use a high dynamic range (HDR) format framebuffer matching the bit depth of the 3D framebuffer. When using the Forward+ renderer this will be an [code]RGBA16[/code] framebuffer, while when using the Mobile renderer it will be an [code]RGB10_A2[/code] framebuffer. Additionally, 2D rendering will take place in linear color space and will be converted to sRGB space immediately before blitting to the screen (if the Viewport is attached to the screen). Practically speaking, this means that the end result of the Viewport will not be clamped into the [code]0-1[/code] range and can be used in 3D rendering without color space adjustments. This allows 2D rendering to take advantage of effects requiring high dynamic range (e.g. 2D glow) as well as substantially improves the appearance of effects requiring highly detailed gradients.

editor/scene/3d/node_3d_editor_plugin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#include "scene/resources/3d/sky_material.h"
101101
#include "scene/resources/packed_scene.h"
102102
#include "scene/resources/surface_tool.h"
103+
#include "servers/rendering/renderer_compositor.h"
103104

104105
constexpr real_t DISTANCE_DEFAULT = 4;
105106

@@ -2933,7 +2934,8 @@ void Node3DEditorViewport::_project_settings_changed() {
29332934
viewport->set_use_hdr_2d(use_hdr_2d);
29342935

29352936
const bool use_debanding = GLOBAL_GET("rendering/anti_aliasing/quality/use_debanding");
2936-
viewport->set_use_debanding(use_debanding);
2937+
viewport->set_use_debanding_warn(use_debanding, false);
2938+
RendererCompositor::get_singleton()->set_use_global_debanding(use_debanding);
29372939

29382940
const bool use_occlusion_culling = GLOBAL_GET("rendering/occlusion_culling/use_occlusion_culling");
29392941
viewport->set_use_occlusion_culling(use_occlusion_culling);

scene/main/scene_tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,7 @@ SceneTree::SceneTree() {
20732073
root->set_use_taa(use_taa);
20742074

20752075
const bool use_debanding = GLOBAL_DEF("rendering/anti_aliasing/quality/use_debanding", false);
2076-
root->set_use_debanding(use_debanding);
2076+
root->set_use_debanding_warn(use_debanding, false);
20772077

20782078
const bool use_occlusion_culling = GLOBAL_DEF("rendering/occlusion_culling/use_occlusion_culling", false);
20792079
root->set_use_occlusion_culling(use_occlusion_culling);

scene/main/viewport.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3715,12 +3715,20 @@ bool Viewport::is_using_taa() const {
37153715
}
37163716

37173717
void Viewport::set_use_debanding(bool p_use_debanding) {
3718+
set_use_debanding_warn(p_use_debanding, true);
3719+
}
3720+
3721+
void Viewport::set_use_debanding_warn(bool p_use_debanding, bool p_warn_no_effect) {
37183722
ERR_MAIN_THREAD_GUARD;
37193723
if (use_debanding == p_use_debanding) {
37203724
return;
37213725
}
37223726
use_debanding = p_use_debanding;
37233727
RS::get_singleton()->viewport_set_use_debanding(viewport, p_use_debanding);
3728+
3729+
if (p_warn_no_effect && use_hdr_2d) {
3730+
WARN_PRINT("Attempting to set viewport debanding when HDR 2D is enabled. This will have no effect; use RenderingServer.use_global_debanding instead.");
3731+
}
37243732
}
37253733

37263734
bool Viewport::is_using_debanding() const {

scene/main/viewport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ class Viewport : public Node {
603603
AnisotropicFiltering get_anisotropic_filtering_level() const;
604604

605605
void set_use_debanding(bool p_use_debanding);
606+
void set_use_debanding_warn(bool p_use_debanding, bool p_warn_no_effect);
606607
bool is_using_debanding() const;
607608

608609
void set_mesh_lod_threshold(float p_pixels);

servers/rendering/renderer_compositor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,20 @@ bool RendererCompositor::is_xr_enabled() const {
4949
return xr_enabled;
5050
}
5151

52+
bool RendererCompositor::get_use_global_debanding() const {
53+
return use_global_debanding;
54+
}
55+
56+
void RendererCompositor::set_use_global_debanding(bool p_enable) {
57+
use_global_debanding = p_enable;
58+
}
59+
5260
RendererCompositor::RendererCompositor() {
5361
ERR_FAIL_COND_MSG(singleton != nullptr, "A RendererCompositor singleton already exists.");
5462
singleton = this;
5563

64+
use_global_debanding = GLOBAL_DEF("rendering/anti_aliasing/quality/use_debanding", false);
65+
5666
#ifndef XR_DISABLED
5767
if (XRServer::get_xr_mode() == XRServer::XRMODE_DEFAULT) {
5868
xr_enabled = GLOBAL_GET("xr/shaders/enabled");

servers/rendering/renderer_compositor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class RendererCompositor {
7171
protected:
7272
static RendererCompositor *(*_create_func)();
7373
bool back_end = false;
74+
bool use_global_debanding = false;
7475
static bool low_end;
7576

7677
public:
@@ -105,6 +106,8 @@ class RendererCompositor {
105106

106107
static bool is_low_end() { return low_end; }
107108
virtual bool is_xr_enabled() const;
109+
virtual bool get_use_global_debanding() const;
110+
virtual void set_use_global_debanding(bool p_enable);
108111

109112
static RendererCompositor *get_singleton() { return singleton; }
110113
RendererCompositor();

servers/rendering/renderer_rd/effects/tone_mapper.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ void ToneMapper::tonemapper(RID p_source_color, RID p_dst_framebuffer, const Ton
123123
tonemap.push_constant.flags |= p_settings.use_color_correction ? TONEMAP_FLAG_USE_COLOR_CORRECTION : 0;
124124

125125
tonemap.push_constant.flags |= p_settings.use_fxaa ? TONEMAP_FLAG_USE_FXAA : 0;
126-
tonemap.push_constant.flags |= p_settings.use_debanding ? TONEMAP_FLAG_USE_DEBANDING : 0;
126+
// When convert_to_srgb is false: postpone debanding until blit.
127+
tonemap.push_constant.flags |= (p_settings.use_debanding && p_settings.convert_to_srgb) ? TONEMAP_FLAG_USE_DEBANDING : 0;
127128
tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
128129
tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
129130

@@ -207,8 +208,8 @@ void ToneMapper::tonemapper(RD::DrawListID p_subpass_draw_list, RID p_source_col
207208
tonemap.push_constant.auto_exposure_scale = p_settings.auto_exposure_scale;
208209

209210
tonemap.push_constant.flags |= p_settings.use_color_correction ? TONEMAP_FLAG_USE_COLOR_CORRECTION : 0;
210-
211-
tonemap.push_constant.flags |= p_settings.use_debanding ? TONEMAP_FLAG_USE_DEBANDING : 0;
211+
// When convert_to_srgb is false: postpone debanding until blit.
212+
tonemap.push_constant.flags |= (p_settings.use_debanding && p_settings.convert_to_srgb) ? TONEMAP_FLAG_USE_DEBANDING : 0;
212213
tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
213214

214215
tonemap.push_constant.flags |= p_settings.convert_to_srgb ? TONEMAP_FLAG_CONVERT_TO_SRGB : 0;

0 commit comments

Comments
 (0)