postEffect.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  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. GFX_ImplementTextureProfile( PostFxTextureSRGBProfile,
  127. GFXTextureProfile::DiffuseMap,
  128. GFXTextureProfile::Static | GFXTextureProfile::PreserveSize | GFXTextureProfile::NoMipmap | GFXTextureProfile::SRGB,
  129. GFXTextureProfile::NONE);
  130. GFX_ImplementTextureProfile( VRTextureProfile,
  131. GFXTextureProfile::DiffuseMap,
  132. GFXTextureProfile::PreserveSize |
  133. GFXTextureProfile::RenderTarget |
  134. GFXTextureProfile::NoMipmap,
  135. GFXTextureProfile::NONE );
  136. GFX_ImplementTextureProfile( VRDepthProfile,
  137. GFXTextureProfile::DiffuseMap,
  138. GFXTextureProfile::PreserveSize |
  139. GFXTextureProfile::NoMipmap |
  140. GFXTextureProfile::ZTarget,
  141. GFXTextureProfile::NONE );
  142. void PostEffect::EffectConst::set( const String &newVal )
  143. {
  144. if ( mStringVal == newVal )
  145. return;
  146. mStringVal = newVal;
  147. mDirty = true;
  148. mValueType = StringType;
  149. }
  150. void PostEffect::EffectConst::set(const F32 &newVal)
  151. {
  152. if (mFloatVal == newVal)
  153. return;
  154. mFloatVal = newVal;
  155. mDirty = true;
  156. mValueType = FloatType;
  157. }
  158. void PostEffect::EffectConst::set(const int& newVal)
  159. {
  160. if (mIntVal == newVal)
  161. return;
  162. mIntVal = newVal;
  163. mDirty = true;
  164. mValueType = IntType;
  165. }
  166. void PostEffect::EffectConst::set(const Point4F &newVal)
  167. {
  168. if (mPointVal == newVal)
  169. return;
  170. mPointVal = newVal;
  171. mDirty = true;
  172. mValueType = PointType;
  173. }
  174. void PostEffect::EffectConst::set(const MatrixF &newVal)
  175. {
  176. if (mMatrixVal == newVal)
  177. return;
  178. mMatrixVal = newVal;
  179. mDirty = true;
  180. mValueType = MatrixType;
  181. }
  182. void PostEffect::EffectConst::set(const Vector<Point4F> &newVal)
  183. {
  184. //if (mPointArrayVal == newVal)
  185. // return;
  186. mPointArrayVal = newVal;
  187. mDirty = true;
  188. mValueType = PointArrayType;
  189. }
  190. void PostEffect::EffectConst::set(const Vector<MatrixF> &newVal)
  191. {
  192. //if (mMatrixArrayVal == newVal)
  193. // return;
  194. mMatrixArrayVal = newVal;
  195. mDirty = true;
  196. mValueType = MatrixArrayType;
  197. }
  198. void PostEffect::EffectConst::setToBuffer( GFXShaderConstBufferRef buff )
  199. {
  200. // Nothing to do if the value hasn't changed.
  201. if ( !mDirty )
  202. return;
  203. mDirty = false;
  204. // If we don't have a handle... get it now.
  205. if ( !mHandle )
  206. mHandle = buff->getShader()->getShaderConstHandle( mName );
  207. // If the handle isn't valid then we're done.
  208. if ( !mHandle->isValid() )
  209. return;
  210. const GFXShaderConstType type = mHandle->getType();
  211. // For now, we're only going
  212. // to support float4 arrays.
  213. // Expand to other types as necessary.
  214. U32 arraySize = mHandle->getArraySize();
  215. if (mValueType == StringType)
  216. {
  217. const char *strVal = mStringVal.c_str();
  218. if (type == GFXSCT_Int)
  219. {
  220. S32 val;
  221. Con::setData(TypeS32, &val, 0, 1, &strVal);
  222. buff->set(mHandle, val);
  223. }
  224. else if (type == GFXSCT_Float)
  225. {
  226. F32 val;
  227. Con::setData(TypeF32, &val, 0, 1, &strVal);
  228. buff->set(mHandle, val);
  229. }
  230. else if (type == GFXSCT_Float2)
  231. {
  232. Point2F val;
  233. Con::setData(TypePoint2F, &val, 0, 1, &strVal);
  234. buff->set(mHandle, val);
  235. }
  236. else if (type == GFXSCT_Float3)
  237. {
  238. Point3F val;
  239. Con::setData(TypePoint3F, &val, 0, 1, &strVal);
  240. buff->set(mHandle, val);
  241. }
  242. else if (type == GFXSCT_Float4)
  243. {
  244. Point4F val;
  245. if (arraySize > 1)
  246. {
  247. // Do array setup!
  248. //U32 unitCount = StringUnit::getUnitCount( strVal, "\t" );
  249. //AssertFatal( unitCount == arraySize, "" );
  250. String tmpString;
  251. Vector<Point4F> valArray;
  252. for (U32 i = 0; i < arraySize; i++)
  253. {
  254. tmpString = StringUnit::getUnit(strVal, i, "\t");
  255. valArray.increment();
  256. const char *tmpCStr = tmpString.c_str();
  257. Con::setData(TypePoint4F, &valArray.last(), 0, 1, &tmpCStr);
  258. }
  259. AlignedArray<Point4F> rectData(valArray.size(), sizeof(Point4F), (U8*)valArray.address(), false);
  260. buff->set(mHandle, rectData);
  261. }
  262. else
  263. {
  264. // Do regular setup.
  265. Con::setData(TypePoint4F, &val, 0, 1, &strVal);
  266. buff->set(mHandle, val);
  267. }
  268. }
  269. else
  270. {
  271. #if TORQUE_DEBUG
  272. const char* err = avar("PostEffect::EffectConst::setToBuffer $s type is not implemented", mName.c_str());
  273. Con::errorf(err);
  274. GFXAssertFatal(0, err);
  275. #endif
  276. }
  277. }
  278. else if (mValueType == FloatType)
  279. {
  280. if (type == GFXSCT_Float)
  281. {
  282. buff->set(mHandle, mFloatVal);
  283. }
  284. else
  285. {
  286. #if TORQUE_DEBUG
  287. const char* err = avar("PostEffect::EffectConst::setToBuffer $s type is not implemented", mName.c_str());
  288. Con::errorf(err);
  289. GFXAssertFatal(0, err);
  290. #endif
  291. }
  292. }
  293. else if (mValueType == IntType)
  294. {
  295. if (type == GFXSCT_Int)
  296. {
  297. buff->set(mHandle, mIntVal);
  298. }
  299. else
  300. {
  301. #if TORQUE_DEBUG
  302. const char* err = avar("PostEffect::EffectConst::setToBuffer $s type is not implemented", mName.c_str());
  303. Con::errorf(err);
  304. GFXAssertFatal(0, err);
  305. #endif
  306. }
  307. }
  308. else if (mValueType == PointType)
  309. {
  310. if (type == GFXSCT_Float2)
  311. {
  312. buff->set(mHandle, Point2F(mPointVal.x, mPointVal.y));
  313. }
  314. else if (type == GFXSCT_Float3)
  315. {
  316. buff->set(mHandle, Point3F(mPointVal.x, mPointVal.y, mPointVal.z));
  317. }
  318. else if (type == GFXSCT_Float4)
  319. {
  320. buff->set(mHandle, mPointVal);
  321. }
  322. else
  323. {
  324. #if TORQUE_DEBUG
  325. const char* err = avar("PostEffect::EffectConst::setToBuffer $s type is not implemented", mName.c_str());
  326. Con::errorf(err);
  327. GFXAssertFatal(0, err);
  328. #endif
  329. }
  330. }
  331. else if (mValueType == MatrixType)
  332. {
  333. if (type == GFXSCT_Float4x4)
  334. {
  335. buff->set(mHandle, mMatrixVal);
  336. }
  337. else
  338. {
  339. #if TORQUE_DEBUG
  340. const char* err = avar("PostEffect::EffectConst::setToBuffer $s type is not implemented", mName.c_str());
  341. Con::errorf(err);
  342. GFXAssertFatal(0, err);
  343. #endif
  344. }
  345. }
  346. else if (mValueType == PointArrayType)
  347. {
  348. if (type == GFXSCT_Float4)
  349. {
  350. if (arraySize != mPointArrayVal.size())
  351. {
  352. #if TORQUE_DEBUG
  353. const char* err = avar("PostEffect::EffectConst::setToBuffer PointArrayType, attempted to feed an array that does not match the uniform array's size!");
  354. Con::errorf(err);
  355. GFXAssertFatal(0, err);
  356. #endif
  357. return;
  358. }
  359. AlignedArray<Point4F> alignedVal = AlignedArray<Point4F>(arraySize, sizeof(Point4F), (U8*)mPointArrayVal.address(), false);
  360. buff->set(mHandle, alignedVal);
  361. }
  362. else
  363. {
  364. #if TORQUE_DEBUG
  365. const char* err = avar("PostEffect::EffectConst::setToBuffer $s type is not implemented", mName.c_str());
  366. Con::errorf(err);
  367. GFXAssertFatal(0, err);
  368. #endif
  369. }
  370. }
  371. else if (mValueType == MatrixArrayType)
  372. {
  373. if (type == GFXSCT_Float4x4)
  374. {
  375. if (arraySize != mMatrixArrayVal.size())
  376. {
  377. #if TORQUE_DEBUG
  378. const char* err = avar("PostEffect::EffectConst::setToBuffer MatrixArrayType, attempted to feed an array that does not match the uniform array's size!");
  379. Con::errorf(err);
  380. GFXAssertFatal(0, err);
  381. #endif
  382. return;
  383. }
  384. buff->set(mHandle, mMatrixArrayVal.address(), arraySize);
  385. }
  386. else
  387. {
  388. #if TORQUE_DEBUG
  389. const char* err = avar("PostEffect::EffectConst::setToBuffer $s type is not implemented", mName.c_str());
  390. Con::errorf(err);
  391. GFXAssertFatal(0, err);
  392. #endif
  393. }
  394. }
  395. }
  396. //-------------------------------------------------------------------------
  397. // PostEffect
  398. //-------------------------------------------------------------------------
  399. PostEffect::PostEffect()
  400. : mRenderTime( PFXAfterDiffuse ),
  401. mRenderPriority( 1.0 ),
  402. mEnabled( false ),
  403. mStateBlockData( NULL ),
  404. mUpdateShader( true ),
  405. mSkip( false ),
  406. mAllowReflectPass( false ),
  407. mTargetClear( PFXTargetClear_None ),
  408. mTargetScale( Point2F::One ),
  409. mTargetViewport( PFXTargetViewport_TargetSize ),
  410. mTargetSize( Point2I::Zero ),
  411. mTargetFormat( GFXFormatR8G8B8A8 ),
  412. mTargetClearColor( LinearColorF::BLACK ),
  413. mOneFrameOnly( false ),
  414. mOnThisFrame( true ),
  415. mRTSizeSC( NULL ),
  416. mIsValid( false ),
  417. mShaderReloadKey( 0 ),
  418. mOneOverRTSizeSC( NULL ),
  419. mViewportOffsetSC( NULL ),
  420. mTargetViewportSC( NULL ),
  421. mFogDataSC( NULL ),
  422. mFogColorSC( NULL ),
  423. mEyePosSC( NULL ),
  424. mMatWorldToScreenSC( NULL ),
  425. mMatScreenToWorldSC( NULL ),
  426. mMatPrevScreenToWorldSC( NULL ),
  427. mNearFarSC( NULL ),
  428. mInvNearFarSC( NULL ),
  429. mWorldToScreenScaleSC( NULL ),
  430. mProjectionOffsetSC( NULL ),
  431. mWaterColorSC( NULL ),
  432. mWaterFogDataSC( NULL ),
  433. mAmbientColorSC( NULL ),
  434. mWaterFogPlaneSC( NULL ),
  435. mWaterDepthGradMaxSC( NULL ),
  436. mScreenSunPosSC( NULL ),
  437. mLightDirectionSC( NULL ),
  438. mCameraForwardSC( NULL ),
  439. mAccumTimeSC( NULL ),
  440. mDeltaTimeSC( NULL ),
  441. mInvCameraMatSC( NULL ),
  442. mMatCameraToWorldSC( NULL),
  443. mInvCameraTransSC(NULL),
  444. mMatCameraToScreenSC(NULL),
  445. mMatScreenToCameraSC(NULL)
  446. {
  447. dMemset( mTexSRGB, 0, sizeof(bool) * NumTextures);
  448. dMemset( mActiveTextures, 0, sizeof( GFXTextureObject* ) * NumTextures );
  449. dMemset( mActiveNamedTarget, 0, sizeof( NamedTexTarget* ) * NumTextures );
  450. dMemset( mActiveTextureViewport, 0, sizeof( RectI ) * NumTextures );
  451. dMemset( mTexSizeSC, 0, sizeof( GFXShaderConstHandle* ) * NumTextures );
  452. dMemset( mRenderTargetParamsSC, 0, sizeof( GFXShaderConstHandle* ) * NumTextures );
  453. for (U32 i = 0; i < NumTextures; i++)
  454. {
  455. INIT_IMAGEASSET_ARRAY(Texture, PostFxTextureProfile, i);
  456. }
  457. }
  458. PostEffect::~PostEffect()
  459. {
  460. EffectConstTable::Iterator iter = mEffectConsts.begin();
  461. for ( ; iter != mEffectConsts.end(); iter++ )
  462. delete iter->value;
  463. }
  464. void PostEffect::initPersistFields()
  465. {
  466. addField( "shader", TypeRealString, Offset( mShaderName, PostEffect ),
  467. "Name of a GFXShaderData for this effect." );
  468. addField( "stateBlock", TYPEID<GFXStateBlockData>(), Offset( mStateBlockData, PostEffect ),
  469. "Name of a GFXStateBlockData for this effect." );
  470. addField( "target", TypeRealString, Offset( mTargetName, PostEffect ),
  471. "String identifier of this effect's target texture.\n"
  472. "@see PFXTextureIdentifiers" );
  473. addField( "targetDepthStencil", TypeRealString, Offset( mTargetDepthStencilName, PostEffect ),
  474. "Optional string identifier for this effect's target depth/stencil texture.\n"
  475. "@see PFXTextureIdentifiers" );
  476. addField( "targetScale", TypePoint2F, Offset( mTargetScale, PostEffect ),
  477. "If targetSize is zero this is used to set a relative size from the current target." );
  478. addField( "targetSize", TypePoint2I, Offset( mTargetSize, PostEffect ),
  479. "If non-zero this is used as the absolute target size." );
  480. addField( "targetFormat", TypeGFXFormat, Offset( mTargetFormat, PostEffect ),
  481. "Format of the target texture, not applicable if writing to the backbuffer." );
  482. addField( "targetClearColor", TypeColorF, Offset( mTargetClearColor, PostEffect ),
  483. "Color to which the target texture is cleared before rendering." );
  484. addField( "targetClear", TYPEID< PFXTargetClear >(), Offset( mTargetClear, PostEffect ),
  485. "Describes when the target texture should be cleared." );
  486. addField( "targetViewport", TYPEID< PFXTargetViewport >(), Offset( mTargetViewport, PostEffect ),
  487. "Specifies how the viewport should be set up for a target texture." );
  488. INITPERSISTFIELD_IMAGEASSET_ARRAY(Texture, NumTextures, PostEffect, "Input textures to this effect ( samplers ).\n"
  489. "@see PFXTextureIdentifiers");
  490. addField("textureSRGB", TypeBool, Offset(mTexSRGB, PostEffect), NumTextures,
  491. "Set input texture to be sRGB");
  492. addField( "renderTime", TYPEID< PFXRenderTime >(), Offset( mRenderTime, PostEffect ),
  493. "When to process this effect during the frame." );
  494. addField( "renderBin", TypeRealString, Offset( mRenderBin, PostEffect ),
  495. "Name of a renderBin, used if renderTime is PFXBeforeBin or PFXAfterBin." );
  496. addField( "renderPriority", TypeF32, Offset( mRenderPriority, PostEffect ),
  497. "PostEffects are processed in DESCENDING order of renderPriority if more than one has the same renderBin/Time." );
  498. addField( "allowReflectPass", TypeBool, Offset( mAllowReflectPass, PostEffect ),
  499. "Is this effect processed during reflection render passes." );
  500. addProtectedField( "enabled", TypeBool, Offset( mEnabled, PostEffect),
  501. &PostEffect::_setIsEnabled, &defaultProtectedGetFn,
  502. "Is the effect on." );
  503. addField( "onThisFrame", TypeBool, Offset( mOnThisFrame, PostEffect ),
  504. "Allows you to turn on a PostEffect for only a single frame." );
  505. addField( "oneFrameOnly", TypeBool, Offset( mOneFrameOnly, PostEffect ),
  506. "Allows you to turn on a PostEffect for only a single frame." );
  507. addField( "skip", TypeBool, Offset( mSkip, PostEffect ),
  508. "Skip processing of this PostEffect and its children even if its parent "
  509. "is enabled. Parent and sibling PostEffects in the chain are still processed." );
  510. Parent::initPersistFields();
  511. }
  512. bool PostEffect::onAdd()
  513. {
  514. if( !Parent::onAdd() )
  515. return false;
  516. LightManager::smActivateSignal.notify( this, &PostEffect::_onLMActivate );
  517. mUpdateShader = true;
  518. // Grab the script path.
  519. Torque::Path scriptPath( Con::getVariable( "$Con::File" ) );
  520. scriptPath.setFileName( String::EmptyString );
  521. scriptPath.setExtension( String::EmptyString );
  522. // Find additional textures
  523. for (S32 i = 0; i < NumTextures; i++)
  524. {
  525. mTextureType[i] = NormalTextureType;
  526. String texFilename = getTexture(i);
  527. // Skip empty stages or ones with variable or target names.
  528. if (texFilename.isEmpty() ||
  529. texFilename[0] == '$' ||
  530. texFilename[0] == '#')
  531. continue;
  532. mTextureProfile[i] = (mTexSRGB[i]) ? &PostFxTextureSRGBProfile : &PostFxTextureProfile;
  533. _setTexture(texFilename, i);
  534. }
  535. // Is the target a named target?
  536. if ( mTargetName.isNotEmpty() && mTargetName[0] == '#' )
  537. {
  538. mNamedTarget.registerWithName( mTargetName.substr( 1 ) );
  539. mNamedTarget.getTextureDelegate().bind( this, &PostEffect::_getTargetTexture );
  540. }
  541. if ( mTargetDepthStencilName.isNotEmpty() && mTargetDepthStencilName[0] == '#' )
  542. mNamedTargetDepthStencil.registerWithName( mTargetDepthStencilName.substr( 1 ) );
  543. if (mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered())
  544. GFXTextureManager::addEventDelegate( this, &PostEffect::_onTextureEvent );
  545. // Call onAdd in script
  546. onAdd_callback();
  547. // Should we start enabled?
  548. if ( mEnabled )
  549. {
  550. mEnabled = false;
  551. enable();
  552. }
  553. getSet()->addObject( this );
  554. return true;
  555. }
  556. void PostEffect::onRemove()
  557. {
  558. Parent::onRemove();
  559. PFXMGR->_removeEffect( this );
  560. LightManager::smActivateSignal.remove( this, &PostEffect::_onLMActivate );
  561. mShader = NULL;
  562. _cleanTargets();
  563. if ( mNamedTarget.isRegistered() || mNamedTargetDepthStencil.isRegistered() )
  564. GFXTextureManager::removeEventDelegate( this, &PostEffect::_onTextureEvent );
  565. if ( mNamedTarget.isRegistered() )
  566. {
  567. mNamedTarget.unregister();
  568. mNamedTarget.getTextureDelegate().clear();
  569. }
  570. if ( mNamedTargetDepthStencil.isRegistered() )
  571. mNamedTargetDepthStencil.unregister();
  572. }
  573. void PostEffect::_updateScreenGeometry( const Frustum &frustum,
  574. GFXVertexBufferHandle<PFXVertex> *outVB )
  575. {
  576. outVB->set( GFX, 4, GFXBufferTypeVolatile );
  577. const Point3F *frustumPoints = frustum.getPoints();
  578. const Point3F& cameraPos = frustum.getPosition();
  579. // Perform a camera offset. We need to manually perform this offset on the postFx's
  580. // polygon, which is at the far plane.
  581. const Point2F& projOffset = frustum.getProjectionOffset();
  582. Point3F cameraOffsetPos = cameraPos;
  583. if(!projOffset.isZero())
  584. {
  585. // First we need to calculate the offset at the near plane. The projOffset
  586. // given above can be thought of a percent as it ranges from 0..1 (or 0..-1).
  587. F32 nearOffset = frustum.getNearRight() * projOffset.x;
  588. // Now given the near plane distance from the camera we can solve the right
  589. // triangle and calcuate the SIN theta for the offset at the near plane.
  590. // SIN theta = x/y
  591. F32 sinTheta = nearOffset / frustum.getNearDist();
  592. // Finally, we can calcuate the offset at the far plane, which is where our sun (or vector)
  593. // light's polygon is drawn.
  594. F32 farOffset = frustum.getFarDist() * sinTheta;
  595. // We can now apply this far plane offset to the far plane itself, which then compensates
  596. // for the project offset.
  597. MatrixF camTrans = frustum.getTransform();
  598. VectorF offset = camTrans.getRightVector();
  599. offset *= farOffset;
  600. cameraOffsetPos += offset;
  601. }
  602. PFXVertex *vert = outVB->lock();
  603. vert->point.set(-1.0, 1.0, 0.0);
  604. vert->texCoord.set(0.0f, 0.0f);
  605. vert->wsEyeRay = frustumPoints[Frustum::FarTopLeft] - cameraOffsetPos;
  606. vert++;
  607. vert->point.set(1.0, 1.0, 0.0);
  608. vert->texCoord.set(1.0f, 0.0f);
  609. vert->wsEyeRay = frustumPoints[Frustum::FarTopRight] - cameraOffsetPos;
  610. vert++;
  611. vert->point.set(-1.0, -1.0, 0.0);
  612. vert->texCoord.set(0.0f, 1.0f);
  613. vert->wsEyeRay = frustumPoints[Frustum::FarBottomLeft] - cameraOffsetPos;
  614. vert++;
  615. vert->point.set(1.0, -1.0, 0.0);
  616. vert->texCoord.set(1.0f, 1.0f);
  617. vert->wsEyeRay = frustumPoints[Frustum::FarBottomRight] - cameraOffsetPos;
  618. vert++;
  619. outVB->unlock();
  620. }
  621. void PostEffect::_setupStateBlock( const SceneRenderState *state )
  622. {
  623. if ( mStateBlock.isNull() )
  624. {
  625. GFXStateBlockDesc desc;
  626. if ( mStateBlockData )
  627. desc = mStateBlockData->getState();
  628. mStateBlock = GFX->createStateBlock( desc );
  629. }
  630. GFX->setStateBlock( mStateBlock );
  631. }
  632. void PostEffect::_setupConstants( const SceneRenderState *state )
  633. {
  634. // Alloc the const buffer.
  635. if ( mShaderConsts.isNull() )
  636. {
  637. mShaderConsts = mShader->allocConstBuffer();
  638. mRTSizeSC = mShader->getShaderConstHandle( "$targetSize" );
  639. mOneOverRTSizeSC = mShader->getShaderConstHandle( "$oneOverTargetSize" );
  640. mRTRatioSC = mShader->getShaderConstHandle("$targetRatio");
  641. for (U32 i = 0; i < NumTextures; i++)
  642. {
  643. mTexSizeSC[i] = mShader->getShaderConstHandle(String::ToString("$texSize%d", i));
  644. mRenderTargetParamsSC[i] = mShader->getShaderConstHandle(String::ToString("$rtParams%d",i));
  645. }
  646. mTargetViewportSC = mShader->getShaderConstHandle( "$targetViewport" );
  647. mFogDataSC = mShader->getShaderConstHandle( ShaderGenVars::fogData );
  648. mFogColorSC = mShader->getShaderConstHandle( ShaderGenVars::fogColor );
  649. mEyePosSC = mShader->getShaderConstHandle( ShaderGenVars::eyePosWorld );
  650. mNearFarSC = mShader->getShaderConstHandle( "$nearFar" );
  651. mInvNearFarSC = mShader->getShaderConstHandle( "$invNearFar" );
  652. mWorldToScreenScaleSC = mShader->getShaderConstHandle( "$worldToScreenScale" );
  653. mMatWorldToScreenSC = mShader->getShaderConstHandle( "$matWorldToScreen" );
  654. mMatScreenToWorldSC = mShader->getShaderConstHandle( "$matScreenToWorld" );
  655. mMatPrevScreenToWorldSC = mShader->getShaderConstHandle( "$matPrevScreenToWorld" );
  656. mProjectionOffsetSC = mShader->getShaderConstHandle( "$projectionOffset" );
  657. mWaterColorSC = mShader->getShaderConstHandle( "$waterColor" );
  658. mAmbientColorSC = mShader->getShaderConstHandle( "$ambientColor" );
  659. mWaterFogDataSC = mShader->getShaderConstHandle( "$waterFogData" );
  660. mWaterFogPlaneSC = mShader->getShaderConstHandle( "$waterFogPlane" );
  661. mWaterDepthGradMaxSC = mShader->getShaderConstHandle( "$waterDepthGradMax" );
  662. mScreenSunPosSC = mShader->getShaderConstHandle( "$screenSunPos" );
  663. mLightDirectionSC = mShader->getShaderConstHandle( "$lightDirection" );
  664. mCameraForwardSC = mShader->getShaderConstHandle( "$camForward" );
  665. mAccumTimeSC = mShader->getShaderConstHandle( "$accumTime" );
  666. mDeltaTimeSC = mShader->getShaderConstHandle( "$deltaTime" );
  667. mInvCameraMatSC = mShader->getShaderConstHandle( "$invCameraMat" );
  668. mMatCameraToWorldSC = mShader->getShaderConstHandle("$cameraToWorld");
  669. mInvCameraTransSC = mShader->getShaderConstHandle("$invCameraTrans");
  670. mMatCameraToScreenSC = mShader->getShaderConstHandle("$cameraToScreen");
  671. mMatScreenToCameraSC = mShader->getShaderConstHandle("$screenToCamera");
  672. }
  673. // Set up shader constants for source image size
  674. if ( mRTSizeSC->isValid() )
  675. {
  676. const Point2I &resolution = GFX->getActiveRenderTarget()->getSize();
  677. Point2F pixelShaderConstantData;
  678. pixelShaderConstantData.x = resolution.x;
  679. pixelShaderConstantData.y = resolution.y;
  680. mShaderConsts->set( mRTSizeSC, pixelShaderConstantData );
  681. }
  682. if ( mOneOverRTSizeSC->isValid() )
  683. {
  684. const Point2I &resolution = GFX->getActiveRenderTarget()->getSize();
  685. Point2F oneOverTargetSize( 1.0f / (F32)resolution.x, 1.0f / (F32)resolution.y );
  686. mShaderConsts->set( mOneOverRTSizeSC, oneOverTargetSize );
  687. }
  688. if (mRTRatioSC->isValid())
  689. {
  690. const Point2I& resolution = GFX->getActiveRenderTarget()->getSize();
  691. mShaderConsts->set(mRTRatioSC, (F32)resolution.x/ (F32)resolution.y);
  692. }
  693. // Set up additional textures
  694. Point2F texSizeConst;
  695. for( U32 i = 0; i < NumTextures; i++ )
  696. {
  697. if( !mActiveTextures[i] )
  698. continue;
  699. if ( mTexSizeSC[i]->isValid() )
  700. {
  701. texSizeConst.x = (F32)mActiveTextures[i]->getWidth();
  702. texSizeConst.y = (F32)mActiveTextures[i]->getHeight();
  703. mShaderConsts->set( mTexSizeSC[i], texSizeConst );
  704. }
  705. }
  706. for ( U32 i = 0; i < NumTextures; i++ )
  707. {
  708. if ( !mRenderTargetParamsSC[i]->isValid() )
  709. continue;
  710. Point4F rtParams( Point4F::One );
  711. if ( mActiveTextures[i] )
  712. {
  713. const Point3I &targetSz = mActiveTextures[i]->getSize();
  714. RectI targetVp = mActiveTextureViewport[i];
  715. ScreenSpace::RenderTargetParameters(targetSz, targetVp, rtParams);
  716. }
  717. mShaderConsts->set( mRenderTargetParamsSC[i], rtParams );
  718. }
  719. // Target viewport (in target space)
  720. if ( mTargetViewportSC->isValid() )
  721. {
  722. const Point2I& targetSize = GFX->getActiveRenderTarget()->getSize();
  723. Point3I size(targetSize.x, targetSize.y, 0);
  724. const RectI& viewport = GFX->getViewport();
  725. Point2F offset((F32)viewport.point.x / (F32)targetSize.x, (F32)viewport.point.y / (F32)targetSize.y );
  726. Point2F scale((F32)viewport.extent.x / (F32)targetSize.x, (F32)viewport.extent.y / (F32)targetSize.y );
  727. Point4F targetParams;
  728. targetParams.x = offset.x;
  729. targetParams.y = offset.y;
  730. targetParams.z = offset.x + scale.x;
  731. targetParams.w = offset.y + scale.y;
  732. mShaderConsts->set( mTargetViewportSC, targetParams );
  733. }
  734. // Set the fog data.
  735. if ( mFogDataSC->isValid() )
  736. {
  737. const FogData &data = state->getSceneManager()->getFogData();
  738. Point3F params;
  739. params.x = data.density;
  740. params.y = data.densityOffset;
  741. if ( !mIsZero( data.atmosphereHeight ) )
  742. params.z = 1.0f / data.atmosphereHeight;
  743. else
  744. params.z = 0.0f;
  745. mShaderConsts->set( mFogDataSC, params );
  746. }
  747. const PFXFrameState &thisFrame = PFXMGR->getFrameState();
  748. if ( mMatWorldToScreenSC->isValid() )
  749. {
  750. // Screen space->world space
  751. MatrixF tempMat = thisFrame.cameraToScreen;
  752. tempMat.mul( thisFrame.worldToCamera );
  753. tempMat.fullInverse();
  754. tempMat.transpose();
  755. // Support using these matrices as float3x3 or float4x4...
  756. mShaderConsts->set( mMatWorldToScreenSC, tempMat, mMatWorldToScreenSC->getType() );
  757. }
  758. if ( mMatScreenToWorldSC->isValid() )
  759. {
  760. // World space->screen space
  761. MatrixF tempMat = thisFrame.cameraToScreen;
  762. tempMat.mul( thisFrame.worldToCamera );
  763. tempMat.transpose();
  764. // Support using these matrices as float3x3 or float4x4...
  765. mShaderConsts->set( mMatScreenToWorldSC, tempMat, mMatScreenToWorldSC->getType() );
  766. }
  767. if ( mMatPrevScreenToWorldSC->isValid() )
  768. {
  769. const PFXFrameState &lastFrame = PFXMGR->getLastFrameState();
  770. // Previous frame world space->screen space
  771. MatrixF tempMat = lastFrame.cameraToScreen;
  772. tempMat.mul( lastFrame.worldToCamera );
  773. tempMat.transpose();
  774. mShaderConsts->set( mMatPrevScreenToWorldSC, tempMat );
  775. }
  776. if (mAmbientColorSC->isValid() && state)
  777. {
  778. const LinearColorF &sunlight = state->getAmbientLightColor();
  779. Point3F ambientColor( sunlight.red, sunlight.green, sunlight.blue );
  780. mShaderConsts->set( mAmbientColorSC, ambientColor );
  781. }
  782. if (mMatCameraToWorldSC->isValid())
  783. {
  784. MatrixF tempMat = thisFrame.worldToCamera;
  785. tempMat.inverse();
  786. mShaderConsts->set(mMatCameraToWorldSC, tempMat);
  787. }
  788. if (mInvCameraTransSC->isValid())
  789. {
  790. MatrixF mat = state->getCameraTransform();
  791. mat.fullInverse();
  792. mShaderConsts->set(mInvCameraTransSC, mat, mInvCameraTransSC->getType());
  793. }
  794. //Projection Matrix
  795. if (mMatCameraToScreenSC->isValid())
  796. {
  797. MatrixF tempMat = thisFrame.cameraToScreen;
  798. mShaderConsts->set(mMatCameraToScreenSC, tempMat, mMatCameraToScreenSC->getType());
  799. }
  800. //Inverse Projection Matrix
  801. if (mMatScreenToCameraSC->isValid())
  802. {
  803. MatrixF tempMat = thisFrame.cameraToScreen;
  804. tempMat.fullInverse();
  805. mShaderConsts->set(mMatScreenToCameraSC, tempMat, mMatScreenToCameraSC->getType());
  806. }
  807. mShaderConsts->setSafe( mAccumTimeSC, MATMGR->getTotalTime() );
  808. mShaderConsts->setSafe( mDeltaTimeSC, MATMGR->getDeltaTime() );
  809. // Now set all the constants that are dependent on the scene state.
  810. if ( state )
  811. {
  812. mShaderConsts->setSafe( mEyePosSC, state->getDiffuseCameraPosition() );
  813. mShaderConsts->setSafe( mNearFarSC, Point2F( state->getNearPlane(), state->getFarPlane() ) );
  814. mShaderConsts->setSafe( mInvNearFarSC, Point2F( 1.0f / state->getNearPlane(), 1.0f / state->getFarPlane() ) );
  815. mShaderConsts->setSafe( mWorldToScreenScaleSC, state->getWorldToScreenScale() );
  816. mShaderConsts->setSafe( mProjectionOffsetSC, state->getCameraFrustum().getProjectionOffset() );
  817. mShaderConsts->setSafe( mFogColorSC, state->getSceneManager()->getFogData().color );
  818. if ( mWaterColorSC->isValid() )
  819. {
  820. LinearColorF color( state->getSceneManager()->getWaterFogData().color );
  821. mShaderConsts->set( mWaterColorSC, color );
  822. }
  823. if ( mWaterFogDataSC->isValid() )
  824. {
  825. const WaterFogData &data = state->getSceneManager()->getWaterFogData();
  826. Point4F params( data.density, data.densityOffset, data.wetDepth, data.wetDarkening );
  827. mShaderConsts->set( mWaterFogDataSC, params );
  828. }
  829. if ( mWaterFogPlaneSC->isValid() )
  830. {
  831. const PlaneF &plane = state->getSceneManager()->getWaterFogData().plane;
  832. mShaderConsts->set( mWaterFogPlaneSC, plane );
  833. }
  834. if ( mWaterDepthGradMaxSC->isValid() )
  835. {
  836. mShaderConsts->set( mWaterDepthGradMaxSC, state->getSceneManager()->getWaterFogData().depthGradMax );
  837. }
  838. if ( mScreenSunPosSC->isValid() )
  839. {
  840. // Grab our projection matrix
  841. // from the frustum.
  842. Frustum frust = state->getCameraFrustum();
  843. MatrixF proj( true );
  844. frust.getProjectionMatrix( &proj );
  845. // Grab the ScatterSky world matrix.
  846. MatrixF camMat = state->getCameraTransform();
  847. camMat.inverse();
  848. MatrixF tmp( true );
  849. tmp = camMat;
  850. tmp.setPosition( Point3F( 0, 0, 0 ) );
  851. Point3F sunPos( 0, 0, 0 );
  852. // Get the light manager and sun light object.
  853. LightInfo *sunLight = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  854. // Grab the light direction and scale
  855. // by the ScatterSky radius to get the world
  856. // space sun position.
  857. const VectorF &lightDir = sunLight->getDirection();
  858. Point3F lightPos( lightDir.x * (6378.0f * 1000.0f),
  859. lightDir.y * (6378.0f * 1000.0f),
  860. lightDir.z * (6378.0f * 1000.0f) );
  861. RectI viewPort = GFX->getViewport();
  862. // Get the screen space sun position.
  863. MathUtils::mProjectWorldToScreen(lightPos, &sunPos, viewPort, tmp, proj);
  864. // And normalize it to the 0 to 1 range.
  865. sunPos.x -= (F32)viewPort.point.x;
  866. sunPos.y -= (F32)viewPort.point.y;
  867. sunPos.x /= (F32)viewPort.extent.x;
  868. sunPos.y /= (F32)viewPort.extent.y;
  869. mShaderConsts->set( mScreenSunPosSC, Point2F( sunPos.x, sunPos.y ) );
  870. }
  871. if ( mLightDirectionSC->isValid() )
  872. {
  873. LightInfo *sunLight = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  874. const VectorF &lightDir = sunLight->getDirection();
  875. mShaderConsts->set( mLightDirectionSC, lightDir );
  876. }
  877. if ( mCameraForwardSC->isValid() )
  878. {
  879. const MatrixF &camMat = state->getCameraTransform();
  880. VectorF camFwd( 0, 0, 0 );
  881. camMat.getColumn( 1, &camFwd );
  882. mShaderConsts->set( mCameraForwardSC, camFwd );
  883. }
  884. if ( mInvCameraMatSC->isValid() )
  885. {
  886. MatrixF mat = state->getCameraTransform();
  887. mat.inverse();
  888. mShaderConsts->set( mInvCameraMatSC, mat, mInvCameraMatSC->getType() );
  889. }
  890. } // if ( state )
  891. // Set EffectConsts - specified from script
  892. // If our shader has reloaded since last frame we must mark all
  893. // EffectConsts dirty so they will be reset.
  894. if ( mShader->getReloadKey() != mShaderReloadKey )
  895. {
  896. mShaderReloadKey = mShader->getReloadKey();
  897. EffectConstTable::Iterator iter = mEffectConsts.begin();
  898. for ( ; iter != mEffectConsts.end(); iter++ )
  899. {
  900. iter->value->mDirty = true;
  901. iter->value->mHandle = NULL;
  902. }
  903. }
  904. // Doesn't look like anyone is using this anymore.
  905. // But if we do want to pass this info to script,
  906. // we should do so in the same way as I am doing below.
  907. /*
  908. Point2F texSizeScriptConst( 0, 0 );
  909. String buffer;
  910. if ( mActiveTextures[0] )
  911. {
  912. texSizeScriptConst.x = (F32)mActiveTextures[0]->getWidth();
  913. texSizeScriptConst.y = (F32)mActiveTextures[0]->getHeight();
  914. dSscanf( buffer.c_str(), "%g %g", texSizeScriptConst.x, texSizeScriptConst.y );
  915. }
  916. */
  917. {
  918. PROFILE_SCOPE( PostEffect_SetShaderConsts );
  919. // Pass some data about the current render state to script.
  920. //
  921. // TODO: This is pretty messy... it should go away. This info
  922. // should be available from some other script accessible method
  923. // or field which isn't PostEffect specific.
  924. //
  925. if ( state )
  926. {
  927. Con::setFloatVariable( "$Param::NearDist", state->getNearPlane() );
  928. Con::setFloatVariable( "$Param::FarDist", state->getFarPlane() );
  929. }
  930. setShaderConsts_callback();
  931. }
  932. EffectConstTable::Iterator iter = mEffectConsts.begin();
  933. for ( ; iter != mEffectConsts.end(); iter++ )
  934. iter->value->setToBuffer( mShaderConsts );
  935. }
  936. void PostEffect::_setupTexture( U32 stage, GFXTexHandle &inputTex, const RectI *inTexViewport )
  937. {
  938. const String &texFilename = getTexture( stage );
  939. GFXTexHandle theTex;
  940. NamedTexTarget *namedTarget = NULL;
  941. RectI viewport = GFX->getViewport();
  942. if ( texFilename.compare( "$inTex", 0, String::NoCase ) == 0 )
  943. {
  944. theTex = inputTex;
  945. if ( inTexViewport )
  946. {
  947. viewport = *inTexViewport;
  948. }
  949. else if ( theTex )
  950. {
  951. viewport.set( 0, 0, theTex->getWidth(), theTex->getHeight() );
  952. }
  953. }
  954. else if ( texFilename.compare( "$backBuffer", 0, String::NoCase ) == 0 )
  955. {
  956. theTex = PFXMGR->getBackBufferTex();
  957. // Always use the GFX viewport when reading from the backbuffer
  958. }
  959. else if ( texFilename.isNotEmpty() && texFilename[0] == '#' )
  960. {
  961. namedTarget = NamedTexTarget::find( texFilename.c_str() + 1 );
  962. if ( namedTarget )
  963. {
  964. theTex = namedTarget->getTexture( 0 );
  965. viewport = namedTarget->getViewport();
  966. }
  967. }
  968. else
  969. {
  970. theTex = mTexture[ stage ];
  971. if ( theTex )
  972. viewport.set( 0, 0, theTex->getWidth(), theTex->getHeight() );
  973. }
  974. mActiveTextures[ stage ] = theTex;
  975. mActiveNamedTarget[ stage ] = namedTarget;
  976. mActiveTextureViewport[ stage ] = viewport;
  977. if ( theTex.isValid() )
  978. GFX->setTexture( stage, theTex );
  979. }
  980. void PostEffect::_setupCubemapTexture(U32 stage, GFXCubemapHandle &inputTex)
  981. {
  982. RectI viewport = GFX->getViewport();
  983. mActiveTextures[stage] = nullptr;
  984. mActiveNamedTarget[stage] = nullptr;
  985. mActiveTextureViewport[stage] = viewport;
  986. if (inputTex.isValid())
  987. GFX->setCubeTexture(stage, inputTex);
  988. }
  989. void PostEffect::_setupCubemapArrayTexture(U32 stage, GFXCubemapArrayHandle &inputTex)
  990. {
  991. RectI viewport = GFX->getViewport();
  992. mActiveTextures[stage] = nullptr;
  993. mActiveNamedTarget[stage] = nullptr;
  994. mActiveTextureViewport[stage] = viewport;
  995. if (inputTex.isValid())
  996. GFX->setCubeArrayTexture(stage, inputTex);
  997. }
  998. void PostEffect::_setupTransforms()
  999. {
  1000. // Set everything to identity.
  1001. GFX->setWorldMatrix( MatrixF::Identity );
  1002. GFX->setProjectionMatrix( MatrixF::Identity );
  1003. }
  1004. void PostEffect::_setupTarget( const SceneRenderState *state, bool *outClearTarget )
  1005. {
  1006. if ( mNamedTarget.isRegistered() ||
  1007. mTargetName.compare( "$outTex", 0, String::NoCase ) == 0 )
  1008. {
  1009. // Size it relative to the texture of the first stage or
  1010. // if NULL then use the current target.
  1011. Point2I targetSize;
  1012. // If we have an absolute target size then use that.
  1013. if ( !mTargetSize.isZero() )
  1014. targetSize = mTargetSize;
  1015. // Else generate a relative size using the target scale.
  1016. else if ( mActiveTextures[ 0 ] )
  1017. {
  1018. const Point3I &texSize = mActiveTextures[ 0 ]->getSize();
  1019. targetSize.set( texSize.x * mTargetScale.x,
  1020. texSize.y * mTargetScale.y );
  1021. }
  1022. else
  1023. {
  1024. GFXTarget *oldTarget = GFX->getActiveRenderTarget();
  1025. const Point2I &oldTargetSize = oldTarget->getSize();
  1026. targetSize.set( oldTargetSize.x * mTargetScale.x,
  1027. oldTargetSize.y * mTargetScale.y );
  1028. }
  1029. // Make sure its at least 1x1.
  1030. targetSize.setMax( Point2I::One );
  1031. if ( mNamedTarget.isRegistered() ||
  1032. !mTargetTex ||
  1033. mTargetTex.getWidthHeight() != targetSize )
  1034. {
  1035. mTargetTex.set( targetSize.x, targetSize.y, mTargetFormat,
  1036. &PostFxTargetProfile, "PostEffect::_setupTarget" );
  1037. if ( mTargetClear == PFXTargetClear_OnCreate )
  1038. *outClearTarget = true;
  1039. if(mTargetViewport == PFXTargetViewport_GFXViewport)
  1040. {
  1041. // We may need to scale the GFX viewport to fit within
  1042. // our target texture size
  1043. GFXTarget *oldTarget = GFX->getActiveRenderTarget();
  1044. const Point2I &oldTargetSize = oldTarget->getSize();
  1045. Point2F scale(targetSize.x / F32(oldTargetSize.x), targetSize.y / F32(oldTargetSize.y));
  1046. const RectI &viewport = GFX->getViewport();
  1047. mNamedTarget.setViewport( RectI( viewport.point.x*scale.x, viewport.point.y*scale.y, viewport.extent.x*scale.x, viewport.extent.y*scale.y ) );
  1048. }
  1049. else if(mTargetViewport == PFXTargetViewport_NamedInTexture0 && mActiveNamedTarget[0] && mActiveNamedTarget[0]->getTexture())
  1050. {
  1051. // Scale the named input texture's viewport to match our target
  1052. const Point3I &namedTargetSize = mActiveNamedTarget[0]->getTexture()->getSize();
  1053. Point2F scale(targetSize.x / F32(namedTargetSize.x), targetSize.y / F32(namedTargetSize.y));
  1054. const RectI &viewport = mActiveNamedTarget[0]->getViewport();
  1055. mNamedTarget.setViewport( RectI( viewport.point.x*scale.x, viewport.point.y*scale.y, viewport.extent.x*scale.x, viewport.extent.y*scale.y ) );
  1056. }
  1057. else
  1058. {
  1059. // PFXTargetViewport_TargetSize
  1060. mNamedTarget.setViewport( RectI( 0, 0, targetSize.x, targetSize.y ) );
  1061. }
  1062. }
  1063. }
  1064. else
  1065. mTargetTex = NULL;
  1066. // Do we have a named depthStencil target?
  1067. if ( mNamedTargetDepthStencil.isRegistered() )
  1068. {
  1069. // Size it relative to the texture of the first stage or
  1070. // if NULL then use the current target.
  1071. Point2I targetSize;
  1072. // If we have an absolute target size then use that.
  1073. if ( !mTargetSize.isZero() )
  1074. targetSize = mTargetSize;
  1075. // Else generate a relative size using the target scale.
  1076. else if ( mActiveTextures[ 0 ] )
  1077. {
  1078. const Point3I &texSize = mActiveTextures[ 0 ]->getSize();
  1079. targetSize.set( texSize.x * mTargetScale.x,
  1080. texSize.y * mTargetScale.y );
  1081. }
  1082. else
  1083. {
  1084. GFXTarget *oldTarget = GFX->getActiveRenderTarget();
  1085. const Point2I &oldTargetSize = oldTarget->getSize();
  1086. targetSize.set( oldTargetSize.x * mTargetScale.x,
  1087. oldTargetSize.y * mTargetScale.y );
  1088. }
  1089. // Make sure its at least 1x1.
  1090. targetSize.setMax( Point2I::One );
  1091. if ( mNamedTargetDepthStencil.isRegistered() &&
  1092. mTargetDepthStencil.getWidthHeight() != targetSize )
  1093. {
  1094. mTargetDepthStencil.set( targetSize.x, targetSize.y, GFXFormatD24S8,
  1095. &GFXZTargetProfile, "PostEffect::_setupTarget" );
  1096. if ( mTargetClear == PFXTargetClear_OnCreate )
  1097. *outClearTarget = true;
  1098. if(mTargetViewport == PFXTargetViewport_GFXViewport)
  1099. {
  1100. // We may need to scale the GFX viewport to fit within
  1101. // our target texture size
  1102. GFXTarget *oldTarget = GFX->getActiveRenderTarget();
  1103. const Point2I &oldTargetSize = oldTarget->getSize();
  1104. Point2F scale(targetSize.x / F32(oldTargetSize.x), targetSize.y / F32(oldTargetSize.y));
  1105. const RectI &viewport = GFX->getViewport();
  1106. mNamedTargetDepthStencil.setViewport( RectI( viewport.point.x*scale.x, viewport.point.y*scale.y, viewport.extent.x*scale.x, viewport.extent.y*scale.y ) );
  1107. }
  1108. else if(mTargetViewport == PFXTargetViewport_NamedInTexture0 && mActiveNamedTarget[0] && mActiveNamedTarget[0]->getTexture())
  1109. {
  1110. // Scale the named input texture's viewport to match our target
  1111. const Point3I &namedTargetSize = mActiveNamedTarget[0]->getTexture()->getSize();
  1112. Point2F scale(targetSize.x / F32(namedTargetSize.x), targetSize.y / F32(namedTargetSize.y));
  1113. const RectI &viewport = mActiveNamedTarget[0]->getViewport();
  1114. mNamedTargetDepthStencil.setViewport( RectI( viewport.point.x*scale.x, viewport.point.y*scale.y, viewport.extent.x*scale.x, viewport.extent.y*scale.y ) );
  1115. }
  1116. else
  1117. {
  1118. // PFXTargetViewport_TargetSize
  1119. mNamedTargetDepthStencil.setViewport( RectI( 0, 0, targetSize.x, targetSize.y ) );
  1120. }
  1121. }
  1122. }
  1123. else
  1124. mTargetDepthStencil = NULL;
  1125. if ( mTargetClear == PFXTargetClear_OnDraw )
  1126. *outClearTarget = true;
  1127. if ( !mTarget && (mTargetTex || mTargetDepthStencil) )
  1128. mTarget = GFX->allocRenderToTextureTarget();
  1129. }
  1130. void PostEffect::_cleanTargets( bool recurse )
  1131. {
  1132. mTargetTex = NULL;
  1133. mTargetDepthStencil = NULL;
  1134. mTarget = NULL;
  1135. if ( !recurse )
  1136. return;
  1137. // Clear the children too!
  1138. for ( U32 i = 0; i < size(); i++ )
  1139. {
  1140. PostEffect *effect = (PostEffect*)(*this)[i];
  1141. effect->_cleanTargets( true );
  1142. }
  1143. }
  1144. void PostEffect::process( const SceneRenderState *state,
  1145. GFXTexHandle &inOutTex,
  1146. const RectI *inTexViewport )
  1147. {
  1148. // If the shader is forced to be skipped... then skip.
  1149. if ( mSkip )
  1150. return;
  1151. // Skip out if we don't support reflection passes.
  1152. if ( state && state->isReflectPass() && !mAllowReflectPass )
  1153. return;
  1154. if ( mOneFrameOnly && !mOnThisFrame )
  1155. return;
  1156. // Check requirements if the shader needs updating.
  1157. if ( mUpdateShader )
  1158. {
  1159. _checkRequirements();
  1160. // Clear the targets if we failed passing
  1161. // the requirements at this time.
  1162. if ( !mIsValid )
  1163. _cleanTargets( true );
  1164. }
  1165. // If we're not valid then we cannot render.
  1166. if ( !mIsValid )
  1167. return;
  1168. GFXDEBUGEVENT_SCOPE_EX( PostEffect_Process, ColorI::GREEN, avar("PostEffect: %s", getName()) );
  1169. preProcess_callback();
  1170. GFXTransformSaver saver;
  1171. // Set the textures.
  1172. for (U32 i = 0; i < NumTextures; i++)
  1173. {
  1174. if (mTextureType[i] == NormalTextureType)
  1175. _setupTexture(i, inOutTex, inTexViewport);
  1176. else if (mTextureType[i] == CubemapType)
  1177. _setupCubemapTexture(i, mCubemapTextures[i]);
  1178. else if (mTextureType[i] == CubemapArrayType)
  1179. _setupCubemapArrayTexture(i, mCubemapArrayTextures[i]);
  1180. }
  1181. _setupStateBlock( state ) ;
  1182. _setupTransforms();
  1183. bool clearTarget = false;
  1184. _setupTarget( state, &clearTarget );
  1185. if ( mTargetTex || mTargetDepthStencil )
  1186. {
  1187. const RectI &oldViewport = GFX->getViewport();
  1188. GFXTarget *oldTarget = GFX->getActiveRenderTarget();
  1189. GFX->pushActiveRenderTarget();
  1190. mTarget->attachTexture( GFXTextureTarget::Color0, mTargetTex );
  1191. // Set the right depth stencil target.
  1192. if ( !mTargetDepthStencil && mTargetTex.getWidthHeight() == GFX->getActiveRenderTarget()->getSize() )
  1193. mTarget->attachTexture( GFXTextureTarget::DepthStencil, GFXTextureTarget::sDefaultDepthStencil );
  1194. else
  1195. mTarget->attachTexture( GFXTextureTarget::DepthStencil, mTargetDepthStencil );
  1196. // Set the render target but not its viewport. We'll do that below.
  1197. GFX->setActiveRenderTarget( mTarget, false );
  1198. if(mNamedTarget.isRegistered())
  1199. {
  1200. // Always use the name target's viewport, if available. It was set up in _setupTarget().
  1201. GFX->setViewport(mNamedTarget.getViewport());
  1202. }
  1203. else if(mTargetViewport == PFXTargetViewport_GFXViewport)
  1204. {
  1205. // Go with the current viewport as scaled against our render target.
  1206. const Point2I &oldTargetSize = oldTarget->getSize();
  1207. const Point2I &targetSize = mTarget->getSize();
  1208. Point2F scale(targetSize.x / F32(oldTargetSize.x), targetSize.y / F32(oldTargetSize.y));
  1209. GFX->setViewport( RectI( oldViewport.point.x*scale.x, oldViewport.point.y*scale.y, oldViewport.extent.x*scale.x, oldViewport.extent.y*scale.y ) );
  1210. }
  1211. else if(mTargetViewport == PFXTargetViewport_NamedInTexture0 && mActiveNamedTarget[0] && mActiveNamedTarget[0]->getTexture())
  1212. {
  1213. // Go with the first input texture, if it is named. Scale the named input texture's viewport to match our target
  1214. const Point3I &namedTargetSize = mActiveNamedTarget[0]->getTexture()->getSize();
  1215. const Point2I &targetSize = mTarget->getSize();
  1216. Point2F scale(targetSize.x / F32(namedTargetSize.x), targetSize.y / F32(namedTargetSize.y));
  1217. const RectI &viewport = mActiveNamedTarget[0]->getViewport();
  1218. GFX->setViewport( RectI( viewport.point.x*scale.x, viewport.point.y*scale.y, viewport.extent.x*scale.x, viewport.extent.y*scale.y ) );
  1219. }
  1220. else
  1221. {
  1222. // Default to using the whole target as the viewport
  1223. GFX->setViewport( RectI( Point2I::Zero, mTarget->getSize() ) );
  1224. }
  1225. }
  1226. if ( clearTarget )
  1227. GFX->clear( GFXClearTarget, mTargetClearColor, 1.f, 0 );
  1228. // Setup the shader and constants.
  1229. if ( mShader )
  1230. {
  1231. GFX->setShader( mShader );
  1232. _setupConstants( state );
  1233. GFX->setShaderConstBuffer( mShaderConsts );
  1234. }
  1235. else
  1236. GFX->setupGenericShaders();
  1237. Frustum frustum;
  1238. if ( state )
  1239. frustum = state->getCameraFrustum();
  1240. else
  1241. {
  1242. // If we don't have a scene state then setup
  1243. // a dummy frustum... you better not be depending
  1244. // on this being related to the camera in any way.
  1245. frustum = Frustum();
  1246. }
  1247. GFXVertexBufferHandle<PFXVertex> vb;
  1248. _updateScreenGeometry( frustum, &vb );
  1249. // Draw it.
  1250. GFX->setVertexBuffer( vb );
  1251. GFX->drawPrimitive( GFXTriangleStrip, 0, 2 );
  1252. // Allow PostEffecVis to hook in.
  1253. PFXVIS->onPFXProcessed( this );
  1254. if ( mTargetTex || mTargetDepthStencil )
  1255. {
  1256. mTarget->resolve();
  1257. GFX->popActiveRenderTarget();
  1258. }
  1259. else
  1260. {
  1261. // We wrote to the active back buffer, so release
  1262. // the current texture copy held by the manager.
  1263. //
  1264. // This ensures a new copy is made.
  1265. PFXMGR->releaseBackBufferTex();
  1266. }
  1267. // Return and release our target texture.
  1268. inOutTex = mTargetTex;
  1269. if ( !mNamedTarget.isRegistered() )
  1270. mTargetTex = NULL;
  1271. // Restore the transforms before the children
  1272. // are processed as it screws up the viewport.
  1273. saver.restore();
  1274. // Now process my children.
  1275. iterator i = begin();
  1276. for ( ; i != end(); i++ )
  1277. {
  1278. PostEffect *effect = static_cast<PostEffect*>(*i);
  1279. effect->process( state, inOutTex );
  1280. }
  1281. if ( mOneFrameOnly )
  1282. mOnThisFrame = false;
  1283. }
  1284. bool PostEffect::_setIsEnabled( void *object, const char *index, const char *data )
  1285. {
  1286. bool enabled = dAtob( data );
  1287. if ( enabled )
  1288. static_cast<PostEffect*>( object )->enable();
  1289. else
  1290. static_cast<PostEffect*>( object )->disable();
  1291. // Always return false from a protected field.
  1292. return false;
  1293. }
  1294. void PostEffect::enable()
  1295. {
  1296. // Don't add TexGen PostEffects to the PostEffectManager!
  1297. if ( mRenderTime == PFXTexGenOnDemand )
  1298. return;
  1299. // Ignore it if its already enabled.
  1300. if ( mEnabled )
  1301. return;
  1302. mEnabled = true;
  1303. // We cannot really enable the effect
  1304. // until its been registed.
  1305. if ( !isProperlyAdded() )
  1306. return;
  1307. // If the enable callback returns 'false' then
  1308. // leave the effect disabled.
  1309. if ( !onEnabled_callback() )
  1310. {
  1311. mEnabled = false;
  1312. return;
  1313. }
  1314. PFXMGR->_addEffect( this );
  1315. }
  1316. void PostEffect::disable()
  1317. {
  1318. if ( !mEnabled )
  1319. return;
  1320. mEnabled = false;
  1321. _cleanTargets( true );
  1322. if ( isProperlyAdded() )
  1323. {
  1324. PFXMGR->_removeEffect( this );
  1325. onDisabled_callback();
  1326. }
  1327. }
  1328. void PostEffect::reload()
  1329. {
  1330. // Reload the shader if we have one or mark it
  1331. // for updating when its processed next.
  1332. if ( mShader )
  1333. mShader->reload();
  1334. else
  1335. mUpdateShader = true;
  1336. // Null stateblock so it is reloaded.
  1337. mStateBlock = NULL;
  1338. // Call reload on any children
  1339. // this PostEffect may have.
  1340. for ( U32 i = 0; i < size(); i++ )
  1341. {
  1342. PostEffect *effect = (PostEffect*)(*this)[i];
  1343. effect->reload();
  1344. }
  1345. }
  1346. void PostEffect::setTexture( U32 index, const String &texFilePath )
  1347. {
  1348. // Set the new texture name.
  1349. mTextureName[index] = texFilePath;
  1350. mTexture[index].free();
  1351. // Skip empty stages or ones with variable or target names.
  1352. if ( texFilePath.isEmpty() ||
  1353. texFilePath[0] == '$' ||
  1354. texFilePath[0] == '#' )
  1355. return;
  1356. mTextureProfile[index] = (mTexSRGB[index])? &PostFxTextureSRGBProfile : &PostFxTextureProfile;
  1357. _setTexture(texFilePath, index);
  1358. mTextureType[index] = NormalTextureType;
  1359. }
  1360. void PostEffect::setTexture(U32 index, const GFXTexHandle& texHandle)
  1361. {
  1362. // Set the new texture name.
  1363. mTextureName[index] = StringTable->EmptyString();
  1364. mTexture[index].free();
  1365. // Skip empty stages or ones with variable or target names.
  1366. if (!texHandle.isValid())
  1367. return;
  1368. // Try to load the texture.
  1369. mTexture[index] = texHandle;
  1370. mTextureType[index] = NormalTextureType;
  1371. }
  1372. void PostEffect::setCubemapTexture(U32 index, const GFXCubemapHandle &cubemapHandle)
  1373. {
  1374. // Set the new texture name.
  1375. mCubemapTextures[index].free();
  1376. // Skip empty stages or ones with variable or target names.
  1377. if (cubemapHandle.isNull())
  1378. return;
  1379. // Try to load the texture.
  1380. mCubemapTextures[index] = cubemapHandle;
  1381. mTextureType[index] = CubemapType;
  1382. }
  1383. void PostEffect::setCubemapArrayTexture(U32 index, const GFXCubemapArrayHandle &cubemapArrayHandle)
  1384. {
  1385. // Set the new texture name.
  1386. mCubemapArrayTextures[index].free();
  1387. // Skip empty stages or ones with variable or target names.
  1388. if (cubemapArrayHandle.isNull())
  1389. return;
  1390. // Try to load the texture.
  1391. mCubemapArrayTextures[index] = cubemapArrayHandle;
  1392. mTextureType[index] = CubemapArrayType;
  1393. }
  1394. void PostEffect::setShaderConst( const String &name, const String &val )
  1395. {
  1396. PROFILE_SCOPE( PostEffect_SetShaderConst );
  1397. EffectConstTable::Iterator iter = mEffectConsts.find( name );
  1398. if ( iter == mEffectConsts.end() )
  1399. {
  1400. EffectConst *newConst = new EffectConst( name, val );
  1401. iter = mEffectConsts.insertUnique( name, newConst );
  1402. }
  1403. iter->value->set( val );
  1404. }
  1405. void PostEffect::setShaderConst(const String &name, const F32 &val)
  1406. {
  1407. PROFILE_SCOPE(PostEffect_SetShaderConst_Float);
  1408. EffectConstTable::Iterator iter = mEffectConsts.find(name);
  1409. if (iter == mEffectConsts.end())
  1410. {
  1411. EffectConst *newConst = new EffectConst(name, val);
  1412. iter = mEffectConsts.insertUnique(name, newConst);
  1413. }
  1414. iter->value->set(val);
  1415. }
  1416. void PostEffect::setShaderConst(const String& name, const int& val)
  1417. {
  1418. PROFILE_SCOPE(PostEffect_SetShaderConst_Int);
  1419. EffectConstTable::Iterator iter = mEffectConsts.find(name);
  1420. if (iter == mEffectConsts.end())
  1421. {
  1422. EffectConst* newConst = new EffectConst(name, val);
  1423. iter = mEffectConsts.insertUnique(name, newConst);
  1424. }
  1425. iter->value->set(val);
  1426. }
  1427. void PostEffect::setShaderConst(const String &name, const Point4F &val)
  1428. {
  1429. PROFILE_SCOPE(PostEffect_SetShaderConst_Point);
  1430. EffectConstTable::Iterator iter = mEffectConsts.find(name);
  1431. if (iter == mEffectConsts.end())
  1432. {
  1433. EffectConst *newConst = new EffectConst(name, val);
  1434. iter = mEffectConsts.insertUnique(name, newConst);
  1435. }
  1436. iter->value->set(val);
  1437. }
  1438. void PostEffect::setShaderConst(const String &name, const MatrixF &val)
  1439. {
  1440. PROFILE_SCOPE(PostEffect_SetShaderConst_Matrix);
  1441. EffectConstTable::Iterator iter = mEffectConsts.find(name);
  1442. if (iter == mEffectConsts.end())
  1443. {
  1444. EffectConst *newConst = new EffectConst(name, val);
  1445. iter = mEffectConsts.insertUnique(name, newConst);
  1446. }
  1447. iter->value->set(val);
  1448. }
  1449. void PostEffect::setShaderConst(const String &name, const Vector<Point4F> &val)
  1450. {
  1451. PROFILE_SCOPE(PostEffect_SetShaderConst_PointArray);
  1452. EffectConstTable::Iterator iter = mEffectConsts.find(name);
  1453. if (iter == mEffectConsts.end())
  1454. {
  1455. EffectConst *newConst = new EffectConst(name, val);
  1456. iter = mEffectConsts.insertUnique(name, newConst);
  1457. }
  1458. iter->value->set(val);
  1459. }
  1460. void PostEffect::setShaderConst(const String &name, const Vector<MatrixF> &val)
  1461. {
  1462. PROFILE_SCOPE(PostEffect_SetShaderConst_MatrixArray);
  1463. EffectConstTable::Iterator iter = mEffectConsts.find(name);
  1464. if (iter == mEffectConsts.end())
  1465. {
  1466. EffectConst *newConst = new EffectConst(name, val);
  1467. iter = mEffectConsts.insertUnique(name, newConst);
  1468. }
  1469. iter->value->set(val);
  1470. }
  1471. F32 PostEffect::getAspectRatio() const
  1472. {
  1473. const Point2I &rtSize = GFX->getActiveRenderTarget()->getSize();
  1474. return (F32)rtSize.x / (F32)rtSize.y;
  1475. }
  1476. void PostEffect::_checkRequirements()
  1477. {
  1478. // This meets requirements if its shader loads
  1479. // properly, we can find all the input textures,
  1480. // and its formats are supported.
  1481. mIsValid = false;
  1482. mUpdateShader = false;
  1483. mShader = NULL;
  1484. mShaderConsts = NULL;
  1485. EffectConstTable::Iterator iter = mEffectConsts.begin();
  1486. for ( ; iter != mEffectConsts.end(); iter++ )
  1487. {
  1488. iter->value->mDirty = true;
  1489. iter->value->mHandle = NULL;
  1490. }
  1491. // First make sure the target format is supported.
  1492. if ( mNamedTarget.isRegistered() )
  1493. {
  1494. Vector<GFXFormat> formats;
  1495. formats.push_back( mTargetFormat );
  1496. GFXFormat format = GFX->selectSupportedFormat( &PostFxTargetProfile,
  1497. formats,
  1498. true,
  1499. false,
  1500. false );
  1501. // If we didn't get our format out then its unsupported!
  1502. if ( format != mTargetFormat )
  1503. return;
  1504. }
  1505. // Gather macros specified on this PostEffect.
  1506. Vector<GFXShaderMacro> macros( mShaderMacros );
  1507. // Now check the input named targets and make sure
  1508. // they exist... else we're invalid.
  1509. for ( U32 i=0; i < NumTextures; i++ )
  1510. {
  1511. if (mTextureType[i] == NormalTextureType)
  1512. {
  1513. const String &texFilename = mTextureName[i];
  1514. if (texFilename.isNotEmpty() && texFilename[0] == '#')
  1515. {
  1516. NamedTexTarget *namedTarget = NamedTexTarget::find(texFilename.c_str() + 1);
  1517. if (!namedTarget)
  1518. {
  1519. return;
  1520. }
  1521. // Grab the macros for shader initialization.
  1522. namedTarget->getShaderMacros(&macros);
  1523. }
  1524. }
  1525. }
  1526. // Finally find and load the shader.
  1527. ShaderData *shaderData;
  1528. if ( Sim::findObject( mShaderName, shaderData ) )
  1529. if ( shaderData->getPixVersion() <= GFX->getPixelShaderVersion() )
  1530. mShader = shaderData->getShader( macros );
  1531. // If we didn't get a shader... we're done.
  1532. if ( !mShader )
  1533. return;
  1534. // If we got here then we're valid.
  1535. mIsValid = true;
  1536. }
  1537. bool PostEffect::dumpShaderDisassembly( String &outFilename ) const
  1538. {
  1539. String data;
  1540. if ( !mShader || !mShader->getDisassembly( data ) )
  1541. return false;
  1542. outFilename = FS::MakeUniquePath( "", "ShaderDisassembly", "txt" );
  1543. FileStream *fstream = FileStream::createAndOpen( outFilename, Torque::FS::File::Write );
  1544. if ( !fstream )
  1545. return false;
  1546. fstream->write( data );
  1547. fstream->close();
  1548. delete fstream;
  1549. return true;
  1550. }
  1551. SimSet* PostEffect::getSet() const
  1552. {
  1553. SimSet *set;
  1554. if ( !Sim::findObject( "PFXSet", set ) )
  1555. {
  1556. set = new SimSet();
  1557. set->registerObject( "PFXSet" );
  1558. Sim::getRootGroup()->addObject( set );
  1559. }
  1560. return set;
  1561. }
  1562. void PostEffect::setShaderMacro( const String &name, const String &value )
  1563. {
  1564. // Check to see if we already have this macro.
  1565. Vector<GFXShaderMacro>::iterator iter = mShaderMacros.begin();
  1566. for ( ; iter != mShaderMacros.end(); iter++ )
  1567. {
  1568. if ( iter->name == name )
  1569. {
  1570. if ( iter->value != value )
  1571. {
  1572. iter->value = value;
  1573. mUpdateShader = true;
  1574. }
  1575. return;
  1576. }
  1577. }
  1578. // Add a new macro.
  1579. mShaderMacros.increment();
  1580. mShaderMacros.last().name = name;
  1581. mShaderMacros.last().value = value;
  1582. mUpdateShader = true;
  1583. }
  1584. bool PostEffect::removeShaderMacro( const String &name )
  1585. {
  1586. Vector<GFXShaderMacro>::iterator iter = mShaderMacros.begin();
  1587. for ( ; iter != mShaderMacros.end(); iter++ )
  1588. {
  1589. if ( iter->name == name )
  1590. {
  1591. mShaderMacros.erase( iter );
  1592. mUpdateShader = true;
  1593. return true;
  1594. }
  1595. }
  1596. return false;
  1597. }
  1598. void PostEffect::clearShaderMacros()
  1599. {
  1600. if ( mShaderMacros.empty() )
  1601. return;
  1602. mShaderMacros.clear();
  1603. mUpdateShader = true;
  1604. }
  1605. GFXTextureObject* PostEffect::_getTargetTexture( U32 )
  1606. {
  1607. // A TexGen PostEffect will generate its texture now if it
  1608. // has not already.
  1609. if ( mRenderTime == PFXTexGenOnDemand &&
  1610. ( !mTargetTex || mUpdateShader ) )
  1611. {
  1612. GFXTexHandle chainTex;
  1613. process( NULL, chainTex );
  1614. // TODO: We should add a conditional copy
  1615. // to a non-RT texture here to reduce the
  1616. // amount of non-swappable RTs in use.
  1617. }
  1618. return mTargetTex.getPointer();
  1619. }
  1620. DefineEngineMethod( PostEffect, reload, void, (),,
  1621. "Reloads the effect shader and textures." )
  1622. {
  1623. return object->reload();
  1624. }
  1625. DefineEngineMethod( PostEffect, enable, void, (),,
  1626. "Enables the effect." )
  1627. {
  1628. object->enable();
  1629. }
  1630. DefineEngineMethod( PostEffect, disable, void, (),,
  1631. "Disables the effect." )
  1632. {
  1633. object->disable();
  1634. }
  1635. DefineEngineMethod( PostEffect, toggle, bool, (),,
  1636. "Toggles the effect between enabled / disabled.\n"
  1637. "@return True if effect is enabled." )
  1638. {
  1639. if ( object->isEnabled() )
  1640. object->disable();
  1641. else
  1642. object->enable();
  1643. return object->isEnabled();
  1644. }
  1645. DefineEngineMethod( PostEffect, isEnabled, bool, (),,
  1646. "@return True if the effect is enabled." )
  1647. {
  1648. return object->isEnabled();
  1649. }
  1650. DefineEngineMethod( PostEffect, setTexture, void, ( S32 index, const char *filePath ),,
  1651. "This is used to set the texture file and load the texture on a running effect. "
  1652. "If the texture file is not different from the current file nothing is changed. If "
  1653. "the texture cannot be found a null texture is assigned.\n"
  1654. "@param index The texture stage index.\n"
  1655. "@param filePath The file name of the texture to set.\n" )
  1656. {
  1657. if ( index > -1 && index < PostEffect::NumTextures )
  1658. object->setTexture( index, filePath );
  1659. }
  1660. DefineEngineMethod( PostEffect, setShaderConst, void, ( const char* name, const char* value ),,
  1661. "Sets the value of a uniform defined in the shader. This will usually "
  1662. "be called within the setShaderConsts callback. Array type constants are "
  1663. "not supported.\n"
  1664. "@param name Name of the constanst, prefixed with '$'.\n"
  1665. "@param value Value to set, space seperate values with more than one element.\n"
  1666. "@tsexample\n"
  1667. "function MyPfx::setShaderConsts( %this )\n"
  1668. "{\n"
  1669. " // example float4 uniform\n"
  1670. " %this.setShaderConst( \"$colorMod\", \"1.0 0.9 1.0 1.0\" );\n"
  1671. " // example float1 uniform\n"
  1672. " %this.setShaderConst( \"$strength\", \"3.0\" );\n"
  1673. " // example integer uniform\n"
  1674. " %this.setShaderConst( \"$loops\", \"5\" );"
  1675. "}\n"
  1676. "@endtsexample" )
  1677. {
  1678. object->setShaderConst( name, value );
  1679. }
  1680. DefineEngineMethod( PostEffect, getAspectRatio, F32, (),,
  1681. "@return Width over height of the backbuffer." )
  1682. {
  1683. return object->getAspectRatio();
  1684. }
  1685. DefineEngineMethod( PostEffect, dumpShaderDisassembly, String, (),,
  1686. "Dumps this PostEffect shader's disassembly to a temporary text file.\n"
  1687. "@return Full path to the dumped file or an empty string if failed." )
  1688. {
  1689. String fileName;
  1690. object->dumpShaderDisassembly( fileName );
  1691. return fileName;
  1692. }
  1693. DefineEngineMethod( PostEffect, setShaderMacro, void, ( const char* key, const char* value ), ( "" ),
  1694. "Adds a macro to the effect's shader or sets an existing one's value. "
  1695. "This will usually be called within the onAdd or preProcess callback.\n"
  1696. "@param key lval of the macro."
  1697. "@param value rval of the macro, or may be empty."
  1698. "@tsexample\n"
  1699. "function MyPfx::onAdd( %this )\n"
  1700. "{\n"
  1701. " %this.setShaderMacro( \"NUM_SAMPLES\", \"10\" );\n"
  1702. " %this.setShaderMacro( \"HIGH_QUALITY_MODE\" );\n"
  1703. " \n"
  1704. " // In the shader looks like... \n"
  1705. " // #define NUM_SAMPLES 10\n"
  1706. " // #define HIGH_QUALITY_MODE\n"
  1707. "}\n"
  1708. "@endtsexample" )
  1709. {
  1710. object->setShaderMacro( key, value );
  1711. }
  1712. DefineEngineMethod( PostEffect, removeShaderMacro, void, ( const char* key ),,
  1713. "Remove a shader macro. This will usually be called within the preProcess callback.\n"
  1714. "@param key Macro to remove." )
  1715. {
  1716. object->removeShaderMacro( key );
  1717. }
  1718. DefineEngineMethod( PostEffect, clearShaderMacros, void, (),,
  1719. "Remove all shader macros." )
  1720. {
  1721. object->clearShaderMacros();
  1722. }
  1723. DefineEngineFunction( dumpRandomNormalMap, void, (),,
  1724. "Creates a 64x64 normal map texture filled with noise. The texture is saved "
  1725. "to randNormTex.png in the location of the game executable.\n\n"
  1726. "@ingroup GFX")
  1727. {
  1728. GFXTexHandle tex;
  1729. tex.set( 64, 64, GFXFormatR8G8B8A8, &GFXTexturePersistentProfile, "" );
  1730. GFXLockedRect *rect = tex.lock();
  1731. U8 *f = rect->bits;
  1732. for ( U32 i = 0; i < 64*64; i++, f += 4 )
  1733. {
  1734. VectorF vec;
  1735. vec.x = mRandF( -1.0f, 1.0f );
  1736. vec.y = mRandF( -1.0f, 1.0f );
  1737. vec.z = mRandF( -1.0f, 1.0f );
  1738. vec.normalizeSafe();
  1739. f[0] = U8_MAX * ( ( 1.0f + vec.x ) * 0.5f );
  1740. f[1] = U8_MAX * ( ( 1.0f + vec.y ) * 0.5f );
  1741. f[2] = U8_MAX * ( ( 1.0f + vec.z ) * 0.5f );
  1742. f[3] = U8_MAX;
  1743. }
  1744. tex.unlock();
  1745. String path = Torque::FS::MakeUniquePath( "", "randNormTex", "png" );
  1746. tex->dumpToDisk( "png", path );
  1747. }