VolumetricFogRTManager.cpp
Engine/source/environment/VolumetricFogRTManager.cpp
Public Variables
Public Functions
ConsoleDocClass(VolumetricFogRTManager , "@brief Creates and maintains one set of rendertargets <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be used by <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">every\n</a>" "<a href="/coding/class/classvolumetricfog/">VolumetricFog</a> object in the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">scene.\n\n</a>" "Will be loaded at startup end removed when ending <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">game.\n\n</a>" "<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Methods:\n</a>" " get() returns the currently loaded VolumetricFogRTManager, also <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">accessible\n</a>" " through <a href="/coding/file/volumetricfogrtmanager_8h/#volumetricfogrtmanager_8h_1adef92cc473c0b730c5c060a4cc45e0da">VFRTM</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">define.\n</a>" " Init() Initializes the rendertargets, called when <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/class/classvolumetricfog/">VolumetricFog</a> object <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">is\n</a>" " added <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">scene.\n</a>" " isInitialed() returns true <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> Rendertargets are present, false <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> not, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">then\n</a>" " Init() should be called <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> create the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">rendertargets.\n</a>" " setQuality(<a href="/coding/file/types_8h/#types_8h_1ac3df7cf3c8cb172a588adec881447d68">U32</a> Quality) Normally <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> rendertarget has the same <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1ab7d671599a7b25ca99a487fa341bc33a">size</a> as the view, \<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " with this method you can scale down the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1ab7d671599a7b25ca99a487fa341bc33a">size</a> of <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">it.\n</a>" " Be aware that scaling down will introduce <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">renderartefacts.\n</a>" " @ingroup Atmosphere" )
DefineEngineFunction(SetFogVolumeQuality , S32 , (U32 new_quality) , "@brief Resizes the rendertargets of the Volumetric Fog <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">object.\n</a>" "@params new_quality <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> quality <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> the rendertargets 1, 2 , 3 )
Detailed Description
Public Variables
VolumetricFogRTManager * gVolumetricFogRTManager
MODULE_END
MODULE_INIT
MODULE_SHUTDOWN
Public Functions
addObjectToScene(gVolumetricFogRTManager )
ConsoleDocClass(VolumetricFogRTManager , "@brief Creates and maintains one set of rendertargets <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be used by <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">every\n</a>" "<a href="/coding/class/classvolumetricfog/">VolumetricFog</a> object in the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">scene.\n\n</a>" "Will be loaded at startup end removed when ending <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">game.\n\n</a>" "<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Methods:\n</a>" " get() returns the currently loaded VolumetricFogRTManager, also <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">accessible\n</a>" " through <a href="/coding/file/volumetricfogrtmanager_8h/#volumetricfogrtmanager_8h_1adef92cc473c0b730c5c060a4cc45e0da">VFRTM</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">define.\n</a>" " Init() Initializes the rendertargets, called when <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/class/classvolumetricfog/">VolumetricFog</a> object <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">is\n</a>" " added <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">scene.\n</a>" " isInitialed() returns true <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> Rendertargets are present, false <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> not, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">then\n</a>" " Init() should be called <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> create the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">rendertargets.\n</a>" " setQuality(<a href="/coding/file/types_8h/#types_8h_1ac3df7cf3c8cb172a588adec881447d68">U32</a> Quality) Normally <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> rendertarget has the same <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1ab7d671599a7b25ca99a487fa341bc33a">size</a> as the view, \<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " with this method you can scale down the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1ab7d671599a7b25ca99a487fa341bc33a">size</a> of <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">it.\n</a>" " Be aware that scaling down will introduce <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">renderartefacts.\n</a>" " @ingroup Atmosphere" )
DefineEngineFunction(SetFogVolumeQuality , S32 , (U32 new_quality) , "@brief Resizes the rendertargets of the Volumetric Fog <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">object.\n</a>" "@params new_quality <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> quality <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> the rendertargets 1, 2 , 3 )
IMPLEMENT_CONOBJECT(VolumetricFogRTManager )
SAFE_DELETE(gVolumetricFogRTManager )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24//----------------------------------------------------------------------------- 25// Volumetric Fog Rendertarget Manager 26// 27// Creates and maintains one set of rendertargets to be used by every 28// VolumetricFog object in the scene. 29// 30// Will be loaded at startup end removed when ending game. 31// 32//----------------------------------------------------------------------------- 33 34#include "VolumetricFogRTManager.h" 35#include "core/module.h" 36#include "scene/sceneManager.h" 37#include "windowManager/platformWindowMgr.h" 38#include "console/engineAPI.h" 39#include "gui/core/guiCanvas.h" 40#include "gfx/gfxDevice.h" 41 42MODULE_BEGIN(VolumetricFogRTManager) 43 44MODULE_INIT_AFTER(Scene) 45MODULE_SHUTDOWN_BEFORE(Scene) 46 47MODULE_INIT 48{ 49 gVolumetricFogRTManager = new VolumetricFogRTManager; 50 gClientSceneGraph->addObjectToScene(gVolumetricFogRTManager); 51} 52 53MODULE_SHUTDOWN 54{ 55 gClientSceneGraph->removeObjectFromScene(gVolumetricFogRTManager); 56 SAFE_DELETE(gVolumetricFogRTManager); 57} 58 59MODULE_END; 60 61ConsoleDocClass( VolumetricFogRTManager, 62"@brief Creates and maintains one set of rendertargets to be used by every\n" 63"VolumetricFog object in the scene.\n\n" 64"Will be loaded at startup end removed when ending game.\n\n" 65"Methods:\n" 66" get() returns the currently loaded VolumetricFogRTManager, also accessible\n" 67" through VFRTM define.\n" 68" Init() Initializes the rendertargets, called when a VolumetricFog object is\n" 69" added to the scene.\n" 70" isInitialed() returns true if Rendertargets are present, false if not, then\n" 71" Init() should be called to create the rendertargets.\n" 72" setQuality(U32 Quality) Normally a rendertarget has the same size as the view,\n" 73" with this method you can scale down the size of it.\n" 74" Be aware that scaling down will introduce renderartefacts.\n" 75"@ingroup Atmosphere" 76); 77 78VolumetricFogRTMResizeSignal VolumetricFogRTManager::smVolumetricFogRTMResizeSignal; 79 80VolumetricFogRTManager *gVolumetricFogRTManager = NULL; 81 82S32 VolumetricFogRTManager::mTargetScale = 1; 83 84IMPLEMENT_CONOBJECT(VolumetricFogRTManager); 85 86VolumetricFogRTManager::VolumetricFogRTManager() 87{ 88 setGlobalBounds(); 89 mTypeMask |= EnvironmentObjectType; 90 mNetFlags.set(IsGhost); 91 mPlatformWindow = NULL; 92 mIsInitialized = false; 93 mNumFogObjects = 0; 94 mWidth = 0; 95 mHeight = 0; 96} 97 98VolumetricFogRTManager::~VolumetricFogRTManager() 99{ 100 if (mFrontTarget.isRegistered()) 101 mFrontTarget.unregister(); 102 103 if (mDepthTarget.isRegistered()) 104 mDepthTarget.unregister(); 105 106 if (mDepthBuffer.isValid()) 107 mDepthBuffer->kill(); 108 109 if (mFrontBuffer.isValid()) 110 mFrontBuffer->kill(); 111} 112 113void VolumetricFogRTManager::onSceneRemove() 114{ 115 if (mIsInitialized) 116 mPlatformWindow->resizeEvent.remove(this, &VolumetricFogRTManager::ResizeRT); 117} 118 119void VolumetricFogRTManager::onRemove() 120{ 121 removeFromScene(); 122 Parent::onRemove(); 123} 124 125void VolumetricFogRTManager::consoleInit() 126{ 127 Con::addVariable("$pref::VolumetricFog::Quality", TypeS32, &mTargetScale, 128 "The scale of the rendertargets.\n" 129 "@ingroup Rendering\n"); 130} 131 132bool VolumetricFogRTManager::Init() 133{ 134 if (mIsInitialized) 135 { 136 Con::errorf("VolumetricFogRTManager allready initialized!!"); 137 return true; 138 } 139 140 if (GFX->getAdapterType() == NullDevice) 141 { 142 mIsInitialized = true; 143 Con::errorf("VolumetricFogRTManager - Dedicated server"); 144 return true; 145 } 146 147 GuiCanvas* cv = dynamic_cast<GuiCanvas*>(Sim::findObject("Canvas")); 148 if (cv == NULL) 149 { 150 Con::errorf("VolumetricFogRTManager::Init() - Canvas not found!!"); 151 return false; 152 } 153 154 mPlatformWindow = cv->getPlatformWindow(); 155 mPlatformWindow->resizeEvent.notify(this,&VolumetricFogRTManager::ResizeRT); 156 157 if (mTargetScale < 1 || GFX->getAdapterType() == Direct3D11) 158 mTargetScale = 1; 159 160 mWidth = mFloor(mPlatformWindow->getClientExtent().x / mTargetScale); 161 mHeight = mFloor(mPlatformWindow->getClientExtent().y / mTargetScale); 162 163 mDepthBuffer = GFXTexHandle(mWidth, mHeight, GFXFormatR32F, 164 &GFXRenderTargetProfile, avar("%s() - mDepthBuffer (line %d)", __FUNCTION__, __LINE__)); 165 if (!mDepthBuffer.isValid()) 166 { 167 Con::errorf("VolumetricFogRTManager Fatal Error: Unable to create Depthbuffer"); 168 return false; 169 } 170 if (!mDepthTarget.registerWithName("volfogdepth")) 171 { 172 Con::errorf("VolumetricFogRTManager Fatal Error : Unable to register Depthbuffer"); 173 return false; 174 } 175 mDepthTarget.setTexture(mDepthBuffer); 176 177 mFrontBuffer = GFXTexHandle(mWidth, mHeight, GFXFormatR32F, 178 &GFXRenderTargetProfile, avar("%s() - mFrontBuffer (line %d)", __FUNCTION__, __LINE__)); 179 if (!mFrontBuffer.isValid()) 180 { 181 Con::errorf("VolumetricFogRTManager Fatal Error: Unable to create front buffer"); 182 return false; 183 } 184 if (!mFrontTarget.registerWithName("volfogfront")) 185 { 186 Con::errorf("VolumetricFogRTManager Fatal Error : Unable to register Frontbuffer"); 187 return false; 188 } 189 190 mFrontTarget.setTexture(mFrontBuffer); 191 192 Con::setVariable("$VolumetricFog::density", "0.0"); 193 194 mIsInitialized = true; 195 196 return true; 197} 198 199U32 VolumetricFogRTManager::IncFogObjects() 200{ 201 mNumFogObjects++; 202 return mNumFogObjects; 203} 204 205U32 VolumetricFogRTManager::DecFogObjects() 206{ 207 if (mNumFogObjects > 0) 208 mNumFogObjects--; 209 return mNumFogObjects; 210} 211 212void VolumetricFogRTManager::ResizeRT( WindowId did, S32 width, S32 height ) 213{ 214 smVolumetricFogRTMResizeSignal.trigger(this, true); 215} 216 217void VolumetricFogRTManager::FogAnswered() 218{ 219 if (Resize()) 220 smVolumetricFogRTMResizeSignal.trigger(this, false); 221 else 222 Con::errorf("VolumetricFogRTManager::FogAnswered - Error resizing rendertargets!"); 223} 224 225bool VolumetricFogRTManager::Resize() 226{ 227 if (mTargetScale < 1 || GFX->getAdapterType() == Direct3D11) 228 mTargetScale = 1; 229 230 mWidth = mFloor(mPlatformWindow->getClientExtent().x / mTargetScale); 231 mHeight = mFloor(mPlatformWindow->getClientExtent().y / mTargetScale); 232 233 if (mWidth < 16 || mHeight < 16) 234 return false; 235 236 if (mFrontTarget.isRegistered()) 237 mFrontTarget.setTexture(NULL); 238 239 if (mDepthTarget.isRegistered()) 240 mDepthTarget.setTexture(NULL); 241 242 if (mDepthBuffer.isValid()) 243 mDepthBuffer->kill(); 244 245 if (mFrontBuffer.isValid()) 246 mFrontBuffer->kill(); 247 248 mFrontBuffer = GFXTexHandle(mWidth, mHeight, GFXFormatR32F, 249 &GFXRenderTargetProfile, avar("%s() - mFrontBuffer (line %d)", __FUNCTION__, __LINE__)); 250 if (!mFrontBuffer.isValid()) 251 { 252 Con::errorf("VolumetricFogRTManager::Resize() Fatal Error: Unable to create front buffer"); 253 return false; 254 } 255 mFrontTarget.setTexture(mFrontBuffer); 256 257 mDepthBuffer = GFXTexHandle(mWidth, mHeight, GFXFormatR32F, 258 &GFXRenderTargetProfile, avar("%s() - mDepthBuffer (line %d)", __FUNCTION__, __LINE__)); 259 if (!mDepthBuffer.isValid()) 260 { 261 Con::errorf("VolumetricFogRTManager::Resize() Fatal Error: Unable to create Depthbuffer"); 262 return false; 263 } 264 mDepthTarget.setTexture(mDepthBuffer); 265 return true; 266} 267 268S32 VolumetricFogRTManager::setQuality(U32 Quality) 269{ 270 if (!mIsInitialized) 271 return (mTargetScale = Quality); 272 273 if (Quality < 1) 274 Quality = 1; 275 276 if (Quality == mTargetScale) 277 return mTargetScale; 278 279 mTargetScale = Quality; 280 281 smVolumetricFogRTMResizeSignal.trigger(this, true); 282 283 return mTargetScale; 284} 285 286VolumetricFogRTManager* VolumetricFogRTManager::get() 287{ 288 return gVolumetricFogRTManager; 289} 290 291DefineEngineFunction(SetFogVolumeQuality, S32, (U32 new_quality), , 292"@brief Resizes the rendertargets of the Volumetric Fog object.\n" 293"@params new_quality new quality for the rendertargets 1 = full size, 2 = halfsize, 3 = 1/3, 4 = 1/4 ...") 294{ 295 if (VFRTM == NULL) 296 return -1; 297 return VFRTM->setQuality(new_quality); 298} 299