2 * \file render_target_manager.cpp
3 * \brief CRenderTargetManager
4 * \date 2014-07-30 21:30GMT
5 * \author Jan Boon (Kaetemi)
9 // NeL - MMORPG Framework <https://wiki.ryzom.dev/>
10 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
12 // This program is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Affero General Public License as
14 // published by the Free Software Foundation, either version 3 of the
15 // License, or (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Affero General Public License for more details.
22 // You should have received a copy of the GNU Affero General Public License
23 // along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "nel/3d/render_target_manager.h"
32 // #include <nel/misc/debug.h>
33 #include "nel/3d/u_camera.h"
34 #include "nel/3d/u_driver.h"
35 #include "nel/3d/material.h"
36 #include "nel/3d/texture_bloom.h"
37 #include "nel/3d/texture_user.h"
38 #include "nel/3d/driver_user.h"
39 #include "nel/3d/u_texture.h"
44 // using namespace NLMISC;
52 struct CRenderTargetDescInt
59 UTexture::TUploadFormat Format
;
62 NL3D::CTextureUser
*TextureUser
;
63 NLMISC::CSmartPtr
<NL3D::ITexture
> TextureInterface
;
68 CRenderTargetManager::CRenderTargetManager() : m_Driver(NULL
)
73 CRenderTargetManager::~CRenderTargetManager()
75 // Call twice to reset counters and cleanup
80 NL3D::CTextureUser
*CRenderTargetManager::getRenderTarget(uint width
, uint height
, bool mode2D
, UTexture::TUploadFormat format
)
82 // Find or create a render target, short loop so no real optimization
83 for (std::vector
<CRenderTargetDescInt
*>::iterator
it(m_RenderTargets
.begin()), end(m_RenderTargets
.end()); it
!= end
; ++it
)
85 CRenderTargetDescInt
*desc
= *it
;
86 if (!desc
->InUse
&& desc
->Width
== width
&& desc
->Height
== height
&& desc
->Mode2D
== mode2D
&& desc
->Format
== format
)
90 return desc
->TextureUser
;
94 nldebug("3D: Create new render target (%u x %u)", width
, height
);
95 NL3D::IDriver
*drvInternal
= (static_cast<CDriverUser
*>(m_Driver
))->getDriver();
96 CRenderTargetDescInt
*desc
= new CRenderTargetDescInt();
97 CTextureBloom
*tex
= new CTextureBloom(); // LOL
99 desc
->TextureInterface
= tex
;
100 desc
->TextureInterface
->setRenderTarget(true);
101 desc
->TextureInterface
->setReleasable(false);
102 desc
->TextureInterface
->setUploadFormat((ITexture::TUploadFormat
)(uint32
)format
);
103 desc
->TextureInterface
->resize(width
, height
);
104 desc
->TextureInterface
->setFilterMode(ITexture::Linear
, ITexture::LinearMipMapOff
);
105 desc
->TextureInterface
->setWrapS(ITexture::Clamp
);
106 desc
->TextureInterface
->setWrapT(ITexture::Clamp
);
107 drvInternal
->setupTexture(*desc
->TextureInterface
);
108 desc
->TextureUser
= new CTextureUser(desc
->TextureInterface
);
109 nlassert(!drvInternal
->isTextureRectangle(desc
->TextureInterface
)); // Not allowed, we only support NPOT for render targets now.
111 desc
->Height
= height
;
112 desc
->Mode2D
= mode2D
;
113 desc
->Format
= format
;
116 m_RenderTargets
.push_back(desc
);
117 return desc
->TextureUser
;
120 void CRenderTargetManager::recycleRenderTarget(NL3D::CTextureUser
*renderTarget
)
122 for (std::vector
<CRenderTargetDescInt
*>::iterator
it(m_RenderTargets
.begin()), end(m_RenderTargets
.end()); it
!= end
; ++it
)
124 CRenderTargetDescInt
*desc
= *it
;
125 if (desc
->TextureUser
== renderTarget
)
131 nlerror("3D: Render target not found");
134 void CRenderTargetManager::cleanup()
136 for (sint i
= 0; i
< (sint
)m_RenderTargets
.size(); ++i
)
138 CRenderTargetDescInt
*desc
= m_RenderTargets
[i
];
139 nlassert(!desc
->InUse
); // Assert for debugging, to not allow textures being carried over between frames. Optional assert
145 nldebug("3D: Release render target (%u x %u)", desc
->Width
, desc
->Height
);
146 delete desc
->TextureUser
;
147 desc
->TextureUser
= NULL
;
148 desc
->TextureInterface
= NULL
; // CSmartPtr
149 m_RenderTargets
.erase(m_RenderTargets
.begin() + i
);
155 // Flag for next round
162 } /* namespace NL3D */