Merge pull request #91176 from clayjohn/GL-adjustments

Add adjustments and color correction to Compatibility renderer
This commit is contained in:
Rémi Verschelde 2024-04-26 11:08:56 +02:00
commit a07f89bfb2
No known key found for this signature in database
GPG key ID: C3336907360768E1
10 changed files with 106 additions and 419 deletions

View file

@ -87,7 +87,7 @@ void PostEffects::_draw_screen_triangle() {
glBindVertexArray(0);
}
void PostEffects::post_copy(GLuint p_dest_framebuffer, Size2i p_dest_size, GLuint p_source_color, Size2i p_source_size, float p_luminance_multiplier, const Glow::GLOWLEVEL *p_glow_buffers, float p_glow_intensity, uint32_t p_view, bool p_use_multiview) {
void PostEffects::post_copy(GLuint p_dest_framebuffer, Size2i p_dest_size, GLuint p_source_color, Size2i p_source_size, float p_luminance_multiplier, const Glow::GLOWLEVEL *p_glow_buffers, float p_glow_intensity, uint32_t p_view, bool p_use_multiview, uint64_t p_spec_constants) {
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glDisable(GL_BLEND);
@ -96,7 +96,7 @@ void PostEffects::post_copy(GLuint p_dest_framebuffer, Size2i p_dest_size, GLuin
glViewport(0, 0, p_dest_size.x, p_dest_size.y);
PostShaderGLES3::ShaderVariant mode = PostShaderGLES3::MODE_DEFAULT;
uint64_t flags = 0;
uint64_t flags = p_spec_constants;
if (p_use_multiview) {
flags |= PostShaderGLES3::USE_MULTIVIEW;
}

View file

@ -59,7 +59,7 @@ public:
PostEffects();
~PostEffects();
void post_copy(GLuint p_dest_framebuffer, Size2i p_dest_size, GLuint p_source_color, Size2i p_source_size, float p_luminance_multiplier, const Glow::GLOWLEVEL *p_glow_buffers, float p_glow_intensity, uint32_t p_view = 0, bool p_use_multiview = false);
void post_copy(GLuint p_dest_framebuffer, Size2i p_dest_size, GLuint p_source_color, Size2i p_source_size, float p_luminance_multiplier, const Glow::GLOWLEVEL *p_glow_buffers, float p_glow_intensity, uint32_t p_view = 0, bool p_use_multiview = false, uint64_t p_spec_constants = 0);
};
} //namespace GLES3

View file

@ -788,7 +788,6 @@ void RasterizerSceneGLES3::_draw_sky(RID p_env, const Projection &p_projection,
}
if (!p_apply_color_adjustments_in_post) {
spec_constants |= SkyShaderGLES3::APPLY_TONEMAPPING;
// TODO add BCS and color corrections once supported.
}
RS::EnvironmentBG background = environment_get_background(p_env);
@ -2336,9 +2335,18 @@ void RasterizerSceneGLES3::render_scene(const Ref<RenderSceneBuffers> &p_render_
SceneState::TonemapUBO tonemap_ubo;
if (render_data.environment.is_valid()) {
bool use_bcs = environment_get_adjustments_enabled(render_data.environment);
if (use_bcs) {
apply_color_adjustments_in_post = true;
}
tonemap_ubo.exposure = environment_get_exposure(render_data.environment);
tonemap_ubo.white = environment_get_white(render_data.environment);
tonemap_ubo.tonemapper = int32_t(environment_get_tone_mapper(render_data.environment));
tonemap_ubo.brightness = environment_get_adjustments_brightness(render_data.environment);
tonemap_ubo.contrast = environment_get_adjustments_contrast(render_data.environment);
tonemap_ubo.saturation = environment_get_adjustments_saturation(render_data.environment);
}
if (scene_state.tonemap_buffer == 0) {
@ -2558,8 +2566,6 @@ void RasterizerSceneGLES3::render_scene(const Ref<RenderSceneBuffers> &p_render_
if (!apply_color_adjustments_in_post) {
spec_constant_base_flags |= SceneShaderGLES3::APPLY_TONEMAPPING;
// TODO add BCS and Color corrections here once supported.
}
}
// Render Opaque Objects.
@ -2700,6 +2706,29 @@ void RasterizerSceneGLES3::_render_post_processing(const RenderDataGLES3 *p_rend
rb->check_glow_buffers();
}
bool use_bcs = environment_get_adjustments_enabled(p_render_data->environment);
uint64_t bcs_spec_constants = 0;
RID color_correction_texture = environment_get_color_correction(p_render_data->environment);
if (use_bcs && color_correction_texture.is_valid()) {
bcs_spec_constants |= PostShaderGLES3::USE_BCS;
bcs_spec_constants |= PostShaderGLES3::USE_COLOR_CORRECTION;
bool use_1d_lut = environment_get_use_1d_color_correction(p_render_data->environment);
GLenum texture_target = GL_TEXTURE_3D;
if (use_1d_lut) {
bcs_spec_constants |= PostShaderGLES3::USE_1D_LUT;
texture_target = GL_TEXTURE_2D;
}
glActiveTexture(GL_TEXTURE2);
glBindTexture(texture_target, texture_storage->texture_get_texid(color_correction_texture));
glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
if (view_count == 1) {
// Resolve if needed.
if (fbo_msaa_3d != 0 && msaa3d_needs_resolve) {
@ -2735,7 +2764,7 @@ void RasterizerSceneGLES3::_render_post_processing(const RenderDataGLES3 *p_rend
}
// Copy color buffer
post_effects->post_copy(fbo_rt, target_size, color, internal_size, p_render_data->luminance_multiplier, glow_buffers, glow_intensity);
post_effects->post_copy(fbo_rt, target_size, color, internal_size, p_render_data->luminance_multiplier, glow_buffers, glow_intensity, 0, false, bcs_spec_constants);
// Copy depth buffer
glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo_int);
@ -2803,7 +2832,7 @@ void RasterizerSceneGLES3::_render_post_processing(const RenderDataGLES3 *p_rend
glBindFramebuffer(GL_FRAMEBUFFER, fbos[2]);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, write_color, 0, v);
post_effects->post_copy(fbos[2], target_size, source_color, internal_size, p_render_data->luminance_multiplier, glow_buffers, glow_intensity, v, true);
post_effects->post_copy(fbos[2], target_size, source_color, internal_size, p_render_data->luminance_multiplier, glow_buffers, glow_intensity, v, true, bcs_spec_constants);
}
// Copy depth
@ -2824,6 +2853,9 @@ void RasterizerSceneGLES3::_render_post_processing(const RenderDataGLES3 *p_rend
glBindFramebuffer(GL_FRAMEBUFFER, fbo_rt);
glDeleteFramebuffers(3, fbos);
}
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0);
}
template <PassMode p_pass_mode>

View file

@ -434,6 +434,11 @@ private:
float white = 1.0;
int32_t tonemapper = 0;
int32_t pad = 0;
int32_t pad2 = 0;
float brightness = 1.0;
float contrast = 1.0;
float saturation = 1.0;
};
static_assert(sizeof(TonemapUBO) % 16 == 0, "Tonemap UBO size must be a multiple of 16 bytes");

View file

@ -1,13 +1,15 @@
/* clang-format off */
#[modes]
mode_default = #define MODE_DEFAULT
// mode_glow = #define MODE_GLOW
mode_default =
#[specializations]
USE_MULTIVIEW = false
USE_GLOW = false
USE_LUMINANCE_MULTIPLIER = false
USE_BCS = false
USE_COLOR_CORRECTION = false
USE_1D_LUT = false
#[vertex]
layout(location = 0) in vec2 vertex_attrib;
@ -25,6 +27,9 @@ void main() {
#[fragment]
/* clang-format on */
// If we reach this code, we always tonemap.
#define APPLY_TONEMAPPING
#include "../tonemap_inc.glsl"
#ifdef USE_MULTIVIEW
@ -57,6 +62,35 @@ vec4 get_glow_color(vec2 uv) {
}
#endif // USE_GLOW
#ifdef USE_COLOR_CORRECTION
#ifdef USE_1D_LUT
uniform sampler2D source_color_correction; //texunit:2
vec3 apply_color_correction(vec3 color) {
color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
return color;
}
#else
uniform sampler3D source_color_correction; //texunit:2
vec3 apply_color_correction(vec3 color) {
return textureLod(source_color_correction, color, 0.0).rgb;
}
#endif // USE_1D_LUT
#endif // USE_COLOR_CORRECTION
#ifdef USE_BCS
vec3 apply_bcs(vec3 color) {
color = mix(vec3(0.0), color, brightness);
color = mix(vec3(0.5), color, contrast);
color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, saturation);
return color;
}
#endif
in vec2 uv_interp;
layout(location = 0) out vec4 frag_color;
@ -85,11 +119,11 @@ void main() {
color.rgb = linear_to_srgb(color.rgb);
#ifdef USE_BCS
color.rgb = apply_bcs(color.rgb, bcs);
color.rgb = apply_bcs(color.rgb);
#endif
#ifdef USE_COLOR_CORRECTION
color.rgb = apply_color_correction(color.rgb, color_correction);
color.rgb = apply_color_correction(color.rgb);
#endif
frag_color = color;

View file

@ -1535,6 +1535,7 @@ void main() {
if (alpha < alpha_scissor_threshold) {
discard;
}
alpha = 1.0;
#else
#ifdef MODE_RENDER_DEPTH
#ifdef USE_OPAQUE_PREPASS
@ -1934,13 +1935,6 @@ void main() {
#endif
frag_color.rgb = linear_to_srgb(frag_color.rgb);
#ifdef USE_BCS
frag_color.rgb = apply_bcs(frag_color.rgb, bcs);
#endif
#ifdef USE_COLOR_CORRECTION
frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction);
#endif
#else // !BASE_PASS
frag_color = vec4(0.0, 0.0, 0.0, alpha);
#endif // !BASE_PASS
@ -2152,14 +2146,6 @@ void main() {
#endif
additive_light_color = linear_to_srgb(additive_light_color);
#ifdef USE_BCS
additive_light_color = apply_bcs(additive_light_color, bcs);
#endif
#ifdef USE_COLOR_CORRECTION
additive_light_color = apply_color_correction(additive_light_color, color_correction);
#endif
frag_color.rgb += additive_light_color;
#endif // USE_ADDITIVE_LIGHTING

View file

@ -209,14 +209,6 @@ void main() {
#endif
color = linear_to_srgb(color);
#ifdef USE_BCS
color = apply_bcs(color, bcs);
#endif
#ifdef USE_COLOR_CORRECTION
color = apply_color_correction(color, color_correction);
#endif
frag_color.rgb = color * luminance_multiplier;
frag_color.a = alpha;

View file

@ -1,333 +0,0 @@
/* clang-format off */
[vertex]
#ifdef USE_GLES_OVER_GL
#define lowp
#define mediump
#define highp
#else
precision highp float;
precision highp int;
#endif
layout(location = 0) in vec2 vertex_attrib;
/* clang-format on */
layout(location = 4) in vec2 uv_in;
out vec2 uv_interp;
void main() {
gl_Position = vec4(vertex_attrib, 0.0, 1.0);
uv_interp = uv_in;
}
/* clang-format off */
[fragment]
#ifdef USE_GLES_OVER_GL
#define lowp
#define mediump
#define highp
#else
#if defined(USE_HIGHP_PRECISION)
precision highp float;
precision highp int;
#else
precision mediump float;
precision mediump int;
#endif
#endif
in vec2 uv_interp;
/* clang-format on */
layout(location = 0) out vec4 frag_color;
#ifdef USE_MULTIVIEW
uniform highp sampler2DArray source; //texunit:0
#else
uniform highp sampler2D source; //texunit:0
#endif
#if defined(USE_GLOW_LEVEL1) || defined(USE_GLOW_LEVEL2) || defined(USE_GLOW_LEVEL3) || defined(USE_GLOW_LEVEL4) || defined(USE_GLOW_LEVEL5) || defined(USE_GLOW_LEVEL6) || defined(USE_GLOW_LEVEL7)
#define USING_GLOW // only use glow when at least one glow level is selected
#ifdef USE_MULTI_TEXTURE_GLOW
uniform highp sampler2D source_glow1; //texunit:2
uniform highp sampler2D source_glow2; //texunit:3
uniform highp sampler2D source_glow3; //texunit:4
uniform highp sampler2D source_glow4; //texunit:5
uniform highp sampler2D source_glow5; //texunit:6
uniform highp sampler2D source_glow6; //texunit:7
#ifdef USE_GLOW_LEVEL7
uniform highp sampler2D source_glow7; //texunit:8
#endif
#else
uniform highp sampler2D source_glow; //texunit:2
#endif
uniform highp float glow_intensity;
#endif
#ifdef USE_BCS
uniform vec3 bcs;
#endif
#ifdef USE_FXAA
uniform vec2 pixel_size;
#endif
#ifdef USE_COLOR_CORRECTION
uniform sampler2D color_correction; //texunit:1
#endif
#ifdef USE_GLOW_FILTER_BICUBIC
// w0, w1, w2, and w3 are the four cubic B-spline basis functions
float w0(float a) {
return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);
}
float w1(float a) {
return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);
}
float w2(float a) {
return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);
}
float w3(float a) {
return (1.0 / 6.0) * (a * a * a);
}
// g0 and g1 are the two amplitude functions
float g0(float a) {
return w0(a) + w1(a);
}
float g1(float a) {
return w2(a) + w3(a);
}
// h0 and h1 are the two offset functions
float h0(float a) {
return -1.0 + w1(a) / (w0(a) + w1(a));
}
float h1(float a) {
return 1.0 + w3(a) / (w2(a) + w3(a));
}
uniform ivec2 glow_texture_size;
vec4 texture_bicubic(sampler2D tex, vec2 uv, int p_lod) {
float lod = float(p_lod);
vec2 tex_size = vec2(glow_texture_size >> p_lod);
vec2 texel_size = vec2(1.0) / tex_size;
uv = uv * tex_size + vec2(0.5);
vec2 iuv = floor(uv);
vec2 fuv = fract(uv);
float g0x = g0(fuv.x);
float g1x = g1(fuv.x);
float h0x = h0(fuv.x);
float h1x = h1(fuv.x);
float h0y = h0(fuv.y);
float h1y = h1(fuv.y);
vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;
vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;
vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;
vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;
return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) +
(g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod)));
}
#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture_bicubic(m_tex, m_uv, m_lod)
#else //!USE_GLOW_FILTER_BICUBIC
#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod))
#endif //USE_GLOW_FILTER_BICUBIC
vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode
#ifdef USE_GLOW_REPLACE
color = glow;
#endif
#ifdef USE_GLOW_SCREEN
color = max((color + glow) - (color * glow), vec3(0.0));
#endif
#ifdef USE_GLOW_SOFTLIGHT
glow = glow * vec3(0.5) + vec3(0.5);
color.r = (glow.r <= 0.5) ? (color.r - (1.0 - 2.0 * glow.r) * color.r * (1.0 - color.r)) : (((glow.r > 0.5) && (color.r <= 0.25)) ? (color.r + (2.0 * glow.r - 1.0) * (4.0 * color.r * (4.0 * color.r + 1.0) * (color.r - 1.0) + 7.0 * color.r)) : (color.r + (2.0 * glow.r - 1.0) * (sqrt(color.r) - color.r)));
color.g = (glow.g <= 0.5) ? (color.g - (1.0 - 2.0 * glow.g) * color.g * (1.0 - color.g)) : (((glow.g > 0.5) && (color.g <= 0.25)) ? (color.g + (2.0 * glow.g - 1.0) * (4.0 * color.g * (4.0 * color.g + 1.0) * (color.g - 1.0) + 7.0 * color.g)) : (color.g + (2.0 * glow.g - 1.0) * (sqrt(color.g) - color.g)));
color.b = (glow.b <= 0.5) ? (color.b - (1.0 - 2.0 * glow.b) * color.b * (1.0 - color.b)) : (((glow.b > 0.5) && (color.b <= 0.25)) ? (color.b + (2.0 * glow.b - 1.0) * (4.0 * color.b * (4.0 * color.b + 1.0) * (color.b - 1.0) + 7.0 * color.b)) : (color.b + (2.0 * glow.b - 1.0) * (sqrt(color.b) - color.b)));
#endif
#if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive
color += glow;
#endif
return color;
}
vec3 apply_bcs(vec3 color, vec3 bcs) {
color = mix(vec3(0.0), color, bcs.x);
color = mix(vec3(0.5), color, bcs.y);
color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z);
return color;
}
vec3 apply_color_correction(vec3 color, sampler2D correction_tex) {
color.r = texture(correction_tex, vec2(color.r, 0.0)).r;
color.g = texture(correction_tex, vec2(color.g, 0.0)).g;
color.b = texture(correction_tex, vec2(color.b, 0.0)).b;
return color;
}
vec3 apply_fxaa(vec3 color, vec2 uv_interp, vec2 pixel_size) {
const float FXAA_REDUCE_MIN = (1.0 / 128.0);
const float FXAA_REDUCE_MUL = (1.0 / 8.0);
const float FXAA_SPAN_MAX = 8.0;
#ifdef USE_MULTIVIEW
vec3 rgbNW = textureLod(source, vec3(uv_interp + vec2(-1.0, -1.0) * pixel_size, ViewIndex), 0.0).xyz;
vec3 rgbNE = textureLod(source, vec3(uv_interp + vec2(1.0, -1.0) * pixel_size, ViewIndex), 0.0).xyz;
vec3 rgbSW = textureLod(source, vec3(uv_interp + vec2(-1.0, 1.0) * pixel_size, ViewIndex), 0.0).xyz;
vec3 rgbSE = textureLod(source, vec3(uv_interp + vec2(1.0, 1.0) * pixel_size, ViewIndex), 0.0).xyz;
#else
vec3 rgbNW = textureLod(source, uv_interp + vec2(-1.0, -1.0) * pixel_size, 0.0).xyz;
vec3 rgbNE = textureLod(source, uv_interp + vec2(1.0, -1.0) * pixel_size, 0.0).xyz;
vec3 rgbSW = textureLod(source, uv_interp + vec2(-1.0, 1.0) * pixel_size, 0.0).xyz;
vec3 rgbSE = textureLod(source, uv_interp + vec2(1.0, 1.0) * pixel_size, 0.0).xyz;
#endif
vec3 rgbM = color;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot(rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
(0.25 * FXAA_REDUCE_MUL),
FXAA_REDUCE_MIN);
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) *
pixel_size;
#ifdef USE_MULTIVIEW
vec3 rgbA = 0.5 * (textureLod(source, vec3(uv_interp + dir * (1.0 / 3.0 - 0.5), ViewIndex), 0.0).xyz + textureLod(source, vec3(uv_interp + dir * (2.0 / 3.0 - 0.5), ViewIndex), 0.0).xyz);
vec3 rgbB = rgbA * 0.5 + 0.25 * (textureLod(source, vec3(uv_interp + dir * -0.5, ViewIndex), 0.0).xyz + textureLod(source, vec3(uv_interp + dir * 0.5, ViewIndex), 0.0).xyz);
#else
vec3 rgbA = 0.5 * (textureLod(source, uv_interp + dir * (1.0 / 3.0 - 0.5), 0.0).xyz + textureLod(source, uv_interp + dir * (2.0 / 3.0 - 0.5), 0.0).xyz);
vec3 rgbB = rgbA * 0.5 + 0.25 * (textureLod(source, uv_interp + dir * -0.5, 0.0).xyz + textureLod(source, uv_interp + dir * 0.5, 0.0).xyz);
#endif
float lumaB = dot(rgbB, luma);
if ((lumaB < lumaMin) || (lumaB > lumaMax)) {
return rgbA;
} else {
return rgbB;
}
}
void main() {
#ifdef USE_MULTIVIEW
vec4 color = textureLod(source, vec3(uv_interp, ViewIndex), 0.0);
#else
vec4 color = textureLod(source, uv_interp, 0.0);
#endif
#ifdef USE_FXAA
color.rgb = apply_fxaa(color.rgb, uv_interp, pixel_size);
#endif
// Glow
#ifdef USING_GLOW
vec3 glow = vec3(0.0);
#ifdef USE_MULTI_TEXTURE_GLOW
#ifdef USE_GLOW_LEVEL1
glow += GLOW_TEXTURE_SAMPLE(source_glow1, uv_interp, 0).rgb;
#ifdef USE_GLOW_LEVEL2
glow += GLOW_TEXTURE_SAMPLE(source_glow2, uv_interp, 0).rgb;
#ifdef USE_GLOW_LEVEL3
glow += GLOW_TEXTURE_SAMPLE(source_glow3, uv_interp, 0).rgb;
#ifdef USE_GLOW_LEVEL4
glow += GLOW_TEXTURE_SAMPLE(source_glow4, uv_interp, 0).rgb;
#ifdef USE_GLOW_LEVEL5
glow += GLOW_TEXTURE_SAMPLE(source_glow5, uv_interp, 0).rgb;
#ifdef USE_GLOW_LEVEL6
glow += GLOW_TEXTURE_SAMPLE(source_glow6, uv_interp, 0).rgb;
#ifdef USE_GLOW_LEVEL7
glow += GLOW_TEXTURE_SAMPLE(source_glow7, uv_interp, 0).rgb;
#endif
#endif
#endif
#endif
#endif
#endif
#endif
#else
#ifdef USE_GLOW_LEVEL1
glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 1).rgb;
#endif
#ifdef USE_GLOW_LEVEL2
glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 2).rgb;
#endif
#ifdef USE_GLOW_LEVEL3
glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 3).rgb;
#endif
#ifdef USE_GLOW_LEVEL4
glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 4).rgb;
#endif
#ifdef USE_GLOW_LEVEL5
glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 5).rgb;
#endif
#ifdef USE_GLOW_LEVEL6
glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 6).rgb;
#endif
#ifdef USE_GLOW_LEVEL7
glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 7).rgb;
#endif
#endif //USE_MULTI_TEXTURE_GLOW
glow *= glow_intensity;
color.rgb = apply_glow(color.rgb, glow);
#endif
// Additional effects
#ifdef USE_BCS
color.rgb = apply_bcs(color.rgb, bcs);
#endif
#ifdef USE_COLOR_CORRECTION
color.rgb = apply_color_correction(color.rgb, color_correction);
#endif
frag_color = color;
}

View file

@ -1,43 +1,31 @@
#ifdef USE_BCS
uniform vec3 bcs;
#endif
#ifdef USE_COLOR_CORRECTION
#ifdef USE_1D_LUT
uniform sampler2D source_color_correction; //texunit:-1
#else
uniform sampler3D source_color_correction; //texunit:-1
#endif
#endif
layout(std140) uniform TonemapData { //ubo:0
float exposure;
float white;
int tonemapper;
int pad;
int pad2;
float brightness;
float contrast;
float saturation;
};
vec3 apply_bcs(vec3 color, vec3 bcs) {
color = mix(vec3(0.0), color, bcs.x);
color = mix(vec3(0.5), color, bcs.y);
color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z);
// This expects 0-1 range input.
vec3 linear_to_srgb(vec3 color) {
//color = clamp(color, vec3(0.0), vec3(1.0));
//const vec3 a = vec3(0.055f);
//return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f)));
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
}
return color;
// This expects 0-1 range input, outside that range it behaves poorly.
vec3 srgb_to_linear(vec3 color) {
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);
}
#ifdef USE_COLOR_CORRECTION
#ifdef USE_1D_LUT
vec3 apply_color_correction(vec3 color) {
color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r;
color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g;
color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b;
return color;
}
#else
vec3 apply_color_correction(vec3 color) {
return textureLod(source_color_correction, color, 0.0).rgb;
}
#endif
#endif
#ifdef APPLY_TONEMAPPING
vec3 tonemap_filmic(vec3 color, float p_white) {
// exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
@ -92,21 +80,6 @@ vec3 tonemap_reinhard(vec3 color, float p_white) {
return (p_white * color + color) / (color * p_white + p_white);
}
// This expects 0-1 range input.
vec3 linear_to_srgb(vec3 color) {
//color = clamp(color, vec3(0.0), vec3(1.0));
//const vec3 a = vec3(0.055f);
//return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f)));
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
}
// This expects 0-1 range input, outside that range it behaves poorly.
vec3 srgb_to_linear(vec3 color) {
// Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html
return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878);
}
#define TONEMAPPER_LINEAR 0
#define TONEMAPPER_REINHARD 1
#define TONEMAPPER_FILMIC 2
@ -125,3 +98,5 @@ vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always
return tonemap_aces(max(vec3(0.0f), color), p_white);
}
}
#endif // APPLY_TONEMAPPING

View file

@ -773,11 +773,7 @@ RS::EnvironmentSDFGIYScale RendererEnvironmentStorage::environment_get_sdfgi_y_s
void RendererEnvironmentStorage::environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, bool p_use_1d_color_correction, RID p_color_correction) {
Environment *env = environment_owner.get_or_null(p_env);
ERR_FAIL_NULL(env);
#ifdef DEBUG_ENABLED
if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" && p_enable) {
WARN_PRINT_ONCE_ED("Adjustments are not supported when using the GL Compatibility backend yet. Support will be added in a future release.");
}
#endif
env->adjustments_enabled = p_enable;
env->adjustments_brightness = p_brightness;
env->adjustments_contrast = p_contrast;