Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/effects/tone_mapper.cpp
22517 views
1
/**************************************************************************/
2
/* tone_mapper.cpp */
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
#include "tone_mapper.h"
32
#include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
33
#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
34
#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
35
36
using namespace RendererRD;
37
38
ToneMapper::ToneMapper(bool p_use_mobile_version) {
39
using_mobile_version = p_use_mobile_version;
40
if (using_mobile_version) {
41
// Initialize tonemapper
42
Vector<String> tonemap_modes;
43
tonemap_modes.push_back("\n");
44
tonemap_modes.push_back("\n#define USE_1D_LUT\n");
45
tonemap_modes.push_back("\n#define SUBPASS\n");
46
tonemap_modes.push_back("\n#define SUBPASS\n#define USE_1D_LUT\n");
47
48
// multiview versions of our shaders
49
tonemap_modes.push_back("\n#define USE_MULTIVIEW\n");
50
tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_1D_LUT\n");
51
tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define SUBPASS\n");
52
tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define SUBPASS\n#define USE_1D_LUT\n");
53
54
tonemap_mobile.shader.initialize(tonemap_modes);
55
56
if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
57
tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_NORMAL_MULTIVIEW, false);
58
tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_1D_LUT_MULTIVIEW, false);
59
tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_SUBPASS_MULTIVIEW, false);
60
tonemap_mobile.shader.set_variant_enabled(TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT_MULTIVIEW, false);
61
}
62
63
tonemap_mobile.shader_version = tonemap_mobile.shader.version_create();
64
65
for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
66
if (tonemap_mobile.shader.is_variant_enabled(i)) {
67
tonemap_mobile.pipelines[i].setup(tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
68
} else {
69
tonemap_mobile.pipelines[i].clear();
70
}
71
}
72
73
} else {
74
// Initialize tonemapper
75
Vector<String> tonemap_modes;
76
tonemap_modes.push_back("\n");
77
tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n");
78
tonemap_modes.push_back("\n#define USE_1D_LUT\n");
79
tonemap_modes.push_back("\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
80
81
// multiview versions of our shaders
82
tonemap_modes.push_back("\n#define USE_MULTIVIEW\n");
83
tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n");
84
tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_1D_LUT\n");
85
tonemap_modes.push_back("\n#define USE_MULTIVIEW\n#define USE_GLOW_FILTER_BICUBIC\n#define USE_1D_LUT\n");
86
87
tonemap.shader.initialize(tonemap_modes);
88
89
if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
90
tonemap.shader.set_variant_enabled(TONEMAP_MODE_NORMAL_MULTIVIEW, false);
91
tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW, false);
92
tonemap.shader.set_variant_enabled(TONEMAP_MODE_1D_LUT_MULTIVIEW, false);
93
tonemap.shader.set_variant_enabled(TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW, false);
94
}
95
96
tonemap.shader_version = tonemap.shader.version_create();
97
98
for (int i = 0; i < TONEMAP_MODE_MAX; i++) {
99
if (tonemap.shader.is_variant_enabled(i)) {
100
tonemap.pipelines[i].setup(tonemap.shader.version_get_shader(tonemap.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
101
} else {
102
tonemap.pipelines[i].clear();
103
}
104
}
105
}
106
}
107
108
ToneMapper::~ToneMapper() {
109
if (using_mobile_version) {
110
tonemap_mobile.shader.version_free(tonemap_mobile.shader_version);
111
} else {
112
tonemap.shader.version_free(tonemap.shader_version);
113
}
114
}
115
116
void ToneMapper::tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
117
ERR_FAIL_COND_MSG(using_mobile_version, "Can't use the non mobile version of the tonemapper with the Mobile renderer.");
118
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
119
ERR_FAIL_NULL(uniform_set_cache);
120
MaterialStorage *material_storage = MaterialStorage::get_singleton();
121
ERR_FAIL_NULL(material_storage);
122
123
memset(&tonemap.push_constant, 0, sizeof(TonemapPushConstant));
124
125
tonemap.push_constant.flags |= p_settings.use_bcs ? TONEMAP_FLAG_USE_BCS : 0;
126
tonemap.push_constant.bcs[0] = p_settings.brightness;
127
tonemap.push_constant.bcs[1] = p_settings.contrast;
128
tonemap.push_constant.bcs[2] = p_settings.saturation;
129
130
tonemap.push_constant.flags |= p_settings.use_glow ? TONEMAP_FLAG_USE_GLOW : 0;
131
tonemap.push_constant.glow_intensity = p_settings.glow_intensity;
132
tonemap.push_constant.glow_map_strength = p_settings.glow_map_strength;
133
tonemap.push_constant.glow_levels[0] = p_settings.glow_levels[0]; // clean this up to just pass by pointer or something
134
tonemap.push_constant.glow_levels[1] = p_settings.glow_levels[1];
135
tonemap.push_constant.glow_levels[2] = p_settings.glow_levels[2];
136
tonemap.push_constant.glow_levels[3] = p_settings.glow_levels[3];
137
tonemap.push_constant.glow_levels[4] = p_settings.glow_levels[4];
138
tonemap.push_constant.glow_levels[5] = p_settings.glow_levels[5];
139
tonemap.push_constant.glow_levels[6] = p_settings.glow_levels[6];
140
tonemap.push_constant.glow_texture_size[0] = p_settings.glow_texture_size.x;
141
tonemap.push_constant.glow_texture_size[1] = p_settings.glow_texture_size.y;
142
tonemap.push_constant.glow_mode = p_settings.glow_mode;
143
144
int mode = p_settings.glow_use_bicubic_upscale ? TONEMAP_MODE_BICUBIC_GLOW_FILTER : TONEMAP_MODE_NORMAL;
145
if (p_settings.use_1d_color_correction) {
146
mode += 2;
147
}
148
149
tonemap.push_constant.tonemapper = p_settings.tonemap_mode;
150
tonemap.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0];
151
tonemap.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1];
152
tonemap.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2];
153
tonemap.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3];
154
tonemap.push_constant.flags |= p_settings.use_auto_exposure ? TONEMAP_FLAG_USE_AUTO_EXPOSURE : 0;
155
tonemap.push_constant.exposure = p_settings.exposure;
156
tonemap.push_constant.white = p_settings.white;
157
tonemap.push_constant.auto_exposure_scale = p_settings.auto_exposure_scale;
158
tonemap.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
159
tonemap.push_constant.output_max_value = MAX(p_settings.max_value, 1.0f);
160
161
tonemap.push_constant.flags |= p_settings.use_color_correction ? TONEMAP_FLAG_USE_COLOR_CORRECTION : 0;
162
163
tonemap.push_constant.flags |= p_settings.use_fxaa ? TONEMAP_FLAG_USE_FXAA : 0;
164
if (p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT) {
165
tonemap.push_constant.flags |= TONEMAP_FLAG_USE_8_BIT_DEBANDING;
166
}
167
tonemap.push_constant.pixel_size[0] = 1.0 / p_settings.texture_size.x;
168
tonemap.push_constant.pixel_size[1] = 1.0 / p_settings.texture_size.y;
169
170
tonemap.push_constant.flags |= p_settings.convert_to_srgb ? TONEMAP_FLAG_CONVERT_TO_SRGB : 0;
171
172
if (p_settings.view_count > 1) {
173
// Use USE_MULTIVIEW versions
174
mode += 4;
175
}
176
177
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
178
RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
179
180
RD::Uniform u_source_color(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_color }));
181
182
RD::Uniform u_exposure_texture;
183
u_exposure_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
184
u_exposure_texture.binding = 0;
185
u_exposure_texture.append_id(default_sampler);
186
u_exposure_texture.append_id(p_settings.exposure_texture);
187
188
RD::Uniform u_glow_texture;
189
u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
190
u_glow_texture.binding = 0;
191
u_glow_texture.append_id(default_mipmap_sampler);
192
u_glow_texture.append_id(p_settings.glow_texture);
193
194
RD::Uniform u_glow_map;
195
u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
196
u_glow_map.binding = 1;
197
u_glow_map.append_id(default_mipmap_sampler);
198
u_glow_map.append_id(p_settings.glow_map);
199
200
RD::Uniform u_color_correction_texture;
201
u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
202
u_color_correction_texture.binding = 0;
203
u_color_correction_texture.append_id(default_sampler);
204
u_color_correction_texture.append_id(p_settings.color_correction_texture);
205
206
RID shader = tonemap.shader.version_get_shader(tonemap.shader_version, mode);
207
ERR_FAIL_COND(shader.is_null());
208
209
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer);
210
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer), false, RD::get_singleton()->draw_list_get_current_pass()));
211
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color), 0);
212
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_exposure_texture), 1);
213
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_glow_texture, u_glow_map), 2);
214
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 3, u_color_correction_texture), 3);
215
216
RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap.push_constant, sizeof(TonemapPushConstant));
217
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
218
RD::get_singleton()->draw_list_end();
219
}
220
221
void ToneMapper::tonemapper_mobile(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings) {
222
ERR_FAIL_COND_MSG(!using_mobile_version, "Can't use the mobile version of the tonemapper with the clustered renderer.");
223
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
224
ERR_FAIL_NULL(uniform_set_cache);
225
MaterialStorage *material_storage = MaterialStorage::get_singleton();
226
ERR_FAIL_NULL(material_storage);
227
228
memset(&tonemap_mobile.push_constant, 0, sizeof(TonemapPushConstantMobile));
229
230
tonemap_mobile.push_constant.bcs[0] = p_settings.brightness;
231
tonemap_mobile.push_constant.bcs[1] = p_settings.contrast;
232
tonemap_mobile.push_constant.bcs[2] = p_settings.saturation;
233
234
tonemap_mobile.push_constant.src_pixel_size[0] = 1.0 / p_settings.texture_size.x;
235
tonemap_mobile.push_constant.src_pixel_size[1] = 1.0 / p_settings.texture_size.y;
236
tonemap_mobile.push_constant.dest_pixel_size[0] = 1.0 / p_settings.dest_texture_size.x;
237
tonemap_mobile.push_constant.dest_pixel_size[1] = 1.0 / p_settings.dest_texture_size.y;
238
tonemap_mobile.push_constant.glow_intensity = p_settings.glow_intensity;
239
tonemap_mobile.push_constant.glow_map_strength = p_settings.glow_map_strength;
240
241
tonemap_mobile.push_constant.exposure = p_settings.exposure;
242
tonemap_mobile.push_constant.white = p_settings.white;
243
tonemap_mobile.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
244
tonemap_mobile.push_constant.output_max_value = MAX(p_settings.max_value, 1.0f);
245
246
tonemap_mobile.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0];
247
tonemap_mobile.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1];
248
tonemap_mobile.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2];
249
tonemap_mobile.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3];
250
251
uint32_t spec_constant = 0;
252
spec_constant |= p_settings.use_bcs ? TONEMAP_MOBILE_FLAG_USE_BCS : 0;
253
spec_constant |= p_settings.use_glow ? TONEMAP_MOBILE_FLAG_USE_GLOW : 0;
254
spec_constant |= p_settings.glow_map_strength > 0.01 ? TONEMAP_MOBILE_FLAG_USE_GLOW_MAP : 0;
255
spec_constant |= p_settings.use_color_correction ? TONEMAP_MOBILE_FLAG_USE_COLOR_CORRECTION : 0;
256
spec_constant |= p_settings.use_fxaa ? TONEMAP_MOBILE_FLAG_USE_FXAA : 0;
257
spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT ? TONEMAP_MOBILE_FLAG_USE_8_BIT_DEBANDING : 0;
258
spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_10_BIT ? TONEMAP_MOBILE_FLAG_USE_10_BIT_DEBANDING : 0;
259
spec_constant |= p_settings.convert_to_srgb ? TONEMAP_MOBILE_FLAG_CONVERT_TO_SRGB : 0;
260
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_LINEAR ? TONEMAP_MOBILE_FLAG_TONEMAPPER_LINEAR : 0;
261
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_REINHARD ? TONEMAP_MOBILE_FLAG_TONEMAPPER_REINHARD : 0;
262
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_FILMIC ? TONEMAP_MOBILE_FLAG_TONEMAPPER_FILMIC : 0;
263
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_ACES ? TONEMAP_MOBILE_FLAG_TONEMAPPER_ACES : 0;
264
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_AGX ? TONEMAP_MOBILE_FLAG_TONEMAPPER_AGX : 0;
265
spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_ADDITIVE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_ADD : 0;
266
spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SCREEN ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SCREEN : 0;
267
spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SOFTLIGHT ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SOFTLIGHT : 0;
268
spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_REPLACE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_REPLACE : 0;
269
spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_MIX ? TONEMAP_MOBILE_FLAG_GLOW_MODE_MIX : 0;
270
271
int mode = p_settings.use_1d_color_correction ? TONEMAP_MOBILE_MODE_1D_LUT : TONEMAP_MOBILE_MODE_NORMAL;
272
273
if (p_settings.view_count > 1) {
274
// Use USE_MULTIVIEW versions
275
mode += 4;
276
}
277
278
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
279
RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
280
281
RD::Uniform u_source_color(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_color }));
282
283
RD::Uniform u_glow_texture;
284
u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
285
u_glow_texture.binding = 1;
286
u_glow_texture.append_id(default_mipmap_sampler);
287
u_glow_texture.append_id(p_settings.glow_texture);
288
289
RD::Uniform u_glow_map;
290
u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
291
u_glow_map.binding = 2;
292
u_glow_map.append_id(default_mipmap_sampler);
293
u_glow_map.append_id(p_settings.glow_map);
294
295
RD::Uniform u_color_correction_texture;
296
u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
297
u_color_correction_texture.binding = 3;
298
u_color_correction_texture.append_id(default_sampler);
299
u_color_correction_texture.append_id(p_settings.color_correction_texture);
300
301
RID shader = tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, mode);
302
ERR_FAIL_COND(shader.is_null());
303
304
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer);
305
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, tonemap_mobile.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer), false, RD::get_singleton()->draw_list_get_current_pass(), spec_constant));
306
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color, u_glow_texture, u_glow_map, u_color_correction_texture), 0);
307
RD::get_singleton()->draw_list_set_push_constant(draw_list, &tonemap_mobile.push_constant, sizeof(TonemapPushConstantMobile));
308
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
309
RD::get_singleton()->draw_list_end();
310
}
311
312
void ToneMapper::tonemapper_subpass(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings) {
313
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
314
ERR_FAIL_NULL(uniform_set_cache);
315
MaterialStorage *material_storage = MaterialStorage::get_singleton();
316
ERR_FAIL_NULL(material_storage);
317
318
ERR_FAIL_COND_MSG(p_settings.use_glow, "Glow is not supported when using subpasses.");
319
320
memset(&tonemap_mobile.push_constant, 0, sizeof(TonemapPushConstantMobile));
321
322
tonemap_mobile.push_constant.bcs[0] = p_settings.brightness;
323
tonemap_mobile.push_constant.bcs[1] = p_settings.contrast;
324
tonemap_mobile.push_constant.bcs[2] = p_settings.saturation;
325
326
tonemap_mobile.push_constant.src_pixel_size[0] = 1.0 / p_settings.texture_size.x;
327
tonemap_mobile.push_constant.src_pixel_size[1] = 1.0 / p_settings.texture_size.y;
328
tonemap_mobile.push_constant.glow_intensity = p_settings.glow_intensity;
329
tonemap_mobile.push_constant.glow_map_strength = p_settings.glow_map_strength;
330
331
tonemap_mobile.push_constant.exposure = p_settings.exposure;
332
tonemap_mobile.push_constant.white = p_settings.white;
333
tonemap_mobile.push_constant.luminance_multiplier = p_settings.luminance_multiplier;
334
335
tonemap_mobile.push_constant.tonemapper_params[0] = p_settings.tonemapper_params[0];
336
tonemap_mobile.push_constant.tonemapper_params[1] = p_settings.tonemapper_params[1];
337
tonemap_mobile.push_constant.tonemapper_params[2] = p_settings.tonemapper_params[2];
338
tonemap_mobile.push_constant.tonemapper_params[3] = p_settings.tonemapper_params[3];
339
340
uint32_t spec_constant = TONEMAP_MOBILE_ADRENO_BUG;
341
spec_constant |= p_settings.use_bcs ? TONEMAP_MOBILE_FLAG_USE_BCS : 0;
342
//spec_constant |= p_settings.use_glow ? TONEMAP_MOBILE_FLAG_USE_GLOW : 0;
343
//spec_constant |= p_settings.glow_map_strength > 0.01 ? TONEMAP_MOBILE_FLAG_USE_GLOW_MAP : 0;
344
//spec_constant |= p_settings.use_color_correction ? TONEMAP_MOBILE_FLAG_USE_COLOR_CORRECTION : 0;
345
//spec_constant |= p_settings.use_fxaa ? TONEMAP_MOBILE_FLAG_USE_FXAA : 0;
346
spec_constant |= p_settings.debanding_mode == TonemapSettings::DEBANDING_MODE_8_BIT ? TONEMAP_MOBILE_FLAG_USE_8_BIT_DEBANDING : 0;
347
spec_constant |= p_settings.convert_to_srgb ? TONEMAP_MOBILE_FLAG_CONVERT_TO_SRGB : 0;
348
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_LINEAR ? TONEMAP_MOBILE_FLAG_TONEMAPPER_LINEAR : 0;
349
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_REINHARD ? TONEMAP_MOBILE_FLAG_TONEMAPPER_REINHARD : 0;
350
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_FILMIC ? TONEMAP_MOBILE_FLAG_TONEMAPPER_FILMIC : 0;
351
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_ACES ? TONEMAP_MOBILE_FLAG_TONEMAPPER_ACES : 0;
352
spec_constant |= p_settings.tonemap_mode == RS::ENV_TONE_MAPPER_AGX ? TONEMAP_MOBILE_FLAG_TONEMAPPER_AGX : 0;
353
//spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_ADDITIVE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_ADD : 0;
354
//spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SCREEN ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SCREEN : 0;
355
//spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_SOFTLIGHT ? TONEMAP_MOBILE_FLAG_GLOW_MODE_SOFTLIGHT : 0;
356
//spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_REPLACE ? TONEMAP_MOBILE_FLAG_GLOW_MODE_REPLACE : 0;
357
//spec_constant |= p_settings.glow_mode == RS::ENV_GLOW_BLEND_MODE_MIX ? TONEMAP_MOBILE_FLAG_GLOW_MODE_MIX : 0;
358
359
int mode = p_settings.use_1d_color_correction ? TONEMAP_MOBILE_MODE_SUBPASS_1D_LUT : TONEMAP_MOBILE_MODE_SUBPASS;
360
if (p_settings.view_count > 1) {
361
// Use USE_MULTIVIEW versions
362
mode += 4;
363
}
364
365
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
366
RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
367
368
RD::Uniform u_source_color;
369
u_source_color.uniform_type = RD::UNIFORM_TYPE_INPUT_ATTACHMENT;
370
u_source_color.binding = 0;
371
u_source_color.append_id(p_source_color);
372
373
RD::Uniform u_glow_texture;
374
u_glow_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
375
u_glow_texture.binding = 1;
376
u_glow_texture.append_id(default_mipmap_sampler);
377
u_glow_texture.append_id(p_settings.glow_texture);
378
379
RD::Uniform u_glow_map;
380
u_glow_map.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
381
u_glow_map.binding = 2;
382
u_glow_map.append_id(default_mipmap_sampler);
383
u_glow_map.append_id(p_settings.glow_map);
384
385
RD::Uniform u_color_correction_texture;
386
u_color_correction_texture.uniform_type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
387
u_color_correction_texture.binding = 3;
388
u_color_correction_texture.append_id(default_sampler);
389
u_color_correction_texture.append_id(p_settings.color_correction_texture);
390
391
RID shader = tonemap_mobile.shader.version_get_shader(tonemap_mobile.shader_version, mode);
392
ERR_FAIL_COND(shader.is_null());
393
394
RD::get_singleton()->draw_list_bind_render_pipeline(p_subpass_draw_list, tonemap_mobile.pipelines[mode].get_render_pipeline(RD::INVALID_ID, p_dst_format_id, false, RD::get_singleton()->draw_list_get_current_pass(), spec_constant));
395
RD::get_singleton()->draw_list_bind_uniform_set(p_subpass_draw_list, uniform_set_cache->get_cache(shader, 0, u_source_color, u_glow_texture, u_glow_map, u_color_correction_texture), 0);
396
RD::get_singleton()->draw_list_set_push_constant(p_subpass_draw_list, &tonemap_mobile.push_constant, sizeof(TonemapPushConstantMobile));
397
RD::get_singleton()->draw_list_draw(p_subpass_draw_list, false, 1u, 3u);
398
}
399
400