| 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 | #ifndef TONE_MAPPER_RD_H |
| 32 | #define TONE_MAPPER_RD_H |
| 33 | |
| 34 | #include "servers/rendering/renderer_rd/pipeline_cache_rd.h" |
| 35 | #include "servers/rendering/renderer_rd/shaders/effects/tonemap.glsl.gen.h" |
| 36 | #include "servers/rendering/renderer_scene_render.h" |
| 37 | |
| 38 | #include "servers/rendering_server.h" |
| 39 | |
| 40 | namespace RendererRD { |
| 41 | |
| 42 | class ToneMapper { |
| 43 | private: |
| 44 | enum TonemapMode { |
| 45 | TONEMAP_MODE_NORMAL, |
| 46 | TONEMAP_MODE_BICUBIC_GLOW_FILTER, |
| 47 | TONEMAP_MODE_1D_LUT, |
| 48 | TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT, |
| 49 | TONEMAP_MODE_SUBPASS, |
| 50 | TONEMAP_MODE_SUBPASS_1D_LUT, |
| 51 | |
| 52 | TONEMAP_MODE_NORMAL_MULTIVIEW, |
| 53 | TONEMAP_MODE_BICUBIC_GLOW_FILTER_MULTIVIEW, |
| 54 | TONEMAP_MODE_1D_LUT_MULTIVIEW, |
| 55 | TONEMAP_MODE_BICUBIC_GLOW_FILTER_1D_LUT_MULTIVIEW, |
| 56 | TONEMAP_MODE_SUBPASS_MULTIVIEW, |
| 57 | TONEMAP_MODE_SUBPASS_1D_LUT_MULTIVIEW, |
| 58 | |
| 59 | TONEMAP_MODE_MAX |
| 60 | }; |
| 61 | |
| 62 | enum { |
| 63 | TONEMAP_FLAG_USE_BCS = (1 << 0), |
| 64 | TONEMAP_FLAG_USE_GLOW = (1 << 1), |
| 65 | TONEMAP_FLAG_USE_AUTO_EXPOSURE = (1 << 2), |
| 66 | TONEMAP_FLAG_USE_COLOR_CORRECTION = (1 << 3), |
| 67 | TONEMAP_FLAG_USE_FXAA = (1 << 4), |
| 68 | TONEMAP_FLAG_USE_DEBANDING = (1 << 5), |
| 69 | TONEMAP_FLAG_CONVERT_TO_SRGB = (1 << 6), |
| 70 | }; |
| 71 | |
| 72 | struct TonemapPushConstant { |
| 73 | float bcs[3]; // 12 - 12 |
| 74 | uint32_t flags; // 4 - 16 |
| 75 | |
| 76 | float pixel_size[2]; // 8 - 24 |
| 77 | uint32_t tonemapper; // 4 - 28 |
| 78 | uint32_t pad; // 4 - 32 |
| 79 | |
| 80 | uint32_t glow_texture_size[2]; // 8 - 40 |
| 81 | float glow_intensity; // 4 - 44 |
| 82 | float glow_map_strength; // 4 - 48 |
| 83 | |
| 84 | uint32_t glow_mode; // 4 - 52 |
| 85 | float glow_levels[7]; // 28 - 80 |
| 86 | |
| 87 | float exposure; // 4 - 84 |
| 88 | float white; // 4 - 88 |
| 89 | float auto_exposure_scale; // 4 - 92 |
| 90 | float luminance_multiplier; // 4 - 96 |
| 91 | }; |
| 92 | |
| 93 | /* tonemap actually writes to a framebuffer, which is |
| 94 | * better to do using the raster pipeline rather than |
| 95 | * compute, as that framebuffer might be in different formats |
| 96 | */ |
| 97 | struct Tonemap { |
| 98 | TonemapPushConstant push_constant; |
| 99 | TonemapShaderRD shader; |
| 100 | RID shader_version; |
| 101 | PipelineCacheRD pipelines[TONEMAP_MODE_MAX]; |
| 102 | } tonemap; |
| 103 | |
| 104 | public: |
| 105 | ToneMapper(); |
| 106 | ~ToneMapper(); |
| 107 | |
| 108 | struct TonemapSettings { |
| 109 | bool use_glow = false; |
| 110 | enum GlowMode { |
| 111 | GLOW_MODE_ADD, |
| 112 | GLOW_MODE_SCREEN, |
| 113 | GLOW_MODE_SOFTLIGHT, |
| 114 | GLOW_MODE_REPLACE, |
| 115 | GLOW_MODE_MIX |
| 116 | }; |
| 117 | |
| 118 | GlowMode glow_mode = GLOW_MODE_ADD; |
| 119 | float glow_intensity = 1.0; |
| 120 | float glow_map_strength = 0.0f; |
| 121 | float glow_levels[7] = { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0 }; |
| 122 | Vector2i glow_texture_size; |
| 123 | bool glow_use_bicubic_upscale = false; |
| 124 | RID glow_texture; |
| 125 | RID glow_map; |
| 126 | |
| 127 | RS::EnvironmentToneMapper tonemap_mode = RS::ENV_TONE_MAPPER_LINEAR; |
| 128 | float exposure = 1.0; |
| 129 | float white = 1.0; |
| 130 | |
| 131 | bool use_auto_exposure = false; |
| 132 | float auto_exposure_scale = 0.5; |
| 133 | RID exposure_texture; |
| 134 | float luminance_multiplier = 1.0; |
| 135 | |
| 136 | bool use_bcs = false; |
| 137 | float brightness = 1.0; |
| 138 | float contrast = 1.0; |
| 139 | float saturation = 1.0; |
| 140 | |
| 141 | bool use_color_correction = false; |
| 142 | bool use_1d_color_correction = false; |
| 143 | RID color_correction_texture; |
| 144 | |
| 145 | bool use_fxaa = false; |
| 146 | bool use_debanding = false; |
| 147 | Vector2i texture_size; |
| 148 | uint32_t view_count = 1; |
| 149 | |
| 150 | bool convert_to_srgb = false; |
| 151 | }; |
| 152 | |
| 153 | void tonemapper(RID p_source_color, RID p_dst_framebuffer, const TonemapSettings &p_settings); |
| 154 | void tonemapper(RD::DrawListID p_subpass_draw_list, RID p_source_color, RD::FramebufferFormatID p_dst_format_id, const TonemapSettings &p_settings); |
| 155 | }; |
| 156 | |
| 157 | } // namespace RendererRD |
| 158 | |
| 159 | #endif // TONE_MAPPER_RD_H |
| 160 | |