Skip to content

Commit

Permalink
Implement rendering pipeline and post-processing (minetest#12465)
Browse files Browse the repository at this point in the history
Co-authored-by: Lars Mueller <[email protected]>
Co-authored-by: sfan5 <[email protected]>
Co-authored-by: lhofhansl <[email protected]>
  • Loading branch information
4 people committed Sep 6, 2022
1 parent 464043b commit ff6dcfe
Show file tree
Hide file tree
Showing 32 changed files with 1,467 additions and 556 deletions.
2 changes: 1 addition & 1 deletion client/shaders/default_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ varying lowp vec4 varColor;

void main(void)
{
gl_FragColor = varColor;
gl_FragData[0] = varColor;
}
43 changes: 4 additions & 39 deletions client/shaders/nodes_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ centroid varying vec2 varTexCoord;
#endif
varying vec3 eyeVec;
varying float nightRatio;
varying vec3 tsEyeVec;
varying vec3 lightVec;
varying vec3 tsLightVec;

const float fogStart = FOG_START;
const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
Expand Down Expand Up @@ -359,40 +362,6 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
#endif
#endif

#if ENABLE_TONE_MAPPING

/* Hable's UC2 Tone mapping parameters
A = 0.22;
B = 0.30;
C = 0.10;
D = 0.20;
E = 0.01;
F = 0.30;
W = 11.2;
equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
*/

vec3 uncharted2Tonemap(vec3 x)
{
return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
}

vec4 applyToneMapping(vec4 color)
{
color = vec4(pow(color.rgb, vec3(2.2)), color.a);
const float gamma = 1.6;
const float exposureBias = 5.5;
color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
// Precalculated white_scale from
//vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
vec3 whiteScale = vec3(1.036015346);
color.rgb *= whiteScale;
return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
}
#endif



void main(void)
{
vec3 color;
Expand Down Expand Up @@ -470,10 +439,6 @@ void main(void)
}
#endif

#if ENABLE_TONE_MAPPING
col = applyToneMapping(col);
#endif

// Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
// the fog will only be rendered correctly if the last operation before the
// clamp() is an addition. Else, the clamp() seems to be ignored.
Expand All @@ -488,5 +453,5 @@ void main(void)
col = mix(skyBgColor, col, clarity);
col = vec4(col.rgb, base.a);

gl_FragColor = col;
gl_FragData[0] = col;
}
4 changes: 4 additions & 0 deletions client/shaders/nodes_shader/opengl_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ centroid varying vec2 varTexCoord;
varying float perspective_factor;
#endif

varying float area_enable_parallax;

varying vec3 eyeVec;
varying float nightRatio;
Expand Down Expand Up @@ -193,6 +194,9 @@ void main(void)

vPosition = gl_Position.xyz;
eyeVec = -(mWorldView * pos).xyz;
#ifdef SECONDSTAGE
normalPass = normalize((inVertexNormal+1)/2);
#endif
vNormal = inVertexNormal;

// Calculate color.
Expand Down
39 changes: 1 addition & 38 deletions client/shaders/object_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -361,39 +361,6 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
#endif
#endif

#if ENABLE_TONE_MAPPING

/* Hable's UC2 Tone mapping parameters
A = 0.22;
B = 0.30;
C = 0.10;
D = 0.20;
E = 0.01;
F = 0.30;
W = 11.2;
equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
*/

vec3 uncharted2Tonemap(vec3 x)
{
return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
}

vec4 applyToneMapping(vec4 color)
{
color = vec4(pow(color.rgb, vec3(2.2)), color.a);
const float gamma = 1.6;
const float exposureBias = 5.5;
color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
// Precalculated white_scale from
//vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
vec3 whiteScale = vec3(1.036015346);
color.rgb *= whiteScale;
return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
}
#endif



void main(void)
{
Expand Down Expand Up @@ -473,10 +440,6 @@ void main(void)
}
#endif

#if ENABLE_TONE_MAPPING
col = applyToneMapping(col);
#endif

// Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
// the fog will only be rendered correctly if the last operation before the
// clamp() is an addition. Else, the clamp() seems to be ignored.
Expand All @@ -491,5 +454,5 @@ void main(void)
col = mix(skyBgColor, col, clarity);
col = vec4(col.rgb, base.a);

gl_FragColor = col;
gl_FragData[0] = col;
}
53 changes: 53 additions & 0 deletions client/shaders/second_stage/opengl_fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
uniform sampler2D baseTexture;

#define rendered baseTexture

#ifdef GL_ES
varying mediump vec2 varTexCoord;
#else
centroid varying vec2 varTexCoord;
#endif

#if ENABLE_TONE_MAPPING

/* Hable's UC2 Tone mapping parameters
A = 0.22;
B = 0.30;
C = 0.10;
D = 0.20;
E = 0.01;
F = 0.30;
W = 11.2;
equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
*/

vec3 uncharted2Tonemap(vec3 x)
{
return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
}

vec4 applyToneMapping(vec4 color)
{
color = vec4(pow(color.rgb, vec3(2.2)), color.a);
const float gamma = 1.6;
const float exposureBias = 5.5;
color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
// Precalculated white_scale from
//vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
vec3 whiteScale = vec3(1.036015346);
color.rgb *= whiteScale;
return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
}
#endif

void main(void)
{
vec2 uv = varTexCoord.st;
vec4 color = texture2D(rendered, uv).rgba;

#if ENABLE_TONE_MAPPING
color = applyToneMapping(color);
#endif

gl_FragColor = vec4(color.rgb, 1.0); // force full alpha to avoid holes in the image.
}
11 changes: 11 additions & 0 deletions client/shaders/second_stage/opengl_vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifdef GL_ES
varying mediump vec2 varTexCoord;
#else
centroid varying vec2 varTexCoord;
#endif

void main(void)
{
varTexCoord.st = inTexCoord0.st;
gl_Position = inVertexPosition;
}
2 changes: 1 addition & 1 deletion client/shaders/selection_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ void main(void)
vec2 uv = varTexCoord.st;
vec4 color = texture2D(baseTexture, uv);
color.rgb *= varColor.rgb;
gl_FragColor = color;
gl_FragData[0] = color;
}
3 changes: 2 additions & 1 deletion src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ set(client_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/render/core.cpp
${CMAKE_CURRENT_SOURCE_DIR}/render/factory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/render/interlaced.cpp
${CMAKE_CURRENT_SOURCE_DIR}/render/pageflip.cpp
${CMAKE_CURRENT_SOURCE_DIR}/render/plain.cpp
${CMAKE_CURRENT_SOURCE_DIR}/render/sidebyside.cpp
${CMAKE_CURRENT_SOURCE_DIR}/render/stereo.cpp
${CMAKE_CURRENT_SOURCE_DIR}/render/secondstage.cpp
${CMAKE_CURRENT_SOURCE_DIR}/render/pipeline.cpp
${CMAKE_CURRENT_SOURCE_DIR}/activeobjectmgr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/camera.cpp
${CMAKE_CURRENT_SOURCE_DIR}/client.cpp
Expand Down
5 changes: 1 addition & 4 deletions src/client/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,14 +627,11 @@ void Camera::wield(const ItemStack &item)

void Camera::drawWieldedTool(irr::core::matrix4* translation)
{
// Clear Z buffer so that the wielded tool stays in front of world geometry
m_wieldmgr->getVideoDriver()->clearBuffers(video::ECBF_DEPTH);

// Draw the wielded node (in a separate scene manager)
scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
cam->setAspectRatio(m_cameranode->getAspectRatio());
cam->setFOV(72.0*M_PI/180.0);
cam->setNearValue(10);
cam->setNearValue(40); // give wield tool smaller z-depth than the world in most cases.
cam->setFarValue(1000);
if (translation != NULL)
{
Expand Down
2 changes: 1 addition & 1 deletion src/client/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect,
bool selected)
{
if (selected) {
/* draw hihlighting around selected item */
/* draw highlighting around selected item */
if (use_hotbar_selected_image) {
core::rect<s32> imgrect2 = rect;
imgrect2.UpperLeftCorner.X -= (m_padding*2);
Expand Down
69 changes: 54 additions & 15 deletions src/client/render/anaglyph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/

#include "anaglyph.h"
#include "client/camera.h"

void RenderingCoreAnaglyph::drawAll()
{
renderBothImages();
drawPostFx();
drawHUD();
}

void RenderingCoreAnaglyph::setupMaterial(int color_mask)
/// SetColorMaskStep step

SetColorMaskStep::SetColorMaskStep(int _color_mask)
: color_mask(_color_mask)
{}

void SetColorMaskStep::run(PipelineContext &context)
{
video::SOverrideMaterial &mat = driver->getOverrideMaterial();
video::SOverrideMaterial &mat = context.device->getVideoDriver()->getOverrideMaterial();
mat.reset();
mat.Material.ColorMask = color_mask;
mat.EnableFlags = video::EMF_COLOR_MASK;
Expand All @@ -38,15 +39,53 @@ void RenderingCoreAnaglyph::setupMaterial(int color_mask)
scene::ESNRP_SHADOW;
}

void RenderingCoreAnaglyph::useEye(bool right)
/// ClearDepthBufferTarget

ClearDepthBufferTarget::ClearDepthBufferTarget(RenderTarget *_target) :
target(_target)
{}

void ClearDepthBufferTarget::activate(PipelineContext &context)
{
target->activate(context);
context.device->getVideoDriver()->clearBuffers(video::ECBF_DEPTH);
}

ConfigureOverrideMaterialTarget::ConfigureOverrideMaterialTarget(RenderTarget *_upstream, bool _enable) :
upstream(_upstream), enable(_enable)
{
}

void ConfigureOverrideMaterialTarget::activate(PipelineContext &context)
{
RenderingCoreStereo::useEye(right);
driver->clearBuffers(video::ECBF_DEPTH);
setupMaterial(right ? video::ECP_GREEN | video::ECP_BLUE : video::ECP_RED);
upstream->activate(context);
context.device->getVideoDriver()->getOverrideMaterial().Enabled = enable;
}

void RenderingCoreAnaglyph::resetEye()

void populateAnaglyphPipeline(RenderPipeline *pipeline, Client *client)
{
setupMaterial(video::ECP_ALL);
RenderingCoreStereo::resetEye();
// clear depth buffer every time 3D is rendered
auto step3D = pipeline->own(create3DStage(client, v2f(1.0)));
auto screen = pipeline->createOwned<ScreenTarget>();
auto clear_depth = pipeline->createOwned<ClearDepthBufferTarget>(screen);
auto enable_override_material = pipeline->createOwned<ConfigureOverrideMaterialTarget>(clear_depth, true);
step3D->setRenderTarget(enable_override_material);

// left eye
pipeline->addStep(pipeline->createOwned<OffsetCameraStep>(false));
pipeline->addStep(pipeline->createOwned<SetColorMaskStep>(video::ECP_RED));
pipeline->addStep(step3D);

// right eye
pipeline->addStep(pipeline->createOwned<OffsetCameraStep>(true));
pipeline->addStep(pipeline->createOwned<SetColorMaskStep>(video::ECP_GREEN | video::ECP_BLUE));
pipeline->addStep(step3D);

// reset
pipeline->addStep(pipeline->createOwned<OffsetCameraStep>(0.0f));
pipeline->addStep(pipeline->createOwned<SetColorMaskStep>(video::ECP_ALL));

pipeline->addStep(pipeline->createOwned<MapPostFxStep>());
pipeline->addStep(pipeline->createOwned<DrawHUD>());
}
Loading

0 comments on commit ff6dcfe

Please sign in to comment.