1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Implement rendering pipeline and post-processing (#12465)

Co-authored-by: Lars Mueller <appgurulars@gmx.de>
Co-authored-by: sfan5 <sfan5@live.de>
Co-authored-by: lhofhansl <lhofhansl@yahoo.com>
This commit is contained in:
x2048 2022-09-06 08:25:18 +02:00 committed by GitHub
parent 464043b8ab
commit ff6dcfea82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 1476 additions and 565 deletions

View file

@ -19,58 +19,134 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "plain.h"
#include "settings.h"
#include "secondstage.h"
#include "client/camera.h"
#include "client/client.h"
#include "client/clientmap.h"
#include "client/hud.h"
#include "client/minimap.h"
#include "client/shadows/dynamicshadowsrender.h"
inline u32 scaledown(u32 coef, u32 size)
/// Draw3D pipeline step
void Draw3D::run(PipelineContext &context)
{
return (size + coef - 1) / coef;
}
if (m_target)
m_target->activate(context);
RenderingCorePlain::RenderingCorePlain(
IrrlichtDevice *_device, Client *_client, Hud *_hud)
: RenderingCore(_device, _client, _hud)
{
scale = g_settings->getU16("undersampling");
}
void RenderingCorePlain::initTextures()
{
if (scale <= 1)
context.device->getSceneManager()->drawAll();
context.device->getVideoDriver()->setTransform(video::ETS_WORLD, core::IdentityMatrix);
if (!context.show_hud)
return;
v2u32 size{scaledown(scale, screensize.X), scaledown(scale, screensize.Y)};
lowres = driver->addRenderTargetTexture(
size, "render_lowres", video::ECF_A8R8G8B8);
context.hud->drawBlockBounds();
context.hud->drawSelectionMesh();
if (context.draw_wield_tool)
context.client->getCamera()->drawWieldedTool();
}
void RenderingCorePlain::clearTextures()
void DrawHUD::run(PipelineContext &context)
{
if (scale <= 1)
return;
driver->removeTexture(lowres);
if (context.show_hud) {
if (context.shadow_renderer)
context.shadow_renderer->drawDebug();
if (context.draw_crosshair)
context.hud->drawCrosshair();
context.hud->drawHotbar(context.client->getEnv().getLocalPlayer()->getWieldIndex());
context.hud->drawLuaElements(context.client->getCamera()->getOffset());
context.client->getCamera()->drawNametags();
auto mapper = context.client->getMinimap();
if (mapper && context.show_minimap)
mapper->drawMinimap();
}
context.device->getGUIEnvironment()->drawAll();
}
void RenderingCorePlain::beforeDraw()
void MapPostFxStep::setRenderTarget(RenderTarget * _target)
{
if (scale <= 1)
return;
driver->setRenderTarget(lowres, true, true, skycolor);
target = _target;
}
void RenderingCorePlain::upscale()
void MapPostFxStep::run(PipelineContext &context)
{
if (scale <= 1)
return;
driver->setRenderTarget(0, true, true);
v2u32 size{scaledown(scale, screensize.X), scaledown(scale, screensize.Y)};
v2u32 dest_size{scale * size.X, scale * size.Y};
driver->draw2DImage(lowres, core::rect<s32>(0, 0, dest_size.X, dest_size.Y),
core::rect<s32>(0, 0, size.X, size.Y));
if (target)
target->activate(context);
context.client->getEnv().getClientMap().renderPostFx(context.client->getCamera()->getCameraMode());
}
void RenderingCorePlain::drawAll()
void RenderShadowMapStep::run(PipelineContext &context)
{
draw3D();
drawPostFx();
upscale();
drawHUD();
// This is necessary to render shadows for animations correctly
context.device->getSceneManager()->getRootSceneNode()->OnAnimate(context.device->getTimer()->getTime());
context.shadow_renderer->update();
}
// class UpscaleStep
void UpscaleStep::run(PipelineContext &context)
{
video::ITexture *lowres = m_source->getTexture(0);
m_target->activate(context);
context.device->getVideoDriver()->draw2DImage(lowres,
core::rect<s32>(0, 0, context.target_size.X, context.target_size.Y),
core::rect<s32>(0, 0, lowres->getSize().Width, lowres->getSize().Height));
}
std::unique_ptr<RenderStep> create3DStage(Client *client, v2f scale)
{
RenderStep *step = new Draw3D();
if (g_settings->getBool("enable_shaders")) {
RenderPipeline *pipeline = new RenderPipeline();
pipeline->addStep(pipeline->own(std::unique_ptr<RenderStep>(step)));
auto effect = addPostProcessing(pipeline, step, scale, client);
effect->setRenderTarget(pipeline->getOutput());
step = pipeline;
}
return std::unique_ptr<RenderStep>(step);
}
static v2f getDownscaleFactor()
{
u16 undersampling = MYMAX(g_settings->getU16("undersampling"), 1);
return v2f(1.0f / undersampling);
}
RenderStep* addUpscaling(RenderPipeline *pipeline, RenderStep *previousStep, v2f downscale_factor)
{
const int TEXTURE_UPSCALE = 0;
if (downscale_factor.X == 1.0f && downscale_factor.Y == 1.0f)
return previousStep;
// Initialize buffer
TextureBuffer *buffer = pipeline->createOwned<TextureBuffer>();
buffer->setTexture(TEXTURE_UPSCALE, downscale_factor, "upscale", video::ECF_A8R8G8B8);
// Attach previous step to the buffer
TextureBufferOutput *buffer_output = pipeline->createOwned<TextureBufferOutput>(buffer, TEXTURE_UPSCALE);
previousStep->setRenderTarget(buffer_output);
// Add upscaling step
RenderStep *upscale = pipeline->createOwned<UpscaleStep>();
upscale->setRenderSource(buffer);
pipeline->addStep(upscale);
return upscale;
}
void populatePlainPipeline(RenderPipeline *pipeline, Client *client)
{
auto downscale_factor = getDownscaleFactor();
auto step3D = pipeline->own(create3DStage(client, downscale_factor));
pipeline->addStep(step3D);
pipeline->addStep<MapPostFxStep>();
step3D = addUpscaling(pipeline, step3D, downscale_factor);
step3D->setRenderTarget(pipeline->createOwned<ScreenTarget>());
pipeline->addStep<DrawHUD>();
}