Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / 3d / render_target_manager.cpp
blobdd7b192a581c80ac8b1b15131851413425acdff6
1 /**
2 * \file render_target_manager.cpp
3 * \brief CRenderTargetManager
4 * \date 2014-07-30 21:30GMT
5 * \author Jan Boon (Kaetemi)
6 * CRenderTargetManager
7 */
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/>.
25 #include "std3d.h"
26 #include "nel/3d/render_target_manager.h"
28 // STL includes
29 #include <sstream>
31 // NeL includes
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"
41 // Project includes
43 using namespace std;
44 // using namespace NLMISC;
46 #ifdef DEBUG_NEW
47 #define new DEBUG_NEW
48 #endif
50 namespace NL3D {
52 struct CRenderTargetDescInt
54 public:
55 // Options
56 uint Width;
57 uint Height;
58 bool Mode2D;
59 UTexture::TUploadFormat Format;
61 // Data
62 NL3D::CTextureUser *TextureUser;
63 NLMISC::CSmartPtr<NL3D::ITexture> TextureInterface;
64 bool InUse;
65 bool Used;
68 CRenderTargetManager::CRenderTargetManager() : m_Driver(NULL)
73 CRenderTargetManager::~CRenderTargetManager()
75 // Call twice to reset counters and cleanup
76 cleanup();
77 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)
88 desc->InUse = true;
89 desc->Used = true;
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
98 tex->mode2D(mode2D);
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.
110 desc->Width = width;
111 desc->Height = height;
112 desc->Mode2D = mode2D;
113 desc->Format = format;
114 desc->Used = true;
115 desc->InUse = true;
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)
127 desc->InUse = false;
128 return;
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
140 if (!desc->InUse)
142 if (!desc->Used)
144 // No longer in use
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);
150 delete desc;
151 --i;
153 else
155 // Flag for next round
156 desc->Used = false;
162 } /* namespace NL3D */
164 /* end of file */