postEffect.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "postFx/postEffect.h"
  24. #include "console/engineAPI.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/strings/stringUnit.h"
  27. #include "console/consoleTypes.h"
  28. #include "console/engineAPI.h"
  29. #include "math/util/frustum.h"
  30. #include "math/mathUtils.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "gfx/gfxStringEnumTranslate.h"
  33. #include "gfx/gfxTextureManager.h"
  34. #include "gfx/gfxDebugEvent.h"
  35. #include "gfx/util/screenspace.h"
  36. #include "gfx/sim/gfxStateBlockData.h"
  37. #include "scene/sceneRenderState.h"
  38. #include "shaderGen/shaderGenVars.h"
  39. #include "lighting/lightInfo.h"
  40. #include "lighting/lightManager.h"
  41. #include "materials/materialManager.h"
  42. #include "materials/shaderData.h"
  43. #include "postFx/postEffectManager.h"
  44. #include "postFx/postEffectVis.h"
  45. ConsoleDocClass( PostEffect,
  46. "@brief A fullscreen shader effect.\n\n"
  47. "@section PFXTextureIdentifiers\n\n"
  48. "@ingroup Rendering\n"
  49. );
  50. IMPLEMENT_CALLBACK( PostEffect, onAdd, void, (), (),
  51. "Called when this object is first created and registered."
  52. );
  53. IMPLEMENT_CALLBACK( PostEffect, preProcess, void, (), (),
  54. "Called when an effect is processed but before textures are bound. This "
  55. "allows the user to change texture related paramaters or macros at runtime.\n"
  56. "@tsexample\n"
  57. "function SSAOPostFx::preProcess( %this )\n"
  58. "{\n"
  59. " if ( $SSAOPostFx::quality !$= %this.quality )\n"
  60. " {\n"
  61. " %this.quality = mClamp( mRound( $SSAOPostFx::quality ), 0, 2 );\n"
  62. " \n"
  63. " %this.setShaderMacro( \"QUALITY\", %this.quality );\n"
  64. " }\n"
  65. " %this.targetScale = $SSAOPostFx::targetScale;\n"
  66. "}\n"
  67. "@endtsexample\n"
  68. "@see setShaderConst\n"
  69. "@see setShaderMacro"
  70. );
  71. IMPLEMENT_CALLBACK( PostEffect, setShaderConsts, void, (), (),
  72. "Called immediate before processing this effect. This is the user's chance "
  73. "to set the value of shader uniforms (constants).\n"
  74. "@see setShaderConst"
  75. );
  76. IMPLEMENT_CALLBACK( PostEffect, onEnabled, bool, (), (),
  77. "Called when this effect becomes enabled. If the user returns false from "
  78. "this callback the effect will not be enabled.\n"
  79. "@return True to allow this effect to be enabled."
  80. );
  81. IMPLEMENT_CALLBACK( PostEffect, onDisabled, void, (), (),
  82. "Called when this effect becomes disabled."
  83. );
  84. ImplementEnumType( PFXRenderTime,
  85. "When to process this effect during the frame.\n"
  86. "@ingroup Rendering\n\n")
  87. { PFXBeforeBin, "PFXBeforeBin", "Before a RenderInstManager bin.\n" },
  88. { PFXAfterBin, "PFXAfterBin", "After a RenderInstManager bin.\n" },
  89. { PFXAfterDiffuse, "PFXAfterDiffuse", "After the diffuse rendering pass.\n" },
  90. { PFXEndOfFrame, "PFXEndOfFrame", "When the end of the frame is reached.\n" },
  91. { PFXTexGenOnDemand, "PFXTexGenOnDemand", "This PostEffect is not processed by the manager. It will generate its texture when it is requested.\n" }
  92. EndImplementEnumType;
  93. ImplementEnumType( PFXTargetClear,
  94. "Describes when the target texture should be cleared\n"
  95. "@ingroup Rendering\n\n")
  96. { PFXTargetClear_None, "PFXTargetClear_None", "Never clear the PostEffect target.\n" },
  97. { PFXTargetClear_OnCreate, "PFXTargetClear_OnCreate", "Clear once on create.\n" },
  98. { PFXTargetClear_OnDraw, "PFXTargetClear_OnDraw", "Clear before every draw.\n" },
  99. EndImplementEnumType;
  100. GFXImplementVertexFormat( PFXVertex )
  101. {
  102. addElement( "POSITION", GFXDeclType_Float3 );
  103. addElement( "TEXCOORD", GFXDeclType_Float2, 0 );
  104. addElement( "TEXCOORD", GFXDeclType_Float3, 1 );
  105. };
  106. GFX_ImplementTextureProfile( PostFxTargetProfile,
  107. GFXTextureProfile::DiffuseMap,
  108. GFXTextureProfile::PreserveSize |
  109. GFXTextureProfile::RenderTarget |
  110. GFXTextureProfile::Pooled,
  111. GFXTextureProfile::None );
  112. IMPLEMENT_CONOBJECT(PostEffect);
  113. GFX_ImplementTextureProfile( PostFxTextureProfile,
  114. GFXTextureProfile::DiffuseMap,
  115. GFXTextureProfile::Static | GFXTextureProfile::PreserveSize | GFXTextureProfile::NoMipmap,
  116. GFXTextureProfile::None );
  117. void PostEffect::EffectConst::set( const String &newVal )
  118. {
  119. if ( mStringVal == newVal )
  120. return;
  121. mStringVal = newVal;
  122. mDirty = true;
  123. }
  124. void PostEffect::EffectConst::setToBuffer( GFXShaderConstBufferRef buff )
  125. {
  126. // Nothing to do if the value hasn't changed.
  127. if ( !mDirty )
  128. return;
  129. mDirty = false;
  130. // If we don't have a handle... get it now.
  131. if ( !mHandle )
  132. mHandle = buff->getShader()->getShaderConstHandle( mName );
  133. // If the handle isn't valid then we're done.
  134. if ( !mHandle->isValid() )
  135. return;
  136. const GFXShaderConstType type = mHandle->getType();
  137. // For now, we're only going
  138. // to support float4 arrays.
  139. // Expand to other types as necessary.
  140. U32 arraySize = mHandle->getArraySize();
  141. const char *strVal = mStringVal.c_str();
  142. if ( type == GFXSCT_Float )
  143. {
  144. F32 val;
  145. Con::setData( TypeF32, &val, 0, 1, &strVal );
  146. buff->set( mHandle, val );
  147. }
  148. else if ( type == GFXSCT_Float2 )
  149. {
  150. Point2F val;
  151. Con::setData( TypePoint2F, &val, 0, 1, &strVal );
  152. buff->set( mHandle, val );
  153. }
  154. else if ( type == GFXSCT_Float3 )
  155. {
  156. Point3F val;
  157. Con::setData( TypePoint3F, &val, 0, 1, &strVal );
  158. buff->set( mHandle, val );
  159. }
  160. else
  161. {
  162. Point4F val;
  163. if ( arraySize > 1 )
  164. {
  165. // Do array setup!
  166. //U32 unitCount = StringUnit::getUnitCount( strVal, "\t" );
  167. //AssertFatal( unitCount == arraySize, "" );
  168. String tmpString;
  169. Vector<Point4F> valArray;
  170. for ( U32 i = 0; i < arraySize; i++ )
  171. {
  172. tmpString = StringUnit::getUnit( strVal, i, "\t" );
  173. valArray.increment();
  174. const char *tmpCStr = tmpString.c_str();
  175. Con::setData( TypePoint4F, &valArray.last(), 0, 1, &tmpCStr );
  176. }
  177. AlignedArray<Point4F> rectData( valArray.size(), sizeof( Point4F ), (U8*)valArray.address(), false );
  178. buff->set( mHandle, rectData );
  179. }
  180. else
  181. {
  182. // Do regular setup.
  183. Con::setData( TypePoint4F, &val, 0, 1, &strVal );
  184. buff->set( mHandle, val );
  185. }
  186. }
  187. }
  188. //-------------------------------------------------------------------------
  189. // PostEffect
  190. //-------------------------------------------------------------------------
  191. PostEffect::PostEffect()
  192. : mRenderTime( PFXAfterDiffuse ),
  193. mRenderPriority( 1.0 ),
  194. mEnabled( false ),
  195. mSkip( false ),
  196. mUpdateShader( true ),
  197. mStateBlockData( NULL ),
  198. mAllowReflectPass( false ),
  199. mTargetClear( PFXTargetClear_None ),
  200. mTargetScale( Point2F::One ),
  201. mTargetSize( Point2I::Zero ),
  202. mTargetFormat( GFXFormatR8G8B8A8 ),
  203. mTargetClearColor( ColorF::BLACK ),
  204. mOneFrameOnly( false ),
  205. mOnThisFrame( true ),
  206. mShaderReloadKey( 0 ),
  207. mIsValid( false ),
  208. mRTSizeSC( NULL ),
  209. mOneOverRTSizeSC( NULL ),
  210. mViewportOffsetSC( NULL ),
  211. mFogDataSC( NULL ),
  212. mFogColorSC( NULL ),
  213. mEyePosSC( NULL ),
  214. mMatWorldToScreenSC( NULL ),
  215. mMatScreenToWorldSC( NULL ),
  216. mMatPrevScreenToWorldSC( NULL ),
  217. mNearFarSC( NULL ),
  218. mInvNearFarSC( NULL ),
  219. mWorldToScreenScaleSC( NULL ),
  220. mWaterColorSC( NULL ),
  221. mWaterFogDataSC( NULL ),
  222. mAmbientColorSC( NULL ),
  223. mWaterFogPlaneSC( NULL ),
  224. mWaterDepthGradMaxSC( NULL ),
  225. mScreenSunPosSC( NULL ),
  226. mLightDirectionSC( NULL ),
  227. mCameraForwardSC( NULL ),
  228. mAccumTimeSC( NULL ),
  229. mDeltaTimeSC( NULL ),
  230. mInvCameraMatSC( NULL )
  231. {
  232. dMemset( mActiveTextures, 0, sizeof( GFXTextureObject* ) * NumTextures );
  233. dMemset( mActiveNamedTarget, 0, sizeof( NamedTexTarget* ) * NumTextures );
  234. dMemset( mActiveTextureViewport, 0, sizeof( RectI ) * NumTextures );
  235. dMemset( mTexSizeSC, 0, sizeof( GFXShaderConstHandle* ) * NumTextures );
  236. dMemset( mRenderTargetParamsSC, 0, sizeof( GFXShaderConstHandle* ) * NumTextures );
  237. }
  238. PostEffect::~PostEffect()
  239. {
  240. EffectConstTable::Iterator iter = mEffectConsts.begin();
  241. for ( ; iter != mEffectConsts.end(); iter++ )
  242. delete iter->value;
  243. }
  244. void PostEffect::initPersistFields()
  245. {
  246. addField( "shader", TypeRealString, Offset( mShaderName, PostEffect ),
  247. "Name of a GFXShaderData for this effect." );
  248. addField( "stateBlock", TYPEID<GFXStateBlockData>(), Offset( mStateBlockData, PostEffect ),
  249. "Name of a GFXStateBlockData for this effect." );
  250. addField( "target", TypeRealString, Offset( mTargetName, PostEffect ),
  251. "String identifier of this effect's target texture.\n"
  252. "@see PFXTextureIdentifiers" );
  253. addField( "targetDepthStencil", TypeRealString, Offset( mTargetDepthStencilName, PostEffect ),
  254. "Optional string identifier for this effect's target depth/stencil texture.\n"
  255. "@see PFXTextureIdentifiers" );
  256. addField( "targetScale", TypePoint2F, Offset( mTargetScale, PostEffect ),
  257. "If targetSize is zero this is used to set a relative size from the current target." );
  258. addField( "targetSize", TypePoint2I, Offset( mTargetSize, PostEffect ),
  259. "If non-zero this is used as the absolute target size." );
  260. addField( "targetFormat", TypeGFXFormat, Offset( mTargetFormat, PostEffect ),
  261. "Format of the target texture, not applicable if writing to the backbuffer." );
  262. addField( "targetClearColor", TypeColorF, Offset( mTargetClearColor, PostEffect ),
  263. "Color to which the target texture is cleared before rendering." );
  264. addField( "targetClear", TYPEID< PFXTargetClear >(), Offset( mTargetClear, PostEffect ),
  265. "Describes when the target texture should be cleared." );
  266. addField( "texture", TypeImageFilename, Offset( mTexFilename, PostEffect ), NumTextures,
  267. "Input textures to this effect ( samplers ).\n"
  268. "@see PFXTextureIdentifiers" );
  269. addField( "renderTime", TYPEID< PFXRenderTime >(), Offset( mRenderTime, PostEffect ),
  270. "When to process this effect during the frame." );
  271. addField( "renderBin", TypeRealString, Offset( mRenderBin, PostEffect ),
  272. "Name of a renderBin, used if renderTime is PFXBeforeBin or PFXAfterBin." );
  273. addField( "renderPriority", TypeF32, Offset( mRenderPriority, PostEffect ),
  274. "PostEffects are processed in DESCENDING order of renderPriority if more than one has the same renderBin/Time." );
  275. addField( "allowReflectPass", TypeBool, Offset( mAllowReflectPass, PostEffect ),
  276. "Is this effect processed during reflection render passes." );
  277. addProtectedField( "isEnabled", TypeBool, Offset( mEnabled, PostEffect),
  278. &PostEffect::_setIsEnabled, &defaultProtectedGetFn,
  279. "Is the effect on." );
  280. addField( "onThisFrame", TypeBool, Offset( mOnThisFrame, PostEffect ),
  281. "Allows you to turn on a PostEffect for only a single frame." );
  282. addField( "oneFrameOnly", TypeBool, Offset( mOneFrameOnly, PostEffect ),
  283. "Allows you to turn on a PostEffect for only a single frame." );
  284. addField( "skip", TypeBool, Offset( mSkip, PostEffect ),
  285. "Skip processing of this PostEffect and its children even if its parent "
  286. "is enabled. Parent and sibling PostEffects in the chain are still processed." );
  287. Parent::initPersistFields();
  288. }
  289. bool PostEffect::onAdd()
  290. {
  291. if( !Parent::onAdd() )
  292. return false;
  293. LightManager::smActivateSignal.notify( this, &PostEffect::_onLMActivate );
  294. mUpdateShader = true;
  295. // Grab the script path.
  296. Torque::Path scriptPath( Con::getVariable( "$Con::File" ) );
  297. scriptPath.setFileName( String::EmptyString );
  298. scriptPath.setExtension( String::EmptyString );
  299. // Find additional textures
  300. for( int i = 0; i < NumTextures; i++ )
  301. {
  302. String texFilename = mTexFilename[i];
  303. // Skip empty stages or ones with variable or target names.
  304. if ( texFilename.isEmpty() ||
  305. texFilename[0] == '$' ||
  306. texFilename[0] == '#' )
  307. continue;
  308. // If '/', then path is specified, open normally
  309. if ( texFilename[0] != '/' )
  310. texFilename = scriptPath.getFullPath() + '/' + texFilename;
  311. // Try to load the texture.
  312. mTextures[i].set( texFilename, &PostFxTextureProfile, avar( "%s() - (line %d)", __FUNCTION__, __LINE__ ) );
  313. }
  314. // Is the target a named target?
  315. if ( mTargetName.isNotEmpty() && mTargetName[0] == '#' )
  316. {
  317. mNamedTarget.registerWithName( mTargetName.substr( 1 ) );
  318. mNamedTarget.getTextureDelegate().bind( this, &PostEffect::_getTargetTexture );
  319. }
  320. if ( mTargetDepthStencilName.isNotEmpty() && mTargetDepthStencilName[0] == '#' )
  321. mNamedTargetDepthStencil.registerWithName( mTargetDepthStencilName.substr( 1 ) );
  322. if (mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered())
  323. GFXTextureManager::addEventDelegate( this, &PostEffect::_onTextureEvent );
  324. // Call onAdd in script
  325. onAdd_callback();
  326. // Should we start enabled?
  327. if ( mEnabled )
  328. {
  329. mEnabled = false;
  330. enable();
  331. }
  332. getSet()->addObject( this );
  333. return true;
  334. }
  335. void PostEffect::onRemove()
  336. {
  337. Parent::onRemove();
  338. PFXMGR->_removeEffect( this );
  339. LightManager::smActivateSignal.remove( this, &PostEffect::_onLMActivate );
  340. mShader = NULL;
  341. _cleanTargets();
  342. if ( mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered() )
  343. GFXTextureManager::removeEventDelegate( this, &PostEffect::_onTextureEvent );
  344. if ( mNamedTarget.isRegistered() )
  345. {
  346. mNamedTarget.unregister();
  347. mNamedTarget.getTextureDelegate().clear();
  348. }
  349. if ( mNamedTargetDepthStencil.isRegistered() )
  350. mNamedTargetDepthStencil.unregister();
  351. }
  352. void PostEffect::_updateScreenGeometry( const Frustum &frustum,
  353. GFXVertexBufferHandle<PFXVertex> *outVB )
  354. {
  355. outVB->set( GFX, 4, GFXBufferTypeVolatile );
  356. const Point3F *frustumPoints = frustum.getPoints();
  357. const Point3F& cameraPos = frustum.getPosition();
  358. PFXVertex *vert = outVB->lock();
  359. vert->point.set( -1.0, -1.0, 0.0 );
  360. vert->texCoord.set( 0.0f, 1.0f );
  361. vert->wsEyeRay = frustumPoints[Frustum::FarBottomLeft] - cameraPos;
  362. vert++;
  363. vert->point.set( -1.0, 1.0, 0.0 );
  364. vert->texCoord.set( 0.0f, 0.0f );
  365. vert->wsEyeRay = frustumPoints[Frustum::FarTopLeft] - cameraPos;
  366. vert++;
  367. vert->point.set( 1.0, 1.0, 0.0 );
  368. vert->texCoord.set( 1.0f, 0.0f );
  369. vert->wsEyeRay = frustumPoints[Frustum::FarTopRight] - cameraPos;
  370. vert++;
  371. vert->point.set( 1.0, -1.0, 0.0 );
  372. vert->texCoord.set( 1.0f, 1.0f );
  373. vert->wsEyeRay = frustumPoints[Frustum::FarBottomRight] - cameraPos;
  374. vert++;
  375. outVB->unlock();
  376. }
  377. void PostEffect::_setupStateBlock( const SceneRenderState *state )
  378. {
  379. if ( mStateBlock.isNull() )
  380. {
  381. GFXStateBlockDesc desc;
  382. if ( mStateBlockData )
  383. desc = mStateBlockData->getState();
  384. mStateBlock = GFX->createStateBlock( desc );
  385. }
  386. GFX->setStateBlock( mStateBlock );
  387. }
  388. void PostEffect::_setupConstants( const SceneRenderState *state )
  389. {
  390. // Alloc the const buffer.
  391. if ( mShaderConsts.isNull() )
  392. {
  393. mShaderConsts = mShader->allocConstBuffer();
  394. mRTSizeSC = mShader->getShaderConstHandle( "$targetSize" );
  395. mOneOverRTSizeSC = mShader->getShaderConstHandle( "$oneOverTargetSize" );
  396. mTexSizeSC[0] = mShader->getShaderConstHandle( "$texSize0" );
  397. mTexSizeSC[1] = mShader->getShaderConstHandle( "$texSize1" );
  398. mTexSizeSC[2] = mShader->getShaderConstHandle( "$texSize2" );
  399. mTexSizeSC[3] = mShader->getShaderConstHandle( "$texSize3" );
  400. mTexSizeSC[4] = mShader->getShaderConstHandle( "$texSize4" );
  401. mTexSizeSC[5] = mShader->getShaderConstHandle( "$texSize5" );
  402. mRenderTargetParamsSC[0] = mShader->getShaderConstHandle( "$rtParams0" );
  403. mRenderTargetParamsSC[1] = mShader->getShaderConstHandle( "$rtParams1" );
  404. mRenderTargetParamsSC[2] = mShader->getShaderConstHandle( "$rtParams2" );
  405. mRenderTargetParamsSC[3] = mShader->getShaderConstHandle( "$rtParams3" );
  406. mRenderTargetParamsSC[4] = mShader->getShaderConstHandle( "$rtParams4" );
  407. mRenderTargetParamsSC[5] = mShader->getShaderConstHandle( "$rtParams5" );
  408. //mViewportSC = shader->getShaderConstHandle( "$viewport" );
  409. mFogDataSC = mShader->getShaderConstHandle( ShaderGenVars::fogData );
  410. mFogColorSC = mShader->getShaderConstHandle( ShaderGenVars::fogColor );
  411. mEyePosSC = mShader->getShaderConstHandle( ShaderGenVars::eyePosWorld );
  412. mNearFarSC = mShader->getShaderConstHandle( "$nearFar" );
  413. mInvNearFarSC = mShader->getShaderConstHandle( "$invNearFar" );
  414. mWorldToScreenScaleSC = mShader->getShaderConstHandle( "$worldToScreenScale" );
  415. mMatWorldToScreenSC = mShader->getShaderConstHandle( "$matWorldToScreen" );
  416. mMatScreenToWorldSC = mShader->getShaderConstHandle( "$matScreenToWorld" );
  417. mMatPrevScreenToWorldSC = mShader->getShaderConstHandle( "$matPrevScreenToWorld" );
  418. mWaterColorSC = mShader->getShaderConstHandle( "$waterColor" );
  419. mAmbientColorSC = mShader->getShaderConstHandle( "$ambientColor" );
  420. mWaterFogDataSC = mShader->getShaderConstHandle( "$waterFogData" );
  421. mWaterFogPlaneSC = mShader->getShaderConstHandle( "$waterFogPlane" );
  422. mWaterDepthGradMaxSC = mShader->getShaderConstHandle( "$waterDepthGradMax" );
  423. mScreenSunPosSC = mShader->getShaderConstHandle( "$screenSunPos" );
  424. mLightDirectionSC = mShader->getShaderConstHandle( "$lightDirection" );
  425. mCameraForwardSC = mShader->getShaderConstHandle( "$camForward" );
  426. mAccumTimeSC = mShader->getShaderConstHandle( "$accumTime" );
  427. mDeltaTimeSC = mShader->getShaderConstHandle( "$deltaTime" );
  428. mInvCameraMatSC = mShader->getShaderConstHandle( "$invCameraMat" );
  429. }
  430. // Set up shader constants for source image size
  431. if ( mRTSizeSC->isValid() )
  432. {
  433. const Point2I &resolution = GFX->getActiveRenderTarget()->getSize();
  434. Point2F pixelShaderConstantData;
  435. pixelShaderConstantData.x = resolution.x;
  436. pixelShaderConstantData.y = resolution.y;
  437. mShaderConsts->set( mRTSizeSC, pixelShaderConstantData );
  438. }
  439. if ( mOneOverRTSizeSC->isValid() )
  440. {
  441. const Point2I &resolution = GFX->getActiveRenderTarget()->getSize();
  442. Point2F oneOverTargetSize( 1.0f / (F32)resolution.x, 1.0f / (F32)resolution.y );
  443. mShaderConsts->set( mOneOverRTSizeSC, oneOverTargetSize );
  444. }
  445. // Set up additional textures
  446. Point2F texSizeConst;
  447. for( U32 i = 0; i < NumTextures; i++ )
  448. {
  449. if( !mActiveTextures[i] )
  450. continue;
  451. if ( mTexSizeSC[i]->isValid() )
  452. {
  453. texSizeConst.x = (F32)mActiveTextures[i]->getWidth();
  454. texSizeConst.y = (F32)mActiveTextures[i]->getHeight();
  455. mShaderConsts->set( mTexSizeSC[i], texSizeConst );
  456. }
  457. }
  458. for ( U32 i = 0; i < NumTextures; i++ )
  459. {
  460. if ( !mRenderTargetParamsSC[i]->isValid() )
  461. continue;
  462. Point4F rtParams( Point4F::One );
  463. if ( mActiveTextures[i] )
  464. {
  465. const Point3I &targetSz = mActiveTextures[i]->getSize();
  466. RectI targetVp = mActiveTextureViewport[i];
  467. ScreenSpace::RenderTargetParameters(targetSz, targetVp, rtParams);
  468. }
  469. mShaderConsts->set( mRenderTargetParamsSC[i], rtParams );
  470. }
  471. // Set the fog data.
  472. if ( mFogDataSC->isValid() )
  473. {
  474. const FogData &data = state->getSceneManager()->getFogData();
  475. Point3F params;
  476. params.x = data.density;
  477. params.y = data.densityOffset;
  478. if ( !mIsZero( data.atmosphereHeight ) )
  479. params.z = 1.0f / data.atmosphereHeight;
  480. else
  481. params.z = 0.0f;
  482. mShaderConsts->set( mFogDataSC, params );
  483. }
  484. const PFXFrameState &thisFrame = PFXMGR->getFrameState();
  485. if ( mMatWorldToScreenSC->isValid() )
  486. {
  487. // Screen space->world space
  488. MatrixF tempMat = thisFrame.cameraToScreen;
  489. tempMat.mul( thisFrame.worldToCamera );
  490. tempMat.fullInverse();
  491. tempMat.transpose();
  492. // Support using these matrices as float3x3 or float4x4...
  493. mShaderConsts->set( mMatWorldToScreenSC, tempMat, mMatWorldToScreenSC->getType() );
  494. }
  495. if ( mMatScreenToWorldSC->isValid() )
  496. {
  497. // World space->screen space
  498. MatrixF tempMat = thisFrame.cameraToScreen;
  499. tempMat.mul( thisFrame.worldToCamera );
  500. tempMat.transpose();
  501. // Support using these matrices as float3x3 or float4x4...
  502. mShaderConsts->set( mMatScreenToWorldSC, tempMat, mMatScreenToWorldSC->getType() );
  503. }
  504. if ( mMatPrevScreenToWorldSC->isValid() )
  505. {
  506. const PFXFrameState &lastFrame = PFXMGR->getLastFrameState();
  507. // Previous frame world space->screen space
  508. MatrixF tempMat = lastFrame.cameraToScreen;
  509. tempMat.mul( lastFrame.worldToCamera );
  510. tempMat.transpose();
  511. mShaderConsts->set( mMatPrevScreenToWorldSC, tempMat );
  512. }
  513. if ( mAmbientColorSC->isValid() )
  514. {
  515. const ColorF &sunlight = state->getAmbientLightColor();
  516. Point3F ambientColor( sunlight.red, sunlight.green, sunlight.blue );
  517. mShaderConsts->set( mAmbientColorSC, ambientColor );
  518. }
  519. mShaderConsts->setSafe( mAccumTimeSC, MATMGR->getTotalTime() );
  520. mShaderConsts->setSafe( mDeltaTimeSC, MATMGR->getDeltaTime() );
  521. // Now set all the constants that are dependent on the scene state.
  522. if ( state )
  523. {
  524. mShaderConsts->setSafe( mEyePosSC, state->getDiffuseCameraPosition() );
  525. mShaderConsts->setSafe( mNearFarSC, Point2F( state->getNearPlane(), state->getFarPlane() ) );
  526. mShaderConsts->setSafe( mInvNearFarSC, Point2F( 1.0f / state->getNearPlane(), 1.0f / state->getFarPlane() ) );
  527. mShaderConsts->setSafe( mWorldToScreenScaleSC, state->getWorldToScreenScale() );
  528. mShaderConsts->setSafe( mFogColorSC, state->getSceneManager()->getFogData().color );
  529. if ( mWaterColorSC->isValid() )
  530. {
  531. ColorF color( state->getSceneManager()->getWaterFogData().color );
  532. mShaderConsts->set( mWaterColorSC, color );
  533. }
  534. if ( mWaterFogDataSC->isValid() )
  535. {
  536. const WaterFogData &data = state->getSceneManager()->getWaterFogData();
  537. Point4F params( data.density, data.densityOffset, data.wetDepth, data.wetDarkening );
  538. mShaderConsts->set( mWaterFogDataSC, params );
  539. }
  540. if ( mWaterFogPlaneSC->isValid() )
  541. {
  542. const PlaneF &plane = state->getSceneManager()->getWaterFogData().plane;
  543. mShaderConsts->set( mWaterFogPlaneSC, plane );
  544. }
  545. if ( mWaterDepthGradMaxSC->isValid() )
  546. {
  547. mShaderConsts->set( mWaterDepthGradMaxSC, state->getSceneManager()->getWaterFogData().depthGradMax );
  548. }
  549. if ( mScreenSunPosSC->isValid() )
  550. {
  551. // Grab our projection matrix
  552. // from the frustum.
  553. Frustum frust = state->getFrustum();
  554. MatrixF proj( true );
  555. frust.getProjectionMatrix( &proj );
  556. // Grab the ScatterSky world matrix.
  557. MatrixF camMat = state->getCameraTransform();
  558. camMat.inverse();
  559. MatrixF tmp( true );
  560. tmp = camMat;
  561. tmp.setPosition( Point3F( 0, 0, 0 ) );
  562. Point3F sunPos( 0, 0, 0 );
  563. // Get the light manager and sun light object.
  564. LightInfo *sunLight = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  565. // Grab the light direction and scale
  566. // by the ScatterSky radius to get the world
  567. // space sun position.
  568. const VectorF &lightDir = sunLight->getDirection();
  569. Point3F lightPos( lightDir.x * (6378.0f * 1000.0f),
  570. lightDir.y * (6378.0f * 1000.0f),
  571. lightDir.z * (6378.0f * 1000.0f) );
  572. // Get the screen space sun position.
  573. MathUtils::mProjectWorldToScreen( lightPos, &sunPos, GFX->getViewport(), tmp, proj );
  574. // And normalize it to the 0 to 1 range.
  575. sunPos.x /= (F32)GFX->getViewport().extent.x;
  576. sunPos.y /= (F32)GFX->getViewport().extent.y;
  577. mShaderConsts->set( mScreenSunPosSC, Point2F( sunPos.x, sunPos.y ) );
  578. }
  579. if ( mLightDirectionSC->isValid() )
  580. {
  581. LightInfo *sunLight = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  582. const VectorF &lightDir = sunLight->getDirection();
  583. mShaderConsts->set( mLightDirectionSC, lightDir );
  584. }
  585. if ( mCameraForwardSC->isValid() )
  586. {
  587. const MatrixF &camMat = state->getCameraTransform();
  588. VectorF camFwd( 0, 0, 0 );
  589. camMat.getColumn( 1, &camFwd );
  590. mShaderConsts->set( mCameraForwardSC, camFwd );
  591. }
  592. if ( mInvCameraMatSC->isValid() )
  593. {
  594. MatrixF mat = state->getCameraTransform();
  595. mat.inverse();
  596. mShaderConsts->set( mInvCameraMatSC, mat, mInvCameraMatSC->getType() );
  597. }
  598. } // if ( state )
  599. // Set EffectConsts - specified from script
  600. // If our shader has reloaded since last frame we must mark all
  601. // EffectConsts dirty so they will be reset.
  602. if ( mShader->getReloadKey() != mShaderReloadKey )
  603. {
  604. mShaderReloadKey = mShader->getReloadKey();
  605. EffectConstTable::Iterator iter = mEffectConsts.begin();
  606. for ( ; iter != mEffectConsts.end(); iter++ )
  607. iter->value->mDirty = true;
  608. }
  609. // Doesn't look like anyone is using this anymore.
  610. // But if we do want to pass this info to script,
  611. // we should do so in the same way as I am doing below.
  612. /*
  613. Point2F texSizeScriptConst( 0, 0 );
  614. String buffer;
  615. if ( mActiveTextures[0] )
  616. {
  617. texSizeScriptConst.x = (F32)mActiveTextures[0]->getWidth();
  618. texSizeScriptConst.y = (F32)mActiveTextures[0]->getHeight();
  619. dSscanf( buffer.c_str(), "%g %g", texSizeScriptConst.x, texSizeScriptConst.y );
  620. }
  621. */
  622. {
  623. PROFILE_SCOPE( PostEffect_SetShaderConsts );
  624. // Pass some data about the current render state to script.
  625. //
  626. // TODO: This is pretty messy... it should go away. This info
  627. // should be available from some other script accessible method
  628. // or field which isn't PostEffect specific.
  629. //
  630. if ( state )
  631. {
  632. Con::setFloatVariable( "$Param::NearDist", state->getNearPlane() );
  633. Con::setFloatVariable( "$Param::FarDist", state->getFarPlane() );
  634. }
  635. setShaderConsts_callback();
  636. }
  637. EffectConstTable::Iterator iter = mEffectConsts.begin();
  638. for ( ; iter != mEffectConsts.end(); iter++ )
  639. iter->value->setToBuffer( mShaderConsts );
  640. }
  641. void PostEffect::_setupTexture( U32 stage, GFXTexHandle &inputTex, const RectI *inTexViewport )
  642. {
  643. const String &texFilename = mTexFilename[ stage ];
  644. GFXTexHandle theTex;
  645. NamedTexTarget *namedTarget = NULL;
  646. RectI viewport = GFX->getViewport();
  647. if ( texFilename.compare( "$inTex", 0, String::NoCase ) == 0 )
  648. {
  649. theTex = inputTex;
  650. if ( inTexViewport )
  651. {
  652. viewport = *inTexViewport;
  653. }
  654. else if ( theTex )
  655. {
  656. viewport.set( 0, 0, theTex->getWidth(), theTex->getHeight() );
  657. }
  658. }
  659. else if ( texFilename.compare( "$backBuffer", 0, String::NoCase ) == 0 )
  660. {
  661. theTex = PFXMGR->getBackBufferTex();
  662. // Always use the GFX viewport when reading from the backbuffer
  663. }
  664. else if ( texFilename.isNotEmpty() && texFilename[0] == '#' )
  665. {
  666. namedTarget = NamedTexTarget::find( texFilename.c_str() + 1 );
  667. if ( namedTarget )
  668. {
  669. theTex = namedTarget->getTexture( 0 );
  670. viewport = namedTarget->getViewport();
  671. }
  672. }
  673. else
  674. {
  675. theTex = mTextures[ stage ];
  676. if ( theTex )
  677. viewport.set( 0, 0, theTex->getWidth(), theTex->getHeight() );
  678. }
  679. mActiveTextures[ stage ] = theTex;
  680. mActiveNamedTarget[ stage ] = namedTarget;
  681. mActiveTextureViewport[ stage ] = viewport;
  682. if ( theTex.isValid() )
  683. GFX->setTexture( stage, theTex );
  684. }
  685. void PostEffect::_setupTransforms()
  686. {
  687. // Set everything to identity.
  688. GFX->setWorldMatrix( MatrixF::Identity );
  689. GFX->setProjectionMatrix( MatrixF::Identity );
  690. }
  691. void PostEffect::_setupTarget( const SceneRenderState *state, bool *outClearTarget )
  692. {
  693. if ( mNamedTarget.isRegistered() ||
  694. mTargetName.compare( "$outTex", 0, String::NoCase ) == 0 )
  695. {
  696. // Size it relative to the texture of the first stage or
  697. // if NULL then use the current target.
  698. Point2I targetSize;
  699. // If we have an absolute target size then use that.
  700. if ( !mTargetSize.isZero() )
  701. targetSize = mTargetSize;
  702. // Else generate a relative size using the target scale.
  703. else if ( mActiveTextures[ 0 ] )
  704. {
  705. const Point3I &texSize = mActiveTextures[ 0 ]->getSize();
  706. targetSize.set( texSize.x * mTargetScale.x,
  707. texSize.y * mTargetScale.y );
  708. }
  709. else
  710. {
  711. GFXTarget *oldTarget = GFX->getActiveRenderTarget();
  712. const Point2I &oldTargetSize = oldTarget->getSize();
  713. targetSize.set( oldTargetSize.x * mTargetScale.x,
  714. oldTargetSize.y * mTargetScale.y );
  715. }
  716. // Make sure its at least 1x1.
  717. targetSize.setMax( Point2I::One );
  718. if ( mNamedTarget.isRegistered() ||
  719. !mTargetTex ||
  720. mTargetTex.getWidthHeight() != targetSize )
  721. {
  722. mTargetTex.set( targetSize.x, targetSize.y, mTargetFormat,
  723. &PostFxTargetProfile, "PostEffect::_setupTarget" );
  724. if ( mTargetClear == PFXTargetClear_OnCreate )
  725. *outClearTarget = true;
  726. mNamedTarget.setViewport( RectI( 0, 0, targetSize.x, targetSize.y ) );
  727. }
  728. }
  729. else
  730. mTargetTex = NULL;
  731. // Do we have a named depthStencil target?
  732. if ( mNamedTargetDepthStencil.isRegistered() )
  733. {
  734. // Size it relative to the texture of the first stage or
  735. // if NULL then use the current target.
  736. Point2I targetSize;
  737. // If we have an absolute target size then use that.
  738. if ( !mTargetSize.isZero() )
  739. targetSize = mTargetSize;
  740. // Else generate a relative size using the target scale.
  741. else if ( mActiveTextures[ 0 ] )
  742. {
  743. const Point3I &texSize = mActiveTextures[ 0 ]->getSize();
  744. targetSize.set( texSize.x * mTargetScale.x,
  745. texSize.y * mTargetScale.y );
  746. }
  747. else
  748. {
  749. GFXTarget *oldTarget = GFX->getActiveRenderTarget();
  750. const Point2I &oldTargetSize = oldTarget->getSize();
  751. targetSize.set( oldTargetSize.x * mTargetScale.x,
  752. oldTargetSize.y * mTargetScale.y );
  753. }
  754. // Make sure its at least 1x1.
  755. targetSize.setMax( Point2I::One );
  756. if ( mNamedTargetDepthStencil.isRegistered() &&
  757. mTargetDepthStencil.getWidthHeight() != targetSize )
  758. {
  759. mTargetDepthStencil.set( targetSize.x, targetSize.y, GFXFormatD24S8,
  760. &GFXDefaultZTargetProfile, "PostEffect::_setupTarget" );
  761. if ( mTargetClear == PFXTargetClear_OnCreate )
  762. *outClearTarget = true;
  763. mNamedTargetDepthStencil.setViewport( RectI( 0, 0, targetSize.x, targetSize.y ) );
  764. }
  765. }
  766. else
  767. mTargetDepthStencil = NULL;
  768. if ( mTargetClear == PFXTargetClear_OnDraw )
  769. *outClearTarget = true;
  770. if ( !mTarget && (mTargetTex || mTargetDepthStencil) )
  771. mTarget = GFX->allocRenderToTextureTarget();
  772. }
  773. void PostEffect::_cleanTargets( bool recurse )
  774. {
  775. mTargetTex = NULL;
  776. mTargetDepthStencil = NULL;
  777. mTarget = NULL;
  778. if ( !recurse )
  779. return;
  780. // Clear the children too!
  781. for ( U32 i = 0; i < size(); i++ )
  782. {
  783. PostEffect *effect = (PostEffect*)(*this)[i];
  784. effect->_cleanTargets( true );
  785. }
  786. }
  787. void PostEffect::process( const SceneRenderState *state,
  788. GFXTexHandle &inOutTex,
  789. const RectI *inTexViewport )
  790. {
  791. // If the shader is forced to be skipped... then skip.
  792. if ( mSkip )
  793. return;
  794. // Skip out if we don't support reflection passes.
  795. if ( state && state->isReflectPass() && !mAllowReflectPass )
  796. return;
  797. if ( mOneFrameOnly && !mOnThisFrame )
  798. return;
  799. // Check requirements if the shader needs updating.
  800. if ( mUpdateShader )
  801. {
  802. _checkRequirements();
  803. // Clear the targets if we failed passing
  804. // the requirements at this time.
  805. if ( !mIsValid )
  806. _cleanTargets( true );
  807. }
  808. // If we're not valid then we cannot render.
  809. if ( !mIsValid )
  810. return;
  811. GFXDEBUGEVENT_SCOPE_EX( PostEffect_Process, ColorI::GREEN, avar("PostEffect: %s", getName()) );
  812. preProcess_callback();
  813. GFXTransformSaver saver;
  814. // Set the textures.
  815. for ( U32 i = 0; i < NumTextures; i++ )
  816. _setupTexture( i, inOutTex, inTexViewport );
  817. _setupStateBlock( state ) ;
  818. _setupTransforms();
  819. bool clearTarget = false;
  820. _setupTarget( state, &clearTarget );
  821. if ( mTargetTex || mTargetDepthStencil )
  822. {
  823. #ifdef TORQUE_OS_XENON
  824. // You may want to disable this functionality for speed reasons as it does
  825. // add some overhead. The upside is it makes things "just work". If you
  826. // re-work your post-effects properly, this is not needed.
  827. //
  828. // If this post effect doesn't alpha blend to the back-buffer, than preserve
  829. // the active render target contents so they are still around the next time
  830. // that render target activates
  831. if(!mStateBlockData->getState().blendEnable)
  832. GFX->getActiveRenderTarget()->preserve();
  833. #endif
  834. GFX->pushActiveRenderTarget();
  835. mTarget->attachTexture( GFXTextureTarget::Color0, mTargetTex );
  836. // Set the right depth stencil target.
  837. if ( !mTargetDepthStencil && mTargetTex.getWidthHeight() == GFX->getActiveRenderTarget()->getSize() )
  838. mTarget->attachTexture( GFXTextureTarget::DepthStencil, GFXTextureTarget::sDefaultDepthStencil );
  839. else
  840. mTarget->attachTexture( GFXTextureTarget::DepthStencil, mTargetDepthStencil );
  841. GFX->setActiveRenderTarget( mTarget );
  842. }
  843. if ( clearTarget )
  844. GFX->clear( GFXClearTarget, mTargetClearColor, 1.f, 0 );
  845. // Setup the shader and constants.
  846. if ( mShader )
  847. {
  848. _setupConstants( state );
  849. GFX->setShader( mShader );
  850. GFX->setShaderConstBuffer( mShaderConsts );
  851. }
  852. else
  853. GFX->disableShaders();
  854. Frustum frustum;
  855. if ( state )
  856. frustum = state->getFrustum();
  857. else
  858. {
  859. // If we don't have a scene state then setup
  860. // a dummy frustum... you better not be depending
  861. // on this being related to the camera in any way.
  862. frustum = Frustum();
  863. }
  864. GFXVertexBufferHandle<PFXVertex> vb;
  865. _updateScreenGeometry( frustum, &vb );
  866. // Draw it.
  867. GFX->setVertexBuffer( vb );
  868. GFX->drawPrimitive( GFXTriangleFan, 0, 2 );
  869. // Allow PostEffecVis to hook in.
  870. PFXVIS->onPFXProcessed( this );
  871. if ( mTargetTex || mTargetDepthStencil )
  872. {
  873. mTarget->resolve();
  874. GFX->popActiveRenderTarget();
  875. }
  876. else
  877. {
  878. // We wrote to the active back buffer, so release
  879. // the current texture copy held by the manager.
  880. //
  881. // This ensures a new copy is made.
  882. PFXMGR->releaseBackBufferTex();
  883. }
  884. // Return and release our target texture.
  885. inOutTex = mTargetTex;
  886. if ( !mNamedTarget.isRegistered() )
  887. mTargetTex = NULL;
  888. // Restore the transforms before the children
  889. // are processed as it screws up the viewport.
  890. saver.restore();
  891. // Now process my children.
  892. iterator i = begin();
  893. for ( ; i != end(); i++ )
  894. {
  895. PostEffect *effect = static_cast<PostEffect*>(*i);
  896. effect->process( state, inOutTex );
  897. }
  898. if ( mOneFrameOnly )
  899. mOnThisFrame = false;
  900. }
  901. bool PostEffect::_setIsEnabled( void *object, const char *index, const char *data )
  902. {
  903. bool enabled = dAtob( data );
  904. if ( enabled )
  905. static_cast<PostEffect*>( object )->enable();
  906. else
  907. static_cast<PostEffect*>( object )->disable();
  908. // Always return false from a protected field.
  909. return false;
  910. }
  911. void PostEffect::enable()
  912. {
  913. // Don't add TexGen PostEffects to the PostEffectManager!
  914. if ( mRenderTime == PFXTexGenOnDemand )
  915. return;
  916. // Ignore it if its already enabled.
  917. if ( mEnabled )
  918. return;
  919. mEnabled = true;
  920. // We cannot really enable the effect
  921. // until its been registed.
  922. if ( !isProperlyAdded() )
  923. return;
  924. // If the enable callback returns 'false' then
  925. // leave the effect disabled.
  926. if ( !onEnabled_callback() )
  927. {
  928. mEnabled = false;
  929. return;
  930. }
  931. PFXMGR->_addEffect( this );
  932. }
  933. void PostEffect::disable()
  934. {
  935. if ( !mEnabled )
  936. return;
  937. mEnabled = false;
  938. _cleanTargets( true );
  939. if ( isProperlyAdded() )
  940. {
  941. PFXMGR->_removeEffect( this );
  942. onDisabled_callback();
  943. }
  944. }
  945. void PostEffect::reload()
  946. {
  947. // Reload the shader if we have one or mark it
  948. // for updating when its processed next.
  949. if ( mShader )
  950. mShader->reload();
  951. else
  952. mUpdateShader = true;
  953. // Null stateblock so it is reloaded.
  954. mStateBlock = NULL;
  955. // Call reload on any children
  956. // this PostEffect may have.
  957. for ( U32 i = 0; i < size(); i++ )
  958. {
  959. PostEffect *effect = (PostEffect*)(*this)[i];
  960. effect->reload();
  961. }
  962. }
  963. void PostEffect::setTexture( U32 index, const String &texFilePath )
  964. {
  965. // Set the new texture name.
  966. mTexFilename[index] = texFilePath;
  967. mTextures[index].free();
  968. // Skip empty stages or ones with variable or target names.
  969. if ( texFilePath.isEmpty() ||
  970. texFilePath[0] == '$' ||
  971. texFilePath[0] == '#' )
  972. return;
  973. // Try to load the texture.
  974. mTextures[index].set( texFilePath, &PostFxTextureProfile, avar( "%s() - (line %d)", __FUNCTION__, __LINE__ ) );
  975. }
  976. void PostEffect::setShaderConst( const String &name, const String &val )
  977. {
  978. PROFILE_SCOPE( PostEffect_SetShaderConst );
  979. EffectConstTable::Iterator iter = mEffectConsts.find( name );
  980. if ( iter == mEffectConsts.end() )
  981. {
  982. EffectConst *newConst = new EffectConst( name, val );
  983. iter = mEffectConsts.insertUnique( name, newConst );
  984. }
  985. iter->value->set( val );
  986. }
  987. F32 PostEffect::getAspectRatio() const
  988. {
  989. const Point2I &rtSize = GFX->getActiveRenderTarget()->getSize();
  990. return (F32)rtSize.x / (F32)rtSize.y;
  991. }
  992. void PostEffect::_checkRequirements()
  993. {
  994. // This meets requirements if its shader loads
  995. // properly, we can find all the input textures,
  996. // and its formats are supported.
  997. mIsValid = false;
  998. mUpdateShader = false;
  999. mShader = NULL;
  1000. // First make sure the target format is supported.
  1001. if ( mNamedTarget.isRegistered() )
  1002. {
  1003. Vector<GFXFormat> formats;
  1004. formats.push_back( mTargetFormat );
  1005. GFXFormat format = GFX->selectSupportedFormat( &PostFxTargetProfile,
  1006. formats,
  1007. true,
  1008. false,
  1009. false );
  1010. // If we didn't get our format out then its unsupported!
  1011. if ( format != mTargetFormat )
  1012. return;
  1013. }
  1014. // Gather macros specified on this PostEffect.
  1015. Vector<GFXShaderMacro> macros( mShaderMacros );
  1016. // Now check the input named targets and make sure
  1017. // they exist... else we're invalid.
  1018. for ( U32 i=0; i < NumTextures; i++ )
  1019. {
  1020. const String &texFilename = mTexFilename[i];
  1021. if ( texFilename.isNotEmpty() && texFilename[0] == '#' )
  1022. {
  1023. NamedTexTarget *namedTarget = NamedTexTarget::find( texFilename.c_str() + 1 );
  1024. if ( !namedTarget )
  1025. return;
  1026. // Grab the macros for shader initialization.
  1027. namedTarget->getShaderMacros( &macros );
  1028. }
  1029. }
  1030. // Finally find and load the shader.
  1031. ShaderData *shaderData;
  1032. if ( Sim::findObject( mShaderName, shaderData ) )
  1033. if ( shaderData->getPixVersion() <= GFX->getPixelShaderVersion() )
  1034. mShader = shaderData->getShader( macros );
  1035. // If we didn't get a shader... we're done.
  1036. if ( !mShader )
  1037. return;
  1038. // If we got here then we're valid.
  1039. mIsValid = true;
  1040. }
  1041. bool PostEffect::dumpShaderDisassembly( String &outFilename ) const
  1042. {
  1043. String data;
  1044. if ( !mShader || !mShader->getDisassembly( data ) )
  1045. return false;
  1046. outFilename = FS::MakeUniquePath( "", "ShaderDisassembly", "txt" );
  1047. FileStream *fstream = FileStream::createAndOpen( outFilename, Torque::FS::File::Write );
  1048. if ( !fstream )
  1049. return false;
  1050. fstream->write( data );
  1051. fstream->close();
  1052. delete fstream;
  1053. return true;
  1054. }
  1055. SimSet* PostEffect::getSet() const
  1056. {
  1057. SimSet *set;
  1058. if ( !Sim::findObject( "PFXSet", set ) )
  1059. {
  1060. set = new SimSet();
  1061. set->registerObject( "PFXSet" );
  1062. Sim::getRootGroup()->addObject( set );
  1063. }
  1064. return set;
  1065. }
  1066. void PostEffect::setShaderMacro( const String &name, const String &value )
  1067. {
  1068. // Check to see if we already have this macro.
  1069. Vector<GFXShaderMacro>::iterator iter = mShaderMacros.begin();
  1070. for ( ; iter != mShaderMacros.end(); iter++ )
  1071. {
  1072. if ( iter->name == name )
  1073. {
  1074. if ( iter->value != value )
  1075. {
  1076. iter->value = value;
  1077. mUpdateShader = true;
  1078. }
  1079. return;
  1080. }
  1081. }
  1082. // Add a new macro.
  1083. mShaderMacros.increment();
  1084. mShaderMacros.last().name = name;
  1085. mShaderMacros.last().value = value;
  1086. mUpdateShader = true;
  1087. }
  1088. bool PostEffect::removeShaderMacro( const String &name )
  1089. {
  1090. Vector<GFXShaderMacro>::iterator iter = mShaderMacros.begin();
  1091. for ( ; iter != mShaderMacros.end(); iter++ )
  1092. {
  1093. if ( iter->name == name )
  1094. {
  1095. mShaderMacros.erase( iter );
  1096. mUpdateShader = true;
  1097. return true;
  1098. }
  1099. }
  1100. return false;
  1101. }
  1102. void PostEffect::clearShaderMacros()
  1103. {
  1104. if ( mShaderMacros.empty() )
  1105. return;
  1106. mShaderMacros.clear();
  1107. mUpdateShader = true;
  1108. }
  1109. GFXTextureObject* PostEffect::_getTargetTexture( U32 )
  1110. {
  1111. // A TexGen PostEffect will generate its texture now if it
  1112. // has not already.
  1113. if ( mRenderTime == PFXTexGenOnDemand &&
  1114. ( !mTargetTex || mUpdateShader ) )
  1115. {
  1116. GFXTexHandle chainTex;
  1117. process( NULL, chainTex );
  1118. // TODO: We should add a conditional copy
  1119. // to a non-RT texture here to reduce the
  1120. // amount of non-swappable RTs in use.
  1121. }
  1122. return mTargetTex.getPointer();
  1123. }
  1124. DefineEngineMethod( PostEffect, reload, void, (),,
  1125. "Reloads the effect shader and textures." )
  1126. {
  1127. return object->reload();
  1128. }
  1129. DefineEngineMethod( PostEffect, enable, void, (),,
  1130. "Enables the effect." )
  1131. {
  1132. object->enable();
  1133. }
  1134. DefineEngineMethod( PostEffect, disable, void, (),,
  1135. "Disables the effect." )
  1136. {
  1137. object->disable();
  1138. }
  1139. DefineEngineMethod( PostEffect, toggle, bool, (),,
  1140. "Toggles the effect between enabled / disabled.\n"
  1141. "@return True if effect is enabled." )
  1142. {
  1143. if ( object->isEnabled() )
  1144. object->disable();
  1145. else
  1146. object->enable();
  1147. return object->isEnabled();
  1148. }
  1149. DefineEngineMethod( PostEffect, isEnabled, bool, (),,
  1150. "@return True if the effect is enabled." )
  1151. {
  1152. return object->isEnabled();
  1153. }
  1154. DefineEngineMethod( PostEffect, setTexture, void, ( S32 index, const char *filePath ),,
  1155. "This is used to set the texture file and load the texture on a running effect. "
  1156. "If the texture file is not different from the current file nothing is changed. If "
  1157. "the texture cannot be found a null texture is assigned.\n"
  1158. "@param index The texture stage index.\n"
  1159. "@param filePath The file name of the texture to set.\n" )
  1160. {
  1161. if ( index > -1 && index < PostEffect::NumTextures )
  1162. object->setTexture( index, filePath );
  1163. }
  1164. DefineEngineMethod( PostEffect, setShaderConst, void, ( const char* name, const char* value ),,
  1165. "Sets the value of a uniform defined in the shader. This will usually "
  1166. "be called within the setShaderConsts callback. Array type constants are "
  1167. "not supported.\n"
  1168. "@param name Name of the constanst, prefixed with '$'.\n"
  1169. "@param value Value to set, space seperate values with more than one element.\n"
  1170. "@tsexample\n"
  1171. "function MyPfx::setShaderConsts( %this )\n"
  1172. "{\n"
  1173. " // example float4 uniform\n"
  1174. " %this.setShaderConst( \"$colorMod\", \"1.0 0.9 1.0 1.0\" );\n"
  1175. " // example float1 uniform\n"
  1176. " %this.setShaderConst( \"$strength\", \"3.0\" );\n"
  1177. " // example integer uniform\n"
  1178. " %this.setShaderConst( \"$loops\", \"5\" );"
  1179. "}\n"
  1180. "@endtsexample" )
  1181. {
  1182. object->setShaderConst( name, value );
  1183. }
  1184. DefineEngineMethod( PostEffect, getAspectRatio, F32, (),,
  1185. "@return Width over height of the backbuffer." )
  1186. {
  1187. return object->getAspectRatio();
  1188. }
  1189. DefineEngineMethod( PostEffect, dumpShaderDisassembly, String, (),,
  1190. "Dumps this PostEffect shader's disassembly to a temporary text file.\n"
  1191. "@return Full path to the dumped file or an empty string if failed." )
  1192. {
  1193. String fileName;
  1194. object->dumpShaderDisassembly( fileName );
  1195. return fileName;
  1196. }
  1197. DefineEngineMethod( PostEffect, setShaderMacro, void, ( const char* key, const char* value ), ( "" ),
  1198. "Adds a macro to the effect's shader or sets an existing one's value. "
  1199. "This will usually be called within the onAdd or preProcess callback.\n"
  1200. "@param key lval of the macro."
  1201. "@param value rval of the macro, or may be empty."
  1202. "@tsexample\n"
  1203. "function MyPfx::onAdd( %this )\n"
  1204. "{\n"
  1205. " %this.setShaderMacro( \"NUM_SAMPLES\", \"10\" );\n"
  1206. " %this.setShaderMacro( \"HIGH_QUALITY_MODE\" );\n"
  1207. " \n"
  1208. " // In the shader looks like... \n"
  1209. " // #define NUM_SAMPLES 10\n"
  1210. " // #define HIGH_QUALITY_MODE\n"
  1211. "}\n"
  1212. "@endtsexample" )
  1213. {
  1214. object->setShaderMacro( key, value );
  1215. }
  1216. DefineEngineMethod( PostEffect, removeShaderMacro, void, ( const char* key ),,
  1217. "Remove a shader macro. This will usually be called within the preProcess callback.\n"
  1218. "@param key Macro to remove." )
  1219. {
  1220. object->removeShaderMacro( key );
  1221. }
  1222. DefineEngineMethod( PostEffect, clearShaderMacros, void, (),,
  1223. "Remove all shader macros." )
  1224. {
  1225. object->clearShaderMacros();
  1226. }
  1227. DefineEngineFunction( dumpRandomNormalMap, void, (),,
  1228. "Creates a 64x64 normal map texture filled with noise. The texture is saved "
  1229. "to randNormTex.png in the location of the game executable.\n\n"
  1230. "@ingroup GFX")
  1231. {
  1232. GFXTexHandle tex;
  1233. tex.set( 64, 64, GFXFormatR8G8B8A8, &GFXDefaultPersistentProfile, "" );
  1234. GFXLockedRect *rect = tex.lock();
  1235. U8 *f = rect->bits;
  1236. for ( U32 i = 0; i < 64*64; i++, f += 4 )
  1237. {
  1238. VectorF vec;
  1239. vec.x = mRandF( -1.0f, 1.0f );
  1240. vec.y = mRandF( -1.0f, 1.0f );
  1241. vec.z = mRandF( -1.0f, 1.0f );
  1242. vec.normalizeSafe();
  1243. f[0] = U8_MAX * ( ( 1.0f + vec.x ) * 0.5f );
  1244. f[1] = U8_MAX * ( ( 1.0f + vec.y ) * 0.5f );
  1245. f[2] = U8_MAX * ( ( 1.0f + vec.z ) * 0.5f );
  1246. f[3] = U8_MAX;
  1247. }
  1248. tex.unlock();
  1249. String path = Torque::FS::MakeUniquePath( "", "randNormTex", "png" );
  1250. tex->dumpToDisk( "png", path );
  1251. }