Skip to content
0.2.0-dev Download ZIP
217 lines ·7.7 KB Raw
1/**************************************************************************/
2/* tone_mapper.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#pragma once
32
33#include "servers/rendering/renderer_rd/pipeline_cache_rd.h"
34#include "servers/rendering/renderer_rd/shaders/effects/tonemap.glsl.gen.h"
35#include "servers/rendering/renderer_rd/shaders/effects/tonemap_mobile.glsl.gen.h"
36
37namespace RendererRD {
38
39class ToneMapper {
40private:
41 bool using_mobile_version = false;
42 enum TonemapMode {
43 TONEMAP_MODE_NORMAL,
44 TONEMAP_MODE_BICUBIC_GLOW_FILTER,
45 TONEMAP_MODE_1D_LUT,
46 TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT,
47
48 TONEMAP_MODE_NORMAL_MULTIVIEW,
49 TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW,
50 TONEMAP_MODE_1D_LUT_MULTIVIEW,
51 TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW,
52
53 TONEMAP_MODE_MAX
54 };
55
56 enum TonemapModeMobile {
57 TONEMAP_MOBILE_MODE_NORMAL,
58 TONEMAP_MOBILE_MODE_1D_LUT,
59 TONEMAP_MOBILE_MODE_SUBPASS,
60 TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT,
61
62 TONEMAP_MOBILE_MODE_NORMAL_MULTIVIEW,
63 TONEMAP_MOBILE_MODE_1D_LUT_MULTIVIEW,
64 TONEMAP_MOBILE_MODE_SUBPASS_MULTIVIEW,
65 TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT_MULTIVIEW,
66
67 TONEMAP_MOBILE_MODE_MAX
68 };
69
70 enum Flags {
71 TONEMAP_FLAG_USE_BCS = (1 << 0),
72 TONEMAP_FLAG_USE_GLOW = (1 << 1),
73 TONEMAP_FLAG_USE_AUTO_EXPOSURE = (1 << 2),
74 TONEMAP_FLAG_USE_COLOR_CORRECTION = (1 << 3),
75 TONEMAP_FLAG_USE_FXAA = (1 << 4),
76 TONEMAP_FLAG_USE_8_BIT_DEBANDING = (1 << 5),
77 TONEMAP_FLAG_CONVERT_TO_SRGB = (1 << 6),
78 };
79
80 enum FlagsMobile {
81 TONEMAP_MOBILE_FLAG_USE_BCS = (1 << 0),
82 TONEMAP_MOBILE_FLAG_USE_GLOW = (1 << 1),
83 TONEMAP_MOBILE_FLAG_USE_GLOW_MAP = (1 << 2),
84 TONEMAP_MOBILE_FLAG_USE_COLOR_CORRECTION = (1 << 3),
85 TONEMAP_MOBILE_FLAG_USE_FXAA = (1 << 4),
86 TONEMAP_MOBILE_FLAG_USE_8_BIT_DEBANDING = (1 << 5),
87 TONEMAP_MOBILE_FLAG_USE_10_BIT_DEBANDING = (1 << 6),
88 TONEMAP_MOBILE_FLAG_CONVERT_TO_SRGB = (1 << 7),
89
90 TONEMAP_MOBILE_FLAG_TONEMAPPER_LINEAR = (1 << 8),
91 TONEMAP_MOBILE_FLAG_TONEMAPPER_REINHARD = (1 << 9),
92 TONEMAP_MOBILE_FLAG_TONEMAPPER_FILMIC = (1 << 10),
93 TONEMAP_MOBILE_FLAG_TONEMAPPER_ACES = (1 << 11),
94 TONEMAP_MOBILE_FLAG_TONEMAPPER_AGX = (1 << 12),
95
96 TONEMAP_MOBILE_FLAG_GLOW_MODE_ADD = (1 << 13),
97 TONEMAP_MOBILE_FLAG_GLOW_MODE_SCREEN = (1 << 14),
98 TONEMAP_MOBILE_FLAG_GLOW_MODE_SOFTLIGHT = (1 << 15),
99 TONEMAP_MOBILE_FLAG_GLOW_MODE_REPLACE = (1 << 16),
100 TONEMAP_MOBILE_FLAG_GLOW_MODE_MIX = (1 << 17),
101 TONEMAP_MOBILE_ADRENO_BUG = (1 << 18), // Needs to be last so we force the pipeline cache to specify specializations for all variants.
102 };
103
104 struct TonemapPushConstant {
105 float bcs[3]; // 12 - 12
106 uint32_t flags; // 4 - 16
107
108 float pixel_size[2]; // 8 - 24
109 uint32_t tonemapper; // 4 - 28
110 float output_max_value; // 4 - 32
111
112 uint32_t glow_texture_size[2]; // 8 - 40
113 float glow_intensity; // 4 - 44
114 float glow_map_strength; // 4 - 48
115
116 uint32_t glow_mode; // 4 - 52
117 float glow_levels[7]; // 28 - 80
118
119 float exposure; // 4 - 84
120 float white; // 4 - 88
121 float auto_exposure_scale; // 4 - 92
122 float luminance_multiplier; // 4 - 96
123
124 float tonemapper_params[4]; // 16 - 112
125 };
126
127 struct TonemapPushConstantMobile {
128 float bcs[3]; // 12 - 12
129 float luminance_multiplier; // 4 - 16
130
131 float src_pixel_size[2]; // 8 - 24
132 float dest_pixel_size[2]; // 8 - 32
133
134 float glow_intensity; // 4 - 36
135 float glow_map_strength; // 4 - 40
136 float exposure; // 4 - 44
137 float white; // 4 - 48
138
139 float tonemapper_params[4]; // 16 - 64
140 float output_max_value; // 4 - 68
141 float pad[3]; // 12 - 80
142 };
143
144 /* tonemap actually writes to a framebuffer, which is
145 * better to do using the raster pipeline rather than
146 * compute, as that framebuffer might be in different formats
147 */
148 struct Tonemap {
149 TonemapPushConstant push_constant;
150 TonemapShaderRD shader;
151 RID shader_version;
152 PipelineCacheRD pipelines[TONEMAP_MODE_MAX];
153 } tonemap;
154
155 struct TonemapMobile {
156 TonemapPushConstantMobile push_constant;
157 TonemapMobileShaderRD shader;
158 RID shader_version;
159 PipelineCacheRD pipelines[TONEMAP_MOBILE_MODE_MAX];
160 } tonemap_mobile;
161
162public:
163 ToneMapper(bool p_use_mobile_version);
164 ~ToneMapper();
165
166 struct TonemapSettings {
167 bool use_glow = false;
168 RSE::EnvironmentGlowBlendMode glow_mode = RSE::ENV_GLOW_BLEND_MODE_SCREEN;
169 float glow_intensity = 0.3;
170 float glow_map_strength = 0.0f;
171 float glow_levels[7] = { 1.0, 0.8, 0.4, 0.1, 0.0, 0.0, 0.0 };
172 Vector2i glow_texture_size;
173 bool glow_use_bicubic_upscale = false;
174 RID glow_texture;
175 RID glow_map;
176
177 RSE::EnvironmentToneMapper tonemap_mode = RSE::ENV_TONE_MAPPER_LINEAR;
178 float tonemapper_params[4] = { 0.0, 0.0, 0.0, 0.0 };
179 float exposure = 1.0;
180 float white = 1.0;
181 float max_value = 1.0;
182
183 bool use_auto_exposure = false;
184 float auto_exposure_scale = 0.5;
185 RID exposure_texture;
186 float luminance_multiplier = 1.0;
187
188 bool use_bcs = false;
189 float brightness = 1.0;
190 float contrast = 1.0;
191 float saturation = 1.0;
192
193 bool use_color_correction = false;
194 bool use_1d_color_correction = false;
195 RID color_correction_texture;
196
197 bool use_fxaa = false;
198 enum DebandingMode {
199 DEBANDING_MODE_DISABLED,
200 DEBANDING_MODE_8_BIT,
201 DEBANDING_MODE_10_BIT,
202 };
203 DebandingMode debanding_mode = DEBANDING_MODE_DISABLED;
204 Vector2i texture_size;
205 Vector2i dest_texture_size;
206 uint32_t view_count = 1;
207 bool bilinear_filtering = true;
208
209 bool convert_to_srgb = false;
210 };
211
212 void tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings);
213 void tonemapper_mobile(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings);
214 void tonemapper_subpass(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings);
215};
216
217} // namespace RendererRD