Adjust render target handling for effects

This commit is contained in:
kaetemi 2014-08-03 22:15:44 +02:00
parent 996dd9aa16
commit 8c6d46bed0
5 changed files with 103 additions and 78 deletions

View file

@ -72,8 +72,8 @@ public:
void setDensityBloom(uint8 densityBloom) { _DensityBloom = densityBloom; } void setDensityBloom(uint8 densityBloom) { _DensityBloom = densityBloom; }
uint8 getDensityBloom() const { return _DensityBloom; } uint8 getDensityBloom() const { return _DensityBloom; }
// Applies bloom to the current render target, if backbuffer is true the final image is rendered to the backbuffer instead of to a render target // Applies bloom to the current render target
void applyBloom(bool backbuffer); void applyBloom();
// Called at the beginning of renderAll method in the main loop, if window has been resized, // Called at the beginning of renderAll method in the main loop, if window has been resized,
// reinitialize bloom textures according to new window size. // reinitialize bloom textures according to new window size.
@ -143,8 +143,6 @@ private:
// materials // materials
// used to display first texture in doBlur passes. // used to display first texture in doBlur passes.
NL3D::UMaterial _BlurMat; NL3D::UMaterial _BlurMat;
// used to display final render target texture onto the backbuffer
NL3D::UMaterial _DisplayInitMat;
// used to blend initial scene render target texture and blur texture according to a // used to blend initial scene render target texture and blur texture according to a
// dest+src - dest*src blend operation. // dest+src - dest*src blend operation.
NL3D::UMaterial _DisplayBlurMat; NL3D::UMaterial _DisplayBlurMat;
@ -154,7 +152,6 @@ private:
// quads // quads
NLMISC::CQuadUV _BlurQuad; NLMISC::CQuadUV _BlurQuad;
NLMISC::CQuadUV _DisplayQuad;
// textures and materials already initialized? // textures and materials already initialized?
bool _Init; bool _Init;

View file

@ -83,11 +83,6 @@ CBloomEffect::~CBloomEffect()
{ {
if (_Init) if (_Init)
{ {
if (!_DisplayInitMat.empty())
{
if (_Driver) _Driver->deleteMaterial(_DisplayInitMat);
}
if (!_DisplayBlurMat.empty()) if (!_DisplayBlurMat.empty())
{ {
if (_Driver) _Driver->deleteMaterial(_DisplayBlurMat); if (_Driver) _Driver->deleteMaterial(_DisplayBlurMat);
@ -160,18 +155,6 @@ void CBloomEffect::init()
matObject->texEnvArg1RGB(3, CMaterial::Constant, CMaterial::SrcColor); matObject->texEnvArg1RGB(3, CMaterial::Constant, CMaterial::SrcColor);
matObject->texEnvArg2RGB(3, CMaterial::Previous, CMaterial::SrcColor); matObject->texEnvArg2RGB(3, CMaterial::Previous, CMaterial::SrcColor);
// initialize display materials
_DisplayInitMat = _Driver->createMaterial();
CMaterial * matObjectInit = _DisplayInitMat.getObjectPtr();
_DisplayInitMat.initUnlit();
_DisplayInitMat.setColor(CRGBA::White);
_DisplayInitMat.setBlend(false);
_DisplayInitMat.setAlphaTest (false);
matObjectInit->setBlendFunc(CMaterial::one, CMaterial::zero);
matObjectInit->setZWrite(false);
matObjectInit->setZFunc(CMaterial::always);
matObjectInit->setDoubleSided(true);
// initialize linear blur material // initialize linear blur material
_DisplayBlurMat = _Driver->createMaterial(); _DisplayBlurMat = _Driver->createMaterial();
CMaterial * matObjectFinal = _DisplayBlurMat.getObjectPtr(); CMaterial * matObjectFinal = _DisplayBlurMat.getObjectPtr();
@ -208,15 +191,6 @@ void CBloomEffect::init()
matObjectFinal->texEnvArg1RGB(1, CMaterial::Previous, CMaterial::SrcColor); matObjectFinal->texEnvArg1RGB(1, CMaterial::Previous, CMaterial::SrcColor);
// initialize quads // initialize quads
_DisplayQuad.V0 = CVector(0.f, 0.f, 0.5f);
_DisplayQuad.V1 = CVector(1.f, 0.f, 0.5f);
_DisplayQuad.V2 = CVector(1.f, 1.f, 0.5f);
_DisplayQuad.V3 = CVector(0.f, 1.f, 0.5f);
_DisplayQuad.Uv0 = CUV(0.f, 0.f);
_DisplayQuad.Uv1 = CUV(1.f, 0.f);
_DisplayQuad.Uv2 = CUV(1.f, 1.f);
_DisplayQuad.Uv3 = CUV(0.f, 1.f);
_BlurQuad.V0 = CVector(-1.f, -1.f, 0.5f); _BlurQuad.V0 = CVector(-1.f, -1.f, 0.5f);
_BlurQuad.V1 = CVector(1.f, -1.f, 0.5f); _BlurQuad.V1 = CVector(1.f, -1.f, 0.5f);
_BlurQuad.V2 = CVector(1.f, 1.f, 0.5f); _BlurQuad.V2 = CVector(1.f, 1.f, 0.5f);
@ -241,7 +215,7 @@ void CBloomEffect::init()
//----------------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------------
void CBloomEffect::applyBloom(bool backbuffer) void CBloomEffect::applyBloom()
{ {
if (!((CDriverUser *)_Driver)->getDriver()->supportBloomEffect()) if (!((CDriverUser *)_Driver)->getDriver()->supportBloomEffect())
return; return;
@ -301,22 +275,7 @@ void CBloomEffect::applyBloom(bool backbuffer)
drv->setRenderTarget(renderTarget); drv->setRenderTarget(renderTarget);
applyBlur(); applyBlur();
// draw final result to backbuffer if last effect
if (backbuffer)
{
CTextureUser texNull;
dru->setRenderTarget(texNull);
_DisplayInitMat.getObjectPtr()->setTexture(0, renderTarget);
UCamera pCam = _Scene->getCam();
_Driver->setMatrixMode2D11();
_Driver->drawQuad(_DisplayQuad, _DisplayInitMat);
_Driver->setMatrixMode3D(pCam);
}
// cleanup material texture references // cleanup material texture references
_DisplayInitMat.getObjectPtr()->setTexture(0, NULL);
_DisplayBlurMat.getObjectPtr()->setTexture(0, NULL); _DisplayBlurMat.getObjectPtr()->setTexture(0, NULL);
_DisplaySquareBlurMat.getObjectPtr()->setTexture(0, NULL); _DisplaySquareBlurMat.getObjectPtr()->setTexture(0, NULL);
_DisplaySquareBlurMat.getObjectPtr()->setTexture(1, NULL); _DisplaySquareBlurMat.getObjectPtr()->setTexture(1, NULL);

View file

@ -40,6 +40,7 @@
#include "nel/3d/u_cloud_scape.h" #include "nel/3d/u_cloud_scape.h"
#include "nel/3d/u_shape_bank.h" #include "nel/3d/u_shape_bank.h"
#include "nel/3d/u_water_env_map.h" #include "nel/3d/u_water_env_map.h"
#include "nel/3d/material.h"
// Sound // Sound
#include "nel/sound/u_audio_mixer.h" #include "nel/sound/u_audio_mixer.h"
// Client // Client
@ -136,6 +137,9 @@ std::string LoadingBitmapFilename;
uint64 StartInitTime = 0; uint64 StartInitTime = 0;
uint64 StartPlayTime = 0; uint64 StartPlayTime = 0;
UMaterial EffectMaterial;
NLMISC::CQuadUV EffectQuad;
// texture for the logos // texture for the logos
std::vector<UTextureFile*> LogoBitmaps; std::vector<UTextureFile*> LogoBitmaps;
@ -568,6 +572,29 @@ void initMainLoop()
// use this scene for bloom effect // use this scene for bloom effect
CBloomEffect::getInstance().setScene(Scene); CBloomEffect::getInstance().setScene(Scene);
if (EffectMaterial.empty())
{
EffectMaterial = Driver->createMaterial();
CMaterial *mat = EffectMaterial.getObjectPtr();
EffectMaterial.initUnlit();
EffectMaterial.setColor(CRGBA::White);
EffectMaterial.setBlend(false);
EffectMaterial.setAlphaTest (false);
mat->setBlendFunc(CMaterial::one, CMaterial::zero);
mat->setZWrite(false);
mat->setZFunc(CMaterial::always);
mat->setDoubleSided(true);
}
EffectQuad.V0 = CVector(0.f, 0.f, 0.5f);
EffectQuad.V1 = CVector(1.f, 0.f, 0.5f);
EffectQuad.V2 = CVector(1.f, 1.f, 0.5f);
EffectQuad.V3 = CVector(0.f, 1.f, 0.5f);
EffectQuad.Uv0 = CUV(0.f, 0.f);
EffectQuad.Uv1 = CUV(1.f, 0.f);
EffectQuad.Uv2 = CUV(1.f, 1.f);
EffectQuad.Uv3 = CUV(0.f, 1.f);
CLandscapePolyDrawer::getInstance().initLandscapePolyDrawingCallback(); CLandscapePolyDrawer::getInstance().initLandscapePolyDrawingCallback();

View file

@ -185,6 +185,9 @@ extern std::vector<UTextureFile*> LogoBitmaps;
extern bool IsInRingSession; extern bool IsInRingSession;
extern std::string UsedFSAddr; extern std::string UsedFSAddr;
extern UMaterial EffectMaterial;
extern NLMISC::CQuadUV EffectQuad;
// temp // temp
extern NLMISC::CValueSmoother smoothFPS; extern NLMISC::CValueSmoother smoothFPS;
extern NLMISC::CValueSmoother moreSmoothFPS; extern NLMISC::CValueSmoother moreSmoothFPS;
@ -569,8 +572,8 @@ void renderScene(bool forceFullDetail, bool bloom)
// set bloom parameters before applying bloom effect // set bloom parameters before applying bloom effect
CBloomEffect::getInstance().setSquareBloom(ClientCfg.SquareBloom); CBloomEffect::getInstance().setSquareBloom(ClientCfg.SquareBloom);
CBloomEffect::getInstance().setDensityBloom((uint8)ClientCfg.DensityBloom); CBloomEffect::getInstance().setDensityBloom((uint8)ClientCfg.DensityBloom);
// init bloom
// CBloomEffect::getInstance().initBloom(); // init effect render target
uint32 winw, winh; uint32 winw, winh;
Driver->getWindowSize(winw, winh); Driver->getWindowSize(winw, winh);
effectRenderTarget = Driver->getRenderTargetManager().getRenderTarget(winw, winh); effectRenderTarget = Driver->getRenderTargetManager().getRenderTarget(winw, winh);
@ -590,9 +593,25 @@ void renderScene(bool forceFullDetail, bool bloom)
if (bloom) if (bloom)
{ {
// apply bloom effect // apply bloom effect
// CBloomEffect::getInstance().endBloom(); CBloomEffect::getInstance().applyBloom();
// CBloomEffect::getInstance().endInterfacesDisplayBloom();
CBloomEffect::getInstance().applyBloom(effectRenderTarget != NULL); // TODO // draw final result to backbuffer
CDriverUser *dru = static_cast<CDriverUser *>(Driver);
CTextureUser texNull;
dru->setRenderTarget(texNull);
EffectMaterial.getObjectPtr()->setTexture(0, effectRenderTarget->getITexture());
UCamera pCam = Scene->getCam();
Driver->setMatrixMode2D11();
Driver->drawQuad(EffectQuad, EffectMaterial);
Driver->setMatrixMode3D(pCam);
EffectMaterial.getObjectPtr()->setTexture(0, NULL);
Driver->getRenderTargetManager().recycleRenderTarget(effectRenderTarget);
effectRenderTarget = NULL;
} }
} }
@ -1629,8 +1648,24 @@ bool mainLoop()
} }
uint i = 0; uint i = 0;
uint bloomStage = 0; bool effectRender = false;
CTextureUser *effectRenderTarget = NULL; CTextureUser *effectRenderTarget = NULL;
bool haveEffects = Render && ClientCfg.Bloom;
if (haveEffects)
{
if (!StereoDisplay)
{
uint32 winw, winh;
Driver->getWindowSize(winw, winh);
effectRenderTarget = Driver->getRenderTargetManager().getRenderTarget(winw, winh);
static_cast<CDriverUser *>(Driver)->setRenderTarget(*effectRenderTarget);
}
if (ClientCfg.Bloom)
{
CBloomEffect::getInstance().setSquareBloom(ClientCfg.SquareBloom);
CBloomEffect::getInstance().setDensityBloom((uint8)ClientCfg.DensityBloom);
}
}
while ((!StereoDisplay && i == 0) || (StereoDisplay && StereoDisplay->nextPass())) while ((!StereoDisplay && i == 0) || (StereoDisplay && StereoDisplay->nextPass()))
{ {
++i; ++i;
@ -1673,22 +1708,7 @@ bool mainLoop()
{ {
if (Render) if (Render)
{ {
if (ClientCfg.Bloom && bloomStage == 0) effectRender = haveEffects;
{
// set bloom parameters before applying bloom effect
CBloomEffect::getInstance().setSquareBloom(ClientCfg.SquareBloom);
CBloomEffect::getInstance().setDensityBloom((uint8)ClientCfg.DensityBloom);
// start bloom effect (just before the first scene element render)
if (!StereoDisplay) // FIXME: Assumes rendering to render target...!
{
uint32 winw, winh;
Driver->getWindowSize(winw, winh);
effectRenderTarget = Driver->getRenderTargetManager().getRenderTarget(winw, winh);
static_cast<CDriverUser *>(Driver)->setRenderTarget(*effectRenderTarget);
}
// CBloomEffect::instance().initBloom();
bloomStage = 1;
}
} }
// Clear buffers // Clear buffers
@ -1729,18 +1749,15 @@ bool mainLoop()
// Render // Render
if (Render) if (Render)
{ {
if (ClientCfg.Bloom && bloomStage == 1) if (effectRender)
{
if (ClientCfg.Bloom)
{ {
// End the actual bloom effect visible in the scene.
if (StereoDisplay) Driver->setViewport(NL3D::CViewport()); if (StereoDisplay) Driver->setViewport(NL3D::CViewport());
CBloomEffect::instance().applyBloom(effectRenderTarget != NULL); CBloomEffect::instance().applyBloom();
if (StereoDisplay) Driver->setViewport(StereoDisplay->getCurrentViewport()); if (StereoDisplay) Driver->setViewport(StereoDisplay->getCurrentViewport());
if (effectRenderTarget)
{
Driver->getRenderTargetManager().recycleRenderTarget(effectRenderTarget);
effectRenderTarget = NULL;
} }
bloomStage = 0; effectRender = false;
} }
// for that frame and // for that frame and
@ -2182,6 +2199,27 @@ bool mainLoop()
} }
} /* stereo pass */ } /* stereo pass */
if (effectRenderTarget)
{
// draw final result to backbuffer
CDriverUser *dru = static_cast<CDriverUser *>(Driver);
CTextureUser texNull;
dru->setRenderTarget(texNull);
EffectMaterial.getObjectPtr()->setTexture(0, effectRenderTarget->getITexture());
UCamera pCam = Scene->getCam();
Driver->setMatrixMode2D11();
Driver->drawQuad(EffectQuad, EffectMaterial);
Driver->setMatrixMode3D(pCam);
EffectMaterial.getObjectPtr()->setTexture(0, NULL);
Driver->getRenderTargetManager().recycleRenderTarget(effectRenderTarget);
effectRenderTarget = NULL;
}
// Draw to screen. // Draw to screen.
static CQuat MainCamOri; static CQuat MainCamOri;
if (FirstFrame) if (FirstFrame)

View file

@ -120,6 +120,7 @@ extern bool userChar;
extern bool serverReceivedReady; extern bool serverReceivedReady;
extern bool CharNameValidArrived; extern bool CharNameValidArrived;
extern UMaterial EffectMaterial;
extern void releaseContextualCursor(); extern void releaseContextualCursor();
extern void selectTipsOfTheDay (uint tips); extern void selectTipsOfTheDay (uint tips);
@ -585,6 +586,9 @@ void release()
// Delete the driver. // Delete the driver.
if(Driver) if(Driver)
{ {
if (!EffectMaterial.empty())
Driver->deleteMaterial(EffectMaterial);
// Release the prim // Release the prim
PrimFiles.release (*Driver); PrimFiles.release (*Driver);