postEffect.cpp 54 KB

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