Skip to content

Commit

Permalink
Require bloom to be enable, scale godrays with bloom, change API to s…
Browse files Browse the repository at this point in the history
…et_lighting
  • Loading branch information
lhofhansl committed Nov 3, 2023
1 parent 0a94eef commit 4d19ffc
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 68 deletions.
4 changes: 1 addition & 3 deletions builtin/settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,9 @@ bloom_strength_factor (Bloom Strength Factor) float 1.0 0.1 10.0
# Requires: shaders, enable_bloom
bloom_radius (Bloom Radius) float 1 0.1 8

[**Volumetric Lighting]

# Set to true to enable volumetric lighting effect (a.k.a. "Godrays").
#
# Requires: shaders_support
# Requires: shaders_support, enable_bloom
enable_volumetric_lighting (Volumetric lighting) bool false

[*Audio]
Expand Down
21 changes: 0 additions & 21 deletions client/shaders/second_stage/opengl_fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
#define rendered texture0
#define bloom texture1
#define volume texture3

struct ExposureParams {
float compensationFactor;
};

uniform sampler2D rendered;
uniform sampler2D bloom;
uniform sampler2D volume;

uniform vec2 texelSize0;

uniform ExposureParams exposureParams;
uniform lowp float bloomIntensity;
uniform lowp float saturation;

#ifdef VOLUMETRIC_LIGHT
uniform lowp float volumetricLightStrength;
#endif

#ifdef GL_ES
varying mediump vec2 varTexCoord;
#else
Expand Down Expand Up @@ -47,17 +41,6 @@ vec4 applyBloom(vec4 color, vec2 uv)

#endif

#ifdef VOLUMETRIC_LIGHT

vec4 applyVolumetricLight(vec4 color, vec2 uv)
{
vec3 light = texture2D(volume, uv).rgb;
color.rgb = mix(color.rgb, light, volumetricLightStrength);
return color;
}

#endif

#if ENABLE_TONE_MAPPING

/* Hable's UC2 Tone mapping parameters
Expand Down Expand Up @@ -122,10 +105,6 @@ void main(void)
#endif
}

#ifdef VOLUMETRIC_LIGHT
color = applyVolumetricLight(color, uv);
#endif

#ifdef ENABLE_BLOOM
color = applyBloom(color, uv);
#endif
Expand Down
11 changes: 9 additions & 2 deletions client/shaders/volumetric_light/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ uniform float sunBrightness;
uniform vec3 moonPositionScreen;
uniform float moonBrightness;

uniform lowp float volumetricLightStrength;

uniform vec3 dayLight;
#ifdef ENABLE_DYNAMIC_SHADOWS
uniform vec3 v_LightDirection;
Expand Down Expand Up @@ -88,6 +90,9 @@ vec3 applyVolumetricLight(vec3 color, vec2 uv, float rawDepth)

color = mix(color, boost * getDirectLightScatteringAtGround(v_LightDirection) * dayLight, lightFactor);

// a factor of 5 tested well
color *= volumetricLightStrength * 5.0;

// if (sunPositionScreen.z < 0.)
// color.rg += 1. - clamp(abs((2. * uv.xy - 1.) - sunPositionScreen.xy / sunPositionScreen.z) * 1000., 0., 1.);
// if (moonPositionScreen.z < 0.)
Expand All @@ -102,9 +107,11 @@ void main(void)
// translate to linear colorspace (approximate)
color = pow(color, vec3(2.2));

float rawDepth = texture2D(depthmap, uv).r;
if (volumetricLightStrength > 0.0) {
float rawDepth = texture2D(depthmap, uv).r;

color = applyVolumetricLight(color, uv, rawDepth);
color = applyVolumetricLight(color, uv, rawDepth);
}

gl_FragColor = vec4(color, 1.0); // force full alpha to avoid holes in the image.
}
8 changes: 3 additions & 5 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7871,11 +7871,6 @@ child will follow movement and rotation of that bone.
Any value between [0.0, 0.99] set the fog_start as a fraction of the viewing_range.
Any value < 0, resets the behavior to being client-controlled.
(default: -1)
* `volumetric_light_strength`: float, set the strength of the client's volumetric light (a.k.a. "godrays") effect.
Any value between [0.0, 1.0] sets the strength of the effect.
A value of 0 disables the effect.
This value has no effect on clients who have the "Volumetric Lighting" shader disabled.
(default: 0)
* `set_sky(base_color, type, {texture names}, clouds)`
* Deprecated. Use `set_sky(sky_parameters)`
* `base_color`: ColorSpec, defaults to white
Expand Down Expand Up @@ -8008,6 +8003,9 @@ child will follow movement and rotation of that bone.
* `speed_dark_bright` set the speed of adapting to bright light (default: `1000.0`)
* `speed_bright_dark` set the speed of adapting to dark scene (default: `1000.0`)
* `center_weight_power` set the power factor for center-weighted luminance measurement (default: `1.0`)
* `volumetric_light`: is a table that controls volumetric light (a.k.a. "godrays")
* `strength`: sets the strength of the volumetric light effect from 0 (off, default) to 1 (strongest)
* This value has no effect on clients who have the "Volumetric Lighting" or "Bloome" shaders disabled.

* `get_lighting()`: returns the current state of lighting for the player.
* Result is a table with the same fields as `light_definition` in `set_lighting`.
Expand Down
4 changes: 1 addition & 3 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
float moon_brightness = 0.f;
m_moon_brightness_pixel.set(&moon_brightness, services);
}
float volumetric_light_strength = m_sky->getVolumetricLightStrength();
float volumetric_light_strength = m_client->getEnv().getLocalPlayer()->getLighting().volumetric_light_strength;
m_volumetric_light_strength_pixel.set(&volumetric_light_strength, services);
}
}
Expand Down Expand Up @@ -3095,8 +3095,6 @@ void Game::handleClientEvent_SetSky(ClientEvent *event, CameraOrientation *cam)
else
sky->setFogStart(rangelim(g_settings->getFloat("fog_start"), 0.0f, 0.99f));

sky->setVolumetricLightStrength(rangelim(event->set_sky->volumetric_light_strength, 0.0f, 1.0f));

delete event->set_sky;
}

Expand Down
45 changes: 22 additions & 23 deletions src/client/render/secondstage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ RenderStep *addPostProcessing(RenderPipeline *pipeline, RenderStep *previousStep
static const u8 TEXTURE_EXPOSURE_2 = 4;
static const u8 TEXTURE_FXAA = 5;
static const u8 TEXTURE_VOLUME = 6;
static const u8 TEXTURE_BLOOM_DOWN = 10;
static const u8 TEXTURE_BLOOM_UP = 20;
static const u8 TEXTURE_SCALE_DOWN = 10;
static const u8 TEXTURE_SCALE_UP = 20;

// Super-sampling is simply rendering into a larger texture.
// Downscaling is done by the final step when rendering to the screen.
Expand Down Expand Up @@ -162,9 +162,9 @@ RenderStep *addPostProcessing(RenderPipeline *pipeline, RenderStep *previousStep

v2f downscale = scale * 0.5;
for (u8 i = 0; i < MIPMAP_LEVELS; i++) {
buffer->setTexture(TEXTURE_BLOOM_DOWN + i, downscale, std::string("downsample") + std::to_string(i), color_format);
buffer->setTexture(TEXTURE_SCALE_DOWN + i, downscale, std::string("downsample") + std::to_string(i), color_format);
if (enable_bloom)
buffer->setTexture(TEXTURE_BLOOM_UP + i, downscale, std::string("upsample") + std::to_string(i), color_format);
buffer->setTexture(TEXTURE_SCALE_UP + i, downscale, std::string("upsample") + std::to_string(i), color_format);
downscale *= 0.5;
}

Expand All @@ -173,20 +173,30 @@ RenderStep *addPostProcessing(RenderPipeline *pipeline, RenderStep *previousStep

// get bright spots
u32 shader_id = client->getShaderSource()->getShader("extract_bloom", TILE_MATERIAL_PLAIN, NDT_MESH);
RenderStep *extract_bloom = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { TEXTURE_COLOR, TEXTURE_EXPOSURE_1 });
RenderStep *extract_bloom = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { source, TEXTURE_EXPOSURE_1 });
extract_bloom->setRenderSource(buffer);
extract_bloom->setRenderTarget(pipeline->createOwned<TextureBufferOutput>(buffer, TEXTURE_BLOOM));
source = TEXTURE_BLOOM;
}

if (enable_volumetric_light) {
buffer->setTexture(TEXTURE_VOLUME, scale, "volume", color_format);

shader_id = client->getShaderSource()->getShader("volumetric_light", TILE_MATERIAL_PLAIN, NDT_MESH);
auto volume = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { source, TEXTURE_DEPTH });
volume->setRenderSource(buffer);
volume->setRenderTarget(pipeline->createOwned<TextureBufferOutput>(buffer, TEXTURE_VOLUME));
source = TEXTURE_VOLUME;
}

// downsample
shader_id = client->getShaderSource()->getShader("bloom_downsample", TILE_MATERIAL_PLAIN, NDT_MESH);
for (u8 i = 0; i < MIPMAP_LEVELS; i++) {
auto step = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { source });
step->setRenderSource(buffer);
step->setBilinearFilter(0, true);
step->setRenderTarget(pipeline->createOwned<TextureBufferOutput>(buffer, TEXTURE_BLOOM_DOWN + i));
source = TEXTURE_BLOOM_DOWN + i;
step->setRenderTarget(pipeline->createOwned<TextureBufferOutput>(buffer, TEXTURE_SCALE_DOWN + i));
source = TEXTURE_SCALE_DOWN + i;
}
}

Expand All @@ -195,35 +205,24 @@ RenderStep *addPostProcessing(RenderPipeline *pipeline, RenderStep *previousStep
// upsample
shader_id = client->getShaderSource()->getShader("bloom_upsample", TILE_MATERIAL_PLAIN, NDT_MESH);
for (u8 i = MIPMAP_LEVELS - 1; i > 0; i--) {
auto step = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { u8(TEXTURE_BLOOM_DOWN + i - 1), source });
auto step = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { u8(TEXTURE_SCALE_DOWN + i - 1), source });
step->setRenderSource(buffer);
step->setBilinearFilter(0, true);
step->setBilinearFilter(1, true);
step->setRenderTarget(pipeline->createOwned<TextureBufferOutput>(buffer, u8(TEXTURE_BLOOM_UP + i - 1)));
source = TEXTURE_BLOOM_UP + i - 1;
step->setRenderTarget(pipeline->createOwned<TextureBufferOutput>(buffer, u8(TEXTURE_SCALE_UP + i - 1)));
source = TEXTURE_SCALE_UP + i - 1;
}
}

// Dynamic Exposure pt2
if (enable_auto_exposure) {
shader_id = client->getShaderSource()->getShader("update_exposure", TILE_MATERIAL_PLAIN, NDT_MESH);
auto update_exposure = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { TEXTURE_EXPOSURE_1, u8(TEXTURE_BLOOM_DOWN + MIPMAP_LEVELS - 1) });
auto update_exposure = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { TEXTURE_EXPOSURE_1, u8(TEXTURE_SCALE_DOWN + MIPMAP_LEVELS - 1) });
update_exposure->setBilinearFilter(1, true);
update_exposure->setRenderSource(buffer);
update_exposure->setRenderTarget(pipeline->createOwned<TextureBufferOutput>(buffer, TEXTURE_EXPOSURE_2));
}

if (enable_volumetric_light) {
buffer->setTexture(TEXTURE_VOLUME, scale, "volume", color_format);

shader_id = client->getShaderSource()->getShader("volumetric_light", TILE_MATERIAL_PLAIN, NDT_MESH);
auto volume = pipeline->addStep<PostProcessingStep>(shader_id, std::vector<u8> { TEXTURE_COLOR, TEXTURE_DEPTH });
volume->setRenderSource(buffer);
volume->setBilinearFilter(0, true);
volume->setBilinearFilter(1, true);
volume->setRenderTarget(pipeline->createOwned<TextureBufferOutput>(buffer, TEXTURE_VOLUME));
}

// FXAA
u8 final_stage_source = TEXTURE_COLOR;

Expand All @@ -241,7 +240,7 @@ RenderStep *addPostProcessing(RenderPipeline *pipeline, RenderStep *previousStep

// final merge
shader_id = client->getShaderSource()->getShader("second_stage", TILE_MATERIAL_PLAIN, NDT_MESH);
PostProcessingStep *effect = pipeline->createOwned<PostProcessingStep>(shader_id, std::vector<u8> { final_stage_source, TEXTURE_BLOOM_UP, TEXTURE_EXPOSURE_2, TEXTURE_VOLUME });
PostProcessingStep *effect = pipeline->createOwned<PostProcessingStep>(shader_id, std::vector<u8> { final_stage_source, TEXTURE_SCALE_UP, TEXTURE_EXPOSURE_2 });
pipeline->addStep(effect);
if (enable_ssaa)
effect->setBilinearFilter(0, true);
Expand Down
1 change: 1 addition & 0 deletions src/lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ struct Lighting
AutoExposure exposure;
float shadow_intensity {0.0f};
float saturation {1.0f};
float volumetric_light_strength {0.0f};
};
6 changes: 2 additions & 4 deletions src/network/clientpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1404,10 +1404,6 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt)
*pkt >> skybox.fog_distance >> skybox.fog_start;
}

if (pkt->getRemainingBytes() >= 4) {
*pkt >> skybox.volumetric_light_strength;
}

ClientEvent *event = new ClientEvent();
event->type = CE_SET_SKY;
event->set_sky = new SkyboxParams(skybox);
Expand Down Expand Up @@ -1808,4 +1804,6 @@ void Client::handleCommand_SetLighting(NetworkPacket *pkt)
>> lighting.exposure.speed_bright_dark
>> lighting.exposure.center_weight_power;
}
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.volumetric_light_strength;
}
18 changes: 12 additions & 6 deletions src/script/lua_api/l_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1835,8 +1835,6 @@ int ObjectRef::l_set_sky(lua_State *L)
sky_params.fog_distance = getintfield_default(L, -1, "fog_distance", sky_params.fog_distance);
sky_params.fog_start = getfloatfield_default(L, -1, "fog_start", sky_params.fog_start);
}

sky_params.volumetric_light_strength = getfloatfield_default(L, 2, "volumetric_light_strength", sky_params.volumetric_light_strength);
} else {
// Handle old set_sky calls, and log deprecated:
log_deprecated(L, "Deprecated call to set_sky, please check lua_api.md");
Expand Down Expand Up @@ -1977,9 +1975,6 @@ int ObjectRef::l_get_sky(lua_State *L)
lua_setfield(L, -2, "fog_start");
lua_setfield(L, -2, "fog");

lua_pushnumber(L, skybox_params.volumetric_light_strength);
lua_setfield(L, -2, "volumetric_light_strength");

return 1;
}

Expand Down Expand Up @@ -2368,7 +2363,14 @@ int ObjectRef::l_set_lighting(lua_State *L)
lighting.exposure.center_weight_power = getfloatfield_default(L, -1, "center_weight_power", lighting.exposure.center_weight_power);
}
lua_pop(L, 1); // exposure
}

lua_getfield(L, 2, "volumetric_light");
if (lua_istable(L, -1)) {
getfloatfield(L, -1, "strength", lighting.volumetric_light_strength);
lighting.volumetric_light_strength = rangelim(lighting.volumetric_light_strength, 0.0f, 1.0f);
}
lua_pop(L, 1); // volumetric_light
}

getServer(L)->setLighting(player, lighting);
return 0;
Expand Down Expand Up @@ -2406,6 +2408,10 @@ int ObjectRef::l_get_lighting(lua_State *L)
lua_pushnumber(L, lighting.exposure.center_weight_power);
lua_setfield(L, -2, "center_weight_power");
lua_setfield(L, -2, "exposure");
lua_newtable(L); // "volumetric_light"
lua_pushnumber(L, lighting.volumetric_light_strength);
lua_setfield(L, -2, "strength");
lua_setfield(L, -2, "volumetric_light");
return 1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,6 @@ void Server::SendSetSky(session_t peer_id, const SkyboxParams &params)

pkt << params.body_orbit_tilt;
pkt << params.fog_distance << params.fog_start;
pkt << params.volumetric_light_strength;
}

Send(&pkt);
Expand Down Expand Up @@ -1920,6 +1919,8 @@ void Server::SendSetLighting(session_t peer_id, const Lighting &lighting)
<< lighting.exposure.speed_bright_dark
<< lighting.exposure.center_weight_power;

pkt << lighting.volumetric_light_strength;

Send(&pkt);
}

Expand Down

0 comments on commit 4d19ffc

Please sign in to comment.