shaderFeatureGLSL.cpp 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  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 "shaderGen/GLSL/shaderFeatureGLSL.h"
  24. #include "shaderGen/langElement.h"
  25. #include "shaderGen/shaderOp.h"
  26. #include "shaderGen/shaderGenVars.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "materials/matInstance.h"
  29. #include "materials/processedMaterial.h"
  30. #include "materials/materialFeatureTypes.h"
  31. #include "core/util/autoPtr.h"
  32. #include "lighting/advanced/advancedLightBinManager.h"
  33. #include "ts/tsShape.h"
  34. LangElement * ShaderFeatureGLSL::setupTexSpaceMat( Vector<ShaderComponent*> &, // componentList
  35. Var **texSpaceMat )
  36. {
  37. Var *N = (Var*) LangElement::find( "normal" );
  38. Var *B = (Var*) LangElement::find( "B" );
  39. Var *T = (Var*) LangElement::find( "T" );
  40. Var *tangentW = (Var*) LangElement::find( "tangentW" );
  41. // setup matrix var
  42. *texSpaceMat = new Var;
  43. (*texSpaceMat)->setType( "float3x3" );
  44. (*texSpaceMat)->setName( "objToTangentSpace" );
  45. MultiLine * meta = new MultiLine;
  46. meta->addStatement( new GenOp( " @ = float3x3(1,0,0, 0,1,0, 0,0,1);\r\n", new DecOp( *texSpaceMat ) ) );
  47. // Protect against missing normal and tangent.
  48. if ( !N || !T )
  49. {
  50. meta->addStatement( new GenOp( " tSetMatrixRow(@, 0, float3( 1, 0, 0 )); tSetMatrixRow(@, 1,float3( 0, 1, 0 )); tSetMatrixRow(@,2, float3( 0, 0, 1 ));\r\n",
  51. *texSpaceMat, *texSpaceMat, *texSpaceMat ) );
  52. return meta;
  53. }
  54. meta->addStatement( new GenOp( " tSetMatrixRow(@, 0, @);\r\n", *texSpaceMat, T ) );
  55. if ( B )
  56. meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, @);\r\n", *texSpaceMat, B ) );
  57. else
  58. {
  59. if(dStricmp((char*)T->type, "vec4") == 0)
  60. meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, cross( @, normalize(@) ) * @.w);\r\n", *texSpaceMat, T, N, T ) );
  61. else if(tangentW)
  62. meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, cross( @, normalize(@) ) * @);\r\n", *texSpaceMat, T, N, tangentW ) );
  63. else
  64. meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, cross( @, normalize(@) ));\r\n", *texSpaceMat, T, N ) );
  65. }
  66. meta->addStatement( new GenOp( " tSetMatrixRow(@, 2, normalize(@));\r\n", *texSpaceMat, N ) );
  67. return meta;
  68. }
  69. LangElement* ShaderFeatureGLSL::assignColor( LangElement *elem,
  70. Material::BlendOp blend,
  71. LangElement *lerpElem,
  72. ShaderFeature::OutputTarget outputTarget )
  73. {
  74. // search for color var
  75. Var *color = (Var*) LangElement::find( getOutputTargetVarName(outputTarget) );
  76. if ( !color )
  77. {
  78. // create color var
  79. color = new Var;
  80. color->setType( "vec4" );
  81. color->setName( getOutputTargetVarName( outputTarget ) );
  82. color->setStructName( "OUT" );
  83. return new GenOp( "@ = @", color, elem );
  84. }
  85. LangElement *assign;
  86. switch ( blend )
  87. {
  88. case Material::Add:
  89. assign = new GenOp( "@ += @", color, elem );
  90. break;
  91. case Material::Sub:
  92. assign = new GenOp( "@ -= @", color, elem );
  93. break;
  94. case Material::Mul:
  95. assign = new GenOp( "@ *= @", color, elem );
  96. break;
  97. case Material::AddAlpha:
  98. assign = new GenOp( "@ += @ * @.a", color, elem, elem );
  99. break;
  100. case Material::LerpAlpha:
  101. if ( !lerpElem )
  102. lerpElem = elem;
  103. assign = new GenOp( "@.rgb = lerp( @.rgb, (@).rgb, (@).a )", color, color, elem, lerpElem );
  104. break;
  105. case Material::ToneMap:
  106. assign = new GenOp( "@ = 1.0 - exp(-1.0 * @ * @)", color, color, elem );
  107. break;
  108. default:
  109. AssertFatal(false, "Unrecognized color blendOp");
  110. // Fallthru
  111. case Material::None:
  112. assign = new GenOp( "@ = @", color, elem );
  113. break;
  114. }
  115. return assign;
  116. }
  117. LangElement *ShaderFeatureGLSL::expandNormalMap( LangElement *sampleNormalOp,
  118. LangElement *normalDecl,
  119. LangElement *normalVar,
  120. const MaterialFeatureData &fd )
  121. {
  122. MultiLine *meta = new MultiLine;
  123. if ( fd.features.hasFeature( MFT_IsDXTnm, getProcessIndex() ) )
  124. {
  125. if ( fd.features[MFT_ImposterVert] )
  126. {
  127. // The imposter system uses object space normals and
  128. // encodes them with the z axis in the alpha component.
  129. meta->addStatement( new GenOp( " @ = float4( normalize( @.xyw * 2.0 - 1.0 ), 0.0 ); // Obj DXTnm\r\n", normalDecl, sampleNormalOp ) );
  130. }
  131. else
  132. {
  133. // DXT Swizzle trick
  134. meta->addStatement( new GenOp( " @ = float4( @.ag * 2.0 - 1.0, 0.0, 0.0 ); // DXTnm\r\n", normalDecl, sampleNormalOp ) );
  135. meta->addStatement( new GenOp( " @.z = sqrt( 1.0 - dot( @.xy, @.xy ) ); // DXTnm\r\n", normalVar, normalVar, normalVar ) );
  136. }
  137. }
  138. else
  139. {
  140. meta->addStatement( new GenOp( " @ = @;\r\n", normalDecl, sampleNormalOp ) );
  141. meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 1.0;\r\n", normalVar, normalVar ) );
  142. }
  143. return meta;
  144. }
  145. ShaderFeatureGLSL::ShaderFeatureGLSL()
  146. {
  147. output = NULL;
  148. }
  149. Var * ShaderFeatureGLSL::getVertTexCoord( const String &name )
  150. {
  151. Var *inTex = NULL;
  152. for( U32 i=0; i<LangElement::elementList.size(); i++ )
  153. {
  154. if( !dStrcmp( (char*)LangElement::elementList[i]->name, name.c_str() ) )
  155. {
  156. inTex = dynamic_cast<Var*>( LangElement::elementList[i] );
  157. if ( inTex )
  158. {
  159. // NOTE: This used to do this check...
  160. //
  161. // dStrcmp( (char*)inTex->structName, "IN" )
  162. //
  163. // ... to ensure that the var was from the input
  164. // vertex structure, but this kept some features
  165. // ( ie. imposter vert ) from decoding their own
  166. // coords for other features to use.
  167. //
  168. // If we run into issues with collisions between
  169. // IN vars and local vars we may need to revise.
  170. break;
  171. }
  172. }
  173. }
  174. return inTex;
  175. }
  176. Var* ShaderFeatureGLSL::getOutObjToTangentSpace( Vector<ShaderComponent*> &componentList,
  177. MultiLine *meta,
  178. const MaterialFeatureData &fd )
  179. {
  180. Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
  181. if ( !outObjToTangentSpace )
  182. meta->addStatement( setupTexSpaceMat( componentList, &outObjToTangentSpace ) );
  183. return outObjToTangentSpace;
  184. }
  185. Var* ShaderFeatureGLSL::getOutWorldToTangent( Vector<ShaderComponent*> &componentList,
  186. MultiLine *meta,
  187. const MaterialFeatureData &fd )
  188. {
  189. Var *outWorldToTangent = (Var*)LangElement::find( "outWorldToTangent" );
  190. if ( outWorldToTangent )
  191. return outWorldToTangent;
  192. Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
  193. if ( !worldToTangent )
  194. {
  195. Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
  196. if(!fd.features[MFT_ParticleNormal])
  197. {
  198. // turn obj->tangent into world->tangent
  199. worldToTangent = new Var;
  200. worldToTangent->setType( "float3x3" );
  201. worldToTangent->setName( "worldToTangent" );
  202. LangElement *worldToTangentDecl = new DecOp( worldToTangent );
  203. // Get the world->obj transform
  204. Var *worldToObj = (Var*)LangElement::find( "worldToObj" );
  205. if ( !worldToObj )
  206. {
  207. worldToObj = new Var;
  208. worldToObj->setName( "worldToObj" );
  209. if ( fd.features[MFT_UseInstancing] )
  210. {
  211. // We just use transpose to convert the 3x3 portion of
  212. // the object transform to its inverse.
  213. worldToObj->setType( "float3x3" );
  214. Var *objTrans = getObjTrans( componentList, true, meta );
  215. meta->addStatement( new GenOp( " @ = transpose( float3x3(@) ); // Instancing!\r\n", new DecOp( worldToObj ), objTrans ) );
  216. }
  217. else
  218. {
  219. worldToObj->setType( "float4x4" );
  220. worldToObj->uniform = true;
  221. worldToObj->constSortPos = cspPrimitive;
  222. }
  223. }
  224. // assign world->tangent transform
  225. meta->addStatement( new GenOp( " @ = tMul( @, float3x3(@) );\r\n", worldToTangentDecl, texSpaceMat, worldToObj ) );
  226. }
  227. else
  228. {
  229. // Assume particle normal generation has set this up in the proper space
  230. worldToTangent = texSpaceMat;
  231. }
  232. }
  233. // send transform to pixel shader
  234. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  235. outWorldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  236. outWorldToTangent->setName( "outWorldToTangent" );
  237. outWorldToTangent->setStructName( "OUT" );
  238. outWorldToTangent->setType( "float3x3" );
  239. meta->addStatement( new GenOp( " @ = @;\r\n", outWorldToTangent, worldToTangent ) );
  240. return outWorldToTangent;
  241. }
  242. Var* ShaderFeatureGLSL::getOutViewToTangent( Vector<ShaderComponent*> &componentList,
  243. MultiLine *meta,
  244. const MaterialFeatureData &fd )
  245. {
  246. Var *outViewToTangent = (Var*)LangElement::find( "outViewToTangent" );
  247. if ( outViewToTangent )
  248. return outViewToTangent;
  249. Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
  250. if ( !viewToTangent )
  251. {
  252. Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
  253. if(!fd.features[MFT_ParticleNormal])
  254. {
  255. // turn obj->tangent into world->tangent
  256. viewToTangent = new Var;
  257. viewToTangent->setType( "float3x3" );
  258. viewToTangent->setName( "viewToTangent" );
  259. LangElement *viewToTangentDecl = new DecOp( viewToTangent );
  260. // Get the view->obj transform
  261. Var *viewToObj = getInvWorldView( componentList, fd.features[MFT_UseInstancing], meta );
  262. // assign world->tangent transform
  263. meta->addStatement( new GenOp( " @ = tMul( (@), float3x3(@) );\r\n", viewToTangentDecl, texSpaceMat, viewToObj ) );
  264. }
  265. else
  266. {
  267. // Assume particle normal generation has set this up in the proper space
  268. viewToTangent = texSpaceMat;
  269. }
  270. }
  271. // send transform to pixel shader
  272. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  273. outViewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  274. outViewToTangent->setName( "outViewToTangent" );
  275. outViewToTangent->setStructName( "OUT" );
  276. outViewToTangent->setType( "float3x3" );
  277. meta->addStatement( new GenOp( " @ = @;\r\n", outViewToTangent, viewToTangent ) );
  278. return outViewToTangent;
  279. }
  280. Var* ShaderFeatureGLSL::getOutTexCoord( const char *name,
  281. const char *type,
  282. bool useTexAnim,
  283. MultiLine *meta,
  284. Vector<ShaderComponent*> &componentList )
  285. {
  286. String outTexName = String::ToString( "out_%s", name );
  287. Var *texCoord = (Var*)LangElement::find( outTexName );
  288. if ( !texCoord )
  289. {
  290. Var *inTex = getVertTexCoord( name );
  291. AssertFatal( inTex, "ShaderFeatureGLSL::getOutTexCoord - Unknown vertex input coord!" );
  292. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  293. texCoord = connectComp->getElement( RT_TEXCOORD );
  294. texCoord->setName( outTexName );
  295. texCoord->setStructName( "OUT" );
  296. texCoord->setType( type );
  297. if( useTexAnim )
  298. {
  299. inTex->setType( "vec4" );
  300. // create texture mat var
  301. Var *texMat = new Var;
  302. texMat->setType( "float4x4" );
  303. texMat->setName( "texMat" );
  304. texMat->uniform = true;
  305. texMat->constSortPos = cspPass;
  306. // Statement allows for casting of different types which
  307. // eliminates vector truncation problems.
  308. String statement = String::ToString( " @ = %s(tMul(@, @).xy);\r\n", type );
  309. meta->addStatement( new GenOp( statement , texCoord, texMat, inTex ) );
  310. }
  311. else
  312. {
  313. // Statement allows for casting of different types which
  314. // eliminates vector truncation problems.
  315. String statement = String::ToString( " @ = %s(@);\r\n", type );
  316. meta->addStatement( new GenOp( statement, texCoord, inTex ) );
  317. }
  318. }
  319. AssertFatal( dStrcmp( type, (const char*)texCoord->type ) == 0,
  320. "ShaderFeatureGLSL::getOutTexCoord - Type mismatch!" );
  321. return texCoord;
  322. }
  323. Var* ShaderFeatureGLSL::getInTexCoord( const char *name,
  324. const char *type,
  325. Vector<ShaderComponent*> &componentList )
  326. {
  327. Var* texCoord = (Var*)LangElement::find( name );
  328. if ( !texCoord )
  329. {
  330. ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
  331. texCoord = connectComp->getElement( RT_TEXCOORD );
  332. texCoord->setName( name );
  333. texCoord->setStructName( "IN" );
  334. texCoord->setType( type );
  335. }
  336. AssertFatal( dStrcmp( type, (const char*)texCoord->type ) == 0,
  337. "ShaderFeatureGLSL::getInTexCoord - Type mismatch!" );
  338. return texCoord;
  339. }
  340. Var* ShaderFeatureGLSL::getInColor( const char *name,
  341. const char *type,
  342. Vector<ShaderComponent*> &componentList )
  343. {
  344. Var *inColor = (Var*)LangElement::find( name );
  345. if ( !inColor )
  346. {
  347. ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
  348. inColor = connectComp->getElement( RT_COLOR );
  349. inColor->setName( name );
  350. inColor->setStructName( "IN" );
  351. inColor->setType( type );
  352. }
  353. AssertFatal( dStrcmp( type, (const char*)inColor->type ) == 0,
  354. "ShaderFeatureGLSL::getInColor - Type mismatch!" );
  355. return inColor;
  356. }
  357. Var* ShaderFeatureGLSL::addOutVpos( MultiLine *meta,
  358. Vector<ShaderComponent*> &componentList )
  359. {
  360. /*
  361. // Nothing to do if we're on SM 3.0... we use the real vpos.
  362. if ( GFX->getPixelShaderVersion() >= 3.0f )
  363. return NULL;
  364. */
  365. // For SM 2.x we need to generate the vpos in the vertex shader
  366. // and pass it as a texture coord to the pixel shader.
  367. Var *outVpos = (Var*)LangElement::find( "outVpos" );
  368. if ( !outVpos )
  369. {
  370. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  371. outVpos = connectComp->getElement( RT_TEXCOORD );
  372. outVpos->setName( "outVpos" );
  373. outVpos->setStructName( "OUT" );
  374. outVpos->setType( "vec4" );
  375. Var *outPosition = (Var*) LangElement::find( "gl_Position" );
  376. AssertFatal( outPosition, "ShaderFeatureGLSL::addOutVpos - Didn't find the output position." );
  377. meta->addStatement( new GenOp( " @ = @;\r\n", outVpos, outPosition ) );
  378. }
  379. return outVpos;
  380. }
  381. Var* ShaderFeatureGLSL::getInVpos( MultiLine *meta,
  382. Vector<ShaderComponent*> &componentList )
  383. {
  384. Var *inVpos = (Var*)LangElement::find( "vpos" );
  385. if ( inVpos )
  386. return inVpos;
  387. ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
  388. /*
  389. if ( GFX->getPixelShaderVersion() >= 3.0f )
  390. {
  391. inVpos = connectComp->getElement( RT_VPOS );
  392. inVpos->setName( "vpos" );
  393. inVpos->setStructName( "IN" );
  394. inVpos->setType( "vec2" );
  395. return inVpos;
  396. }
  397. */
  398. inVpos = connectComp->getElement( RT_TEXCOORD );
  399. inVpos->setName( "inVpos" );
  400. inVpos->setStructName( "IN" );
  401. inVpos->setType( "vec4" );
  402. Var *vpos = new Var( "vpos", "vec2" );
  403. meta->addStatement( new GenOp( " @ = @.xy / @.w;\r\n", new DecOp( vpos ), inVpos, inVpos ) );
  404. return vpos;
  405. }
  406. Var* ShaderFeatureGLSL::getInWorldToTangent( Vector<ShaderComponent*> &componentList )
  407. {
  408. Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
  409. if ( !worldToTangent )
  410. {
  411. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  412. worldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  413. worldToTangent->setName( "worldToTangent" );
  414. worldToTangent->setStructName( "IN" );
  415. worldToTangent->setType( "float3x3" );
  416. }
  417. return worldToTangent;
  418. }
  419. Var* ShaderFeatureGLSL::getInViewToTangent( Vector<ShaderComponent*> &componentList )
  420. {
  421. Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
  422. if ( !viewToTangent )
  423. {
  424. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  425. viewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
  426. viewToTangent->setName( "viewToTangent" );
  427. viewToTangent->setStructName( "IN" );
  428. viewToTangent->setType( "float3x3" );
  429. }
  430. return viewToTangent;
  431. }
  432. Var* ShaderFeatureGLSL::getNormalMapTex()
  433. {
  434. Var *normalMap = (Var*)LangElement::find( "bumpMap" );
  435. if ( !normalMap )
  436. {
  437. normalMap = new Var;
  438. normalMap->setType( "sampler2D" );
  439. normalMap->setName( "bumpMap" );
  440. normalMap->uniform = true;
  441. normalMap->sampler = true;
  442. normalMap->constNum = Var::getTexUnitNum();
  443. }
  444. return normalMap;
  445. }
  446. Var* ShaderFeatureGLSL::getObjTrans( Vector<ShaderComponent*> &componentList,
  447. bool useInstancing,
  448. MultiLine *meta )
  449. {
  450. Var *objTrans = (Var*)LangElement::find( "objTrans" );
  451. if ( objTrans )
  452. return objTrans;
  453. if ( useInstancing )
  454. {
  455. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  456. Var *instObjTrans = vertStruct->getElement( RT_TEXCOORD, 4, 4 );
  457. instObjTrans->setStructName( "IN" );
  458. instObjTrans->setName( "inst_objectTrans" );
  459. mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+0 );
  460. mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+1 );
  461. mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+2 );
  462. mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+3 );
  463. objTrans = new Var;
  464. objTrans->setType( "mat4x4" );
  465. objTrans->setName( "objTrans" );
  466. meta->addStatement( new GenOp( " @ = mat4x4( // Instancing!\r\n", new DecOp( objTrans ), instObjTrans ) );
  467. meta->addStatement( new GenOp( " @[0],\r\n", instObjTrans ) );
  468. meta->addStatement( new GenOp( " @[1],\r\n", instObjTrans ) );
  469. meta->addStatement( new GenOp( " @[2],\r\n",instObjTrans ) );
  470. meta->addStatement( new GenOp( " @[3] );\r\n", instObjTrans ) );
  471. }
  472. else
  473. {
  474. objTrans = new Var;
  475. objTrans->setType( "float4x4" );
  476. objTrans->setName( "objTrans" );
  477. objTrans->uniform = true;
  478. objTrans->constSortPos = cspPrimitive;
  479. }
  480. return objTrans;
  481. }
  482. Var* ShaderFeatureGLSL::getModelView( Vector<ShaderComponent*> &componentList,
  483. bool useInstancing,
  484. MultiLine *meta )
  485. {
  486. Var *modelview = (Var*)LangElement::find( "modelview" );
  487. if ( modelview )
  488. return modelview;
  489. if ( useInstancing )
  490. {
  491. Var *objTrans = getObjTrans( componentList, useInstancing, meta );
  492. Var *viewProj = (Var*)LangElement::find( "viewProj" );
  493. if ( !viewProj )
  494. {
  495. viewProj = new Var;
  496. viewProj->setType( "float4x4" );
  497. viewProj->setName( "viewProj" );
  498. viewProj->uniform = true;
  499. viewProj->constSortPos = cspPass;
  500. }
  501. modelview = new Var;
  502. modelview->setType( "float4x4" );
  503. modelview->setName( "modelview" );
  504. meta->addStatement( new GenOp( " @ = tMul( @, @ ); // Instancing!\r\n", new DecOp( modelview ), viewProj, objTrans ) );
  505. }
  506. else
  507. {
  508. modelview = new Var;
  509. modelview->setType( "float4x4" );
  510. modelview->setName( "modelview" );
  511. modelview->uniform = true;
  512. modelview->constSortPos = cspPrimitive;
  513. }
  514. return modelview;
  515. }
  516. Var* ShaderFeatureGLSL::getWorldView( Vector<ShaderComponent*> &componentList,
  517. bool useInstancing,
  518. MultiLine *meta )
  519. {
  520. Var *worldView = (Var*)LangElement::find( "worldViewOnly" );
  521. if ( worldView )
  522. return worldView;
  523. if ( useInstancing )
  524. {
  525. Var *objTrans = getObjTrans( componentList, useInstancing, meta );
  526. Var *worldToCamera = (Var*)LangElement::find( "worldToCamera" );
  527. if ( !worldToCamera )
  528. {
  529. worldToCamera = new Var;
  530. worldToCamera->setType( "float4x4" );
  531. worldToCamera->setName( "worldToCamera" );
  532. worldToCamera->uniform = true;
  533. worldToCamera->constSortPos = cspPass;
  534. }
  535. worldView = new Var;
  536. worldView->setType( "float4x4" );
  537. worldView->setName( "worldViewOnly" );
  538. meta->addStatement( new GenOp( " @ = tMul( @, @ ); // Instancing!\r\n", new DecOp( worldView ), worldToCamera, objTrans ) );
  539. }
  540. else
  541. {
  542. worldView = new Var;
  543. worldView->setType( "float4x4" );
  544. worldView->setName( "worldViewOnly" );
  545. worldView->uniform = true;
  546. worldView->constSortPos = cspPrimitive;
  547. }
  548. return worldView;
  549. }
  550. Var* ShaderFeatureGLSL::getInvWorldView( Vector<ShaderComponent*> &componentList,
  551. bool useInstancing,
  552. MultiLine *meta )
  553. {
  554. Var *viewToObj = (Var*)LangElement::find( "viewToObj" );
  555. if ( viewToObj )
  556. return viewToObj;
  557. if ( useInstancing )
  558. {
  559. Var *worldView = getWorldView( componentList, useInstancing, meta );
  560. viewToObj = new Var;
  561. viewToObj->setType( "float3x3" );
  562. viewToObj->setName( "viewToObj" );
  563. // We just use transpose to convert the 3x3 portion
  564. // of the world view transform into its inverse.
  565. meta->addStatement( new GenOp( " @ = transpose( float3x3(@) ); // Instancing!\r\n", new DecOp( viewToObj ), worldView ) );
  566. }
  567. else
  568. {
  569. viewToObj = new Var;
  570. viewToObj->setType( "float4x4" );
  571. viewToObj->setName( "viewToObj" );
  572. viewToObj->uniform = true;
  573. viewToObj->constSortPos = cspPrimitive;
  574. }
  575. return viewToObj;
  576. }
  577. void ShaderFeatureGLSL::getWsPosition( Vector<ShaderComponent*> &componentList,
  578. bool useInstancing,
  579. MultiLine *meta,
  580. LangElement *wsPosition )
  581. {
  582. Var *inPosition = (Var*)LangElement::find( "wsPosition" );
  583. if ( inPosition )
  584. {
  585. meta->addStatement( new GenOp( " @ = @.xyz;\r\n",
  586. wsPosition, inPosition ) );
  587. return;
  588. }
  589. // Get the input position.
  590. inPosition = (Var*)LangElement::find( "inPosition" );
  591. if ( !inPosition )
  592. inPosition = (Var*)LangElement::find( "position" );
  593. AssertFatal( inPosition, "ShaderFeatureGLSL::getWsPosition - The vertex position was not found!" );
  594. Var *objTrans = getObjTrans( componentList, useInstancing, meta );
  595. meta->addStatement( new GenOp( " @ = tMul( @, vec4( @.xyz, 1 ) ).xyz;\r\n",
  596. wsPosition, objTrans, inPosition ) );
  597. }
  598. Var* ShaderFeatureGLSL::addOutWsPosition( Vector<ShaderComponent*> &componentList,
  599. bool useInstancing,
  600. MultiLine *meta )
  601. {
  602. Var *outWsPosition = (Var*)LangElement::find( "outWsPosition" );
  603. if ( !outWsPosition )
  604. {
  605. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  606. outWsPosition = connectComp->getElement( RT_TEXCOORD );
  607. outWsPosition->setName( "outWsPosition" );
  608. outWsPosition->setStructName( "OUT" );
  609. outWsPosition->setType( "vec3" );
  610. getWsPosition( componentList, useInstancing, meta, outWsPosition );
  611. }
  612. return outWsPosition;
  613. }
  614. Var* ShaderFeatureGLSL::getInWsPosition( Vector<ShaderComponent*> &componentList )
  615. {
  616. Var *wsPosition = (Var*)LangElement::find( "wsPosition" );
  617. if ( !wsPosition )
  618. {
  619. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  620. wsPosition = connectComp->getElement( RT_TEXCOORD );
  621. wsPosition->setName( "wsPosition" );
  622. wsPosition->setStructName( "IN" );
  623. wsPosition->setType( "vec3" );
  624. }
  625. return wsPosition;
  626. }
  627. Var* ShaderFeatureGLSL::getWsView( Var *wsPosition, MultiLine *meta )
  628. {
  629. Var *wsView = (Var*)LangElement::find( "wsView" );
  630. if ( !wsView )
  631. {
  632. wsView = new Var( "wsView", "vec3" );
  633. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  634. if ( !eyePos )
  635. {
  636. eyePos = new Var;
  637. eyePos->setType( "vec3" );
  638. eyePos->setName( "eyePosWorld" );
  639. eyePos->uniform = true;
  640. eyePos->constSortPos = cspPass;
  641. }
  642. meta->addStatement( new GenOp( " @ = normalize( @ - @ );\r\n",
  643. new DecOp( wsView ), eyePos, wsPosition ) );
  644. }
  645. return wsView;
  646. }
  647. Var* ShaderFeatureGLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &componentList,
  648. MultiLine *meta,
  649. bool useTexAnim )
  650. {
  651. // Check if its already added.
  652. Var *outTex = (Var*)LangElement::find( "detCoord" );
  653. if ( outTex )
  654. return outTex;
  655. // Grab incoming texture coords.
  656. Var *inTex = getVertTexCoord( "texCoord" );
  657. // create detail variable
  658. Var *detScale = new Var;
  659. detScale->setType( "vec2" );
  660. detScale->setName( "detailScale" );
  661. detScale->uniform = true;
  662. detScale->constSortPos = cspPotentialPrimitive;
  663. // grab connector texcoord register
  664. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  665. outTex = connectComp->getElement( RT_TEXCOORD );
  666. outTex->setName( "detCoord" );
  667. outTex->setStructName( "OUT" );
  668. outTex->setType( "vec2" );
  669. if ( useTexAnim )
  670. {
  671. inTex->setType( "vec4" );
  672. // Find or create the texture matrix.
  673. Var *texMat = (Var*)LangElement::find( "texMat" );
  674. if ( !texMat )
  675. {
  676. texMat = new Var;
  677. texMat->setType( "float4x4" );
  678. texMat->setName( "texMat" );
  679. texMat->uniform = true;
  680. texMat->constSortPos = cspPass;
  681. }
  682. meta->addStatement( new GenOp( " @ = tMul(@, @).xy * @;\r\n", outTex, texMat, inTex, detScale ) );
  683. }
  684. else
  685. {
  686. // setup output to mul texCoord by detail scale
  687. meta->addStatement( new GenOp( " @ = @ * @;\r\n", outTex, inTex, detScale ) );
  688. }
  689. return outTex;
  690. }
  691. //****************************************************************************
  692. // Base Texture
  693. //****************************************************************************
  694. DiffuseMapFeatGLSL::DiffuseMapFeatGLSL()
  695. : mTorqueDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/gl/torque.glsl"))
  696. {
  697. addDependency(&mTorqueDep);
  698. }
  699. void DiffuseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  700. const MaterialFeatureData &fd )
  701. {
  702. MultiLine *meta = new MultiLine;
  703. getOutTexCoord( "texCoord",
  704. "vec2",
  705. fd.features[MFT_TexAnim],
  706. meta,
  707. componentList );
  708. output = meta;
  709. }
  710. U32 DiffuseMapFeatGLSL::getOutputTargets(const MaterialFeatureData &fd) const
  711. {
  712. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  713. }
  714. void DiffuseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  715. const MaterialFeatureData &fd )
  716. {
  717. // grab connector texcoord register
  718. Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
  719. //determine output target
  720. ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
  721. if (fd.features[MFT_isDeferred])
  722. targ = ShaderFeature::RenderTarget1;
  723. // create texture var
  724. Var *diffuseMap = new Var;
  725. diffuseMap->setType( "sampler2D" );
  726. diffuseMap->setName( "diffuseMap" );
  727. diffuseMap->uniform = true;
  728. diffuseMap->sampler = true;
  729. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  730. // create sample color var
  731. Var *diffColor = new Var;
  732. diffColor->setType("vec4");
  733. diffColor->setName("diffuseColor");
  734. LangElement *colorDecl = new DecOp( diffColor );
  735. MultiLine * meta = new MultiLine;
  736. output = meta;
  737. if ( fd.features[MFT_CubeMap] )
  738. {
  739. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n",
  740. colorDecl,
  741. diffuseMap,
  742. inTex ) );
  743. if (!fd.features[MFT_Imposter])
  744. meta->addStatement( new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor) );
  745. meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul, NULL, targ) ) );
  746. }
  747. else if(fd.features[MFT_DiffuseMapAtlas])
  748. {
  749. // Handle atlased textures
  750. // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
  751. Var *atlasedTex = new Var;
  752. atlasedTex->setName("atlasedTexCoord");
  753. atlasedTex->setType("vec2");
  754. LangElement *atDecl = new DecOp(atlasedTex);
  755. // Parameters of the texture atlas
  756. Var *atParams = new Var;
  757. atParams->setType("vec4");
  758. atParams->setName("diffuseAtlasParams");
  759. atParams->uniform = true;
  760. atParams->constSortPos = cspPotentialPrimitive;
  761. // Parameters of the texture (tile) this object is using in the atlas
  762. Var *tileParams = new Var;
  763. tileParams->setType("vec4");
  764. tileParams->setName("diffuseAtlasTileParams");
  765. tileParams->uniform = true;
  766. tileParams->constSortPos = cspPotentialPrimitive;
  767. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  768. if(is_sm3)
  769. {
  770. // Figure out the mip level
  771. meta->addStatement(new GenOp(" float2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
  772. meta->addStatement(new GenOp(" float2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
  773. meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
  774. meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
  775. // And the size of the mip level
  776. meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
  777. meta->addStatement(new GenOp(" float2 mipSz = mipPixSz / @.xy;\r\n", atParams));
  778. }
  779. else
  780. {
  781. meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
  782. }
  783. // Tiling mode
  784. // TODO: Select wrap or clamp somehow
  785. if( true ) // Wrap
  786. meta->addStatement(new GenOp(" @ = frac(@);\r\n", atDecl, inTex));
  787. else // Clamp
  788. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
  789. // Finally scale/offset, and correct for filtering
  790. meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
  791. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  792. // Add a newline
  793. meta->addStatement(new GenOp( "\r\n"));
  794. // For the rest of the feature...
  795. inTex = atlasedTex;
  796. // To dump out UV coords...
  797. //#define DEBUG_ATLASED_UV_COORDS
  798. #ifdef DEBUG_ATLASED_UV_COORDS
  799. if(!fd.features[MFT_DeferredConditioner])
  800. {
  801. meta->addStatement(new GenOp(" @ = vec4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
  802. meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
  803. return;
  804. }
  805. #endif
  806. if(is_sm3)
  807. {
  808. meta->addStatement(new GenOp( " @ = tex2Dlod(@, float4(@, 0.0, mipLod));\r\n",
  809. new DecOp(diffColor), diffuseMap, inTex));
  810. if (!fd.features[MFT_Imposter])
  811. meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
  812. }
  813. else
  814. {
  815. meta->addStatement(new GenOp( " @ = tex2D(@, @);\r\n",
  816. new DecOp(diffColor), diffuseMap, inTex));
  817. if (!fd.features[MFT_Imposter])
  818. meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
  819. }
  820. meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
  821. }
  822. else
  823. {
  824. meta->addStatement(new GenOp("@ = tex2D(@, @);\r\n", colorDecl, diffuseMap, inTex));
  825. if (!fd.features[MFT_Imposter])
  826. meta->addStatement(new GenOp(" @ = toLinear(@);\r\n", diffColor, diffColor));
  827. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
  828. }
  829. }
  830. ShaderFeature::Resources DiffuseMapFeatGLSL::getResources( const MaterialFeatureData &fd )
  831. {
  832. Resources res;
  833. res.numTex = 1;
  834. res.numTexReg = 1;
  835. return res;
  836. }
  837. void DiffuseMapFeatGLSL::setTexData( Material::StageData &stageDat,
  838. const MaterialFeatureData &fd,
  839. RenderPassData &passData,
  840. U32 &texIndex )
  841. {
  842. GFXTextureObject *tex = stageDat.getTex( MFT_DiffuseMap );
  843. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  844. passData.mTexSlot[ texIndex++ ].texObject = tex;
  845. }
  846. //****************************************************************************
  847. // Overlay Texture
  848. //****************************************************************************
  849. void OverlayTexFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  850. const MaterialFeatureData &fd )
  851. {
  852. Var *inTex = getVertTexCoord( "texCoord2" );
  853. AssertFatal( inTex, "OverlayTexFeatGLSL::processVert() - The second UV set was not found!" );
  854. // grab connector texcoord register
  855. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  856. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  857. outTex->setName( "outTexCoord2" );
  858. outTex->setStructName( "OUT" );
  859. outTex->setType( "vec2" );
  860. if( fd.features[MFT_TexAnim] )
  861. {
  862. inTex->setType( "vec4" );
  863. // Find or create the texture matrix.
  864. Var *texMat = (Var*)LangElement::find( "texMat" );
  865. if ( !texMat )
  866. {
  867. texMat = new Var;
  868. texMat->setType( "float4x4" );
  869. texMat->setName( "texMat" );
  870. texMat->uniform = true;
  871. texMat->constSortPos = cspPass;
  872. }
  873. output = new GenOp( " @ = tMul(@, @);\r\n", outTex, texMat, inTex );
  874. return;
  875. }
  876. // setup language elements to output incoming tex coords to output
  877. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  878. }
  879. void OverlayTexFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  880. const MaterialFeatureData &fd )
  881. {
  882. // grab connector texcoord register
  883. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  884. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  885. inTex->setName( "texCoord2" );
  886. inTex->setStructName( "IN" );
  887. inTex->setType( "vec2" );
  888. // create texture var
  889. Var *diffuseMap = new Var;
  890. diffuseMap->setType( "sampler2D" );
  891. diffuseMap->setName( "overlayMap" );
  892. diffuseMap->uniform = true;
  893. diffuseMap->sampler = true;
  894. diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  895. LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
  896. output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
  897. }
  898. ShaderFeature::Resources OverlayTexFeatGLSL::getResources( const MaterialFeatureData &fd )
  899. {
  900. Resources res;
  901. res.numTex = 1;
  902. res.numTexReg = 1;
  903. return res;
  904. }
  905. void OverlayTexFeatGLSL::setTexData( Material::StageData &stageDat,
  906. const MaterialFeatureData &fd,
  907. RenderPassData &passData,
  908. U32 &texIndex )
  909. {
  910. GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
  911. if ( tex )
  912. {
  913. passData.mSamplerNames[ texIndex ] = "overlayMap";
  914. passData.mTexSlot[ texIndex++ ].texObject = tex;
  915. }
  916. }
  917. //****************************************************************************
  918. // Diffuse color
  919. //****************************************************************************
  920. U32 DiffuseFeatureGLSL::getOutputTargets(const MaterialFeatureData &fd) const
  921. {
  922. return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
  923. }
  924. void DiffuseFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  925. const MaterialFeatureData &fd )
  926. {
  927. Var* diffuseMaterialColor = new Var;
  928. diffuseMaterialColor->setType( "vec4" );
  929. diffuseMaterialColor->setName( "diffuseMaterialColor" );
  930. diffuseMaterialColor->uniform = true;
  931. diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
  932. MultiLine* meta = new MultiLine;
  933. Var *col = (Var*)LangElement::find("col");
  934. ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
  935. if (fd.features[MFT_isDeferred])
  936. {
  937. targ = ShaderFeature::RenderTarget1;
  938. col = (Var*)LangElement::find("col1");
  939. MultiLine * meta = new MultiLine;
  940. if (!col)
  941. {
  942. // create color var
  943. col = new Var;
  944. col->setType("vec4");
  945. col->setName(getOutputTargetVarName(targ));
  946. col->setStructName("OUT");
  947. meta->addStatement(new GenOp(" @ = vec4(1.0);\r\n", col));
  948. }
  949. }
  950. Material::BlendOp op;
  951. if (fd.features[MFT_DiffuseMap])
  952. op = Material::Mul;
  953. else
  954. op = Material::None;
  955. meta->addStatement(new GenOp(" @;\r\n", assignColor(diffuseMaterialColor, op, NULL, targ)));
  956. output = meta;
  957. }
  958. //****************************************************************************
  959. // Diffuse vertex color
  960. //****************************************************************************
  961. void DiffuseVertColorFeatureGLSL::processVert( Vector< ShaderComponent* >& componentList,
  962. const MaterialFeatureData& fd )
  963. {
  964. // Create vertex color connector if it doesn't exist.
  965. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  966. if( !outColor )
  967. {
  968. // Search for vert color.
  969. Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
  970. if( !inColor )
  971. {
  972. output = NULL;
  973. return;
  974. }
  975. // Create connector.
  976. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  977. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  978. Var* outColor = connectComp->getElement( RT_COLOR );
  979. outColor->setName( "vertColor" );
  980. outColor->setStructName( "OUT" );
  981. outColor->setType( "vec4" );
  982. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  983. }
  984. else
  985. output = NULL; // Nothing we need to do.
  986. }
  987. void DiffuseVertColorFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  988. const MaterialFeatureData &fd )
  989. {
  990. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  991. if( !vertColor )
  992. {
  993. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  994. AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  995. vertColor = connectComp->getElement( RT_COLOR );
  996. vertColor->setName( "vertColor" );
  997. vertColor->setStructName( "IN" );
  998. vertColor->setType( "vec4" );
  999. }
  1000. MultiLine* meta = new MultiLine;
  1001. if (fd.features[MFT_isDeferred])
  1002. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul, NULL, ShaderFeature::RenderTarget1)));
  1003. else
  1004. meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul)));
  1005. output = meta;
  1006. }
  1007. //****************************************************************************
  1008. // Lightmap
  1009. //****************************************************************************
  1010. void LightmapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1011. const MaterialFeatureData &fd )
  1012. {
  1013. // grab tex register from incoming vert
  1014. Var *inTex = getVertTexCoord( "texCoord2" );
  1015. // grab connector texcoord register
  1016. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1017. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1018. outTex->setName( "texCoord2" );
  1019. outTex->setStructName( "OUT" );
  1020. outTex->setType( "vec2" );
  1021. // setup language elements to output incoming tex coords to output
  1022. output = new GenOp( " @ = @;\r\n", outTex, inTex );
  1023. }
  1024. void LightmapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1025. const MaterialFeatureData &fd )
  1026. {
  1027. // grab connector texcoord register
  1028. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1029. Var *inTex = connectComp->getElement( RT_TEXCOORD );
  1030. inTex->setName( "texCoord2" );
  1031. inTex->setStructName( "IN" );
  1032. inTex->setType( "vec2" );
  1033. // create texture var
  1034. Var *lightMap = new Var;
  1035. lightMap->setType( "sampler2D" );
  1036. lightMap->setName( "lightMap" );
  1037. lightMap->uniform = true;
  1038. lightMap->sampler = true;
  1039. lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1040. // argh, pixel specular should prob use this too
  1041. if( fd.features[MFT_NormalMap] )
  1042. {
  1043. Var *lmColor = new Var;
  1044. lmColor->setName( "lmColor" );
  1045. lmColor->setType( "vec4" );
  1046. LangElement *lmColorDecl = new DecOp( lmColor );
  1047. output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
  1048. return;
  1049. }
  1050. // Add realtime lighting, if it is available
  1051. LangElement *statement = NULL;
  1052. if( fd.features[MFT_RTLighting] )
  1053. {
  1054. // Advanced lighting is the only dynamic lighting supported right now
  1055. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1056. if(inColor != NULL)
  1057. {
  1058. // Find out if RTLighting should be added or substituted
  1059. bool bPreProcessedLighting = false;
  1060. AdvancedLightBinManager *lightBin;
  1061. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1062. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1063. // Lightmap has already been included in the advanced light bin, so
  1064. // no need to do any sampling or anything
  1065. if(bPreProcessedLighting)
  1066. statement = new GenOp( "vec4(@, 1.0)", inColor );
  1067. else
  1068. statement = new GenOp( "tex2D(@, @) + vec4(@.rgb, 0.0)", lightMap, inTex, inColor );
  1069. }
  1070. }
  1071. // If we still don't have it... then just sample the lightmap.
  1072. if ( !statement )
  1073. statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
  1074. // Assign to proper render target
  1075. MultiLine *meta = new MultiLine;
  1076. if( fd.features[MFT_LightbufferMRT] )
  1077. {
  1078. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1079. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1080. }
  1081. else
  1082. meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
  1083. output = meta;
  1084. }
  1085. ShaderFeature::Resources LightmapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1086. {
  1087. Resources res;
  1088. res.numTex = 1;
  1089. res.numTexReg = 1;
  1090. return res;
  1091. }
  1092. void LightmapFeatGLSL::setTexData( Material::StageData &stageDat,
  1093. const MaterialFeatureData &fd,
  1094. RenderPassData &passData,
  1095. U32 &texIndex )
  1096. {
  1097. GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
  1098. passData.mSamplerNames[ texIndex ] = "lightMap";
  1099. if ( tex )
  1100. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1101. else
  1102. passData.mTexType[ texIndex++ ] = Material::Lightmap;
  1103. }
  1104. U32 LightmapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1105. {
  1106. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1107. }
  1108. //****************************************************************************
  1109. // Tonemap
  1110. //****************************************************************************
  1111. void TonemapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1112. const MaterialFeatureData &fd )
  1113. {
  1114. // Grab the connector
  1115. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1116. // Set up the second set of texCoords
  1117. Var *inTex2 = getVertTexCoord( "texCoord2" );
  1118. if ( inTex2 )
  1119. {
  1120. Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
  1121. outTex2->setName( "texCoord2" );
  1122. outTex2->setStructName( "OUT" );
  1123. outTex2->setType( "vec2" );
  1124. output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
  1125. }
  1126. }
  1127. void TonemapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1128. const MaterialFeatureData &fd )
  1129. {
  1130. // Grab connector
  1131. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1132. Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
  1133. inTex2->setName( "texCoord2" );
  1134. inTex2->setStructName( "IN" );
  1135. inTex2->setType( "vec2" );
  1136. // create texture var
  1137. Var *toneMap = new Var;
  1138. toneMap->setType( "sampler2D" );
  1139. toneMap->setName( "toneMap" );
  1140. toneMap->uniform = true;
  1141. toneMap->sampler = true;
  1142. toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1143. MultiLine * meta = new MultiLine;
  1144. // First get the toneMap color
  1145. Var *toneMapColor = new Var;
  1146. toneMapColor->setType( "vec4" );
  1147. toneMapColor->setName( "toneMapColor" );
  1148. LangElement *toneMapColorDecl = new DecOp( toneMapColor );
  1149. meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
  1150. // We do a different calculation if there is a diffuse map or not
  1151. Material::BlendOp blendOp = Material::Mul;
  1152. if ( fd.features[MFT_DiffuseMap] )
  1153. {
  1154. // Reverse the tonemap
  1155. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
  1156. // Re-tonemap with the current color factored in
  1157. blendOp = Material::ToneMap;
  1158. }
  1159. // Find out if RTLighting should be added
  1160. bool bPreProcessedLighting = false;
  1161. AdvancedLightBinManager *lightBin;
  1162. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1163. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1164. // Add in the realtime lighting contribution
  1165. if ( fd.features[MFT_RTLighting] )
  1166. {
  1167. // Right now, only Advanced Lighting is supported
  1168. Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
  1169. if(inColor != NULL)
  1170. {
  1171. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1172. // the dynamic light buffer, and it already has the tonemap included
  1173. if(bPreProcessedLighting)
  1174. meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
  1175. else
  1176. meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
  1177. }
  1178. }
  1179. // Assign to proper render target
  1180. if( fd.features[MFT_LightbufferMRT] )
  1181. {
  1182. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1183. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1184. }
  1185. else
  1186. meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
  1187. output = meta;
  1188. }
  1189. ShaderFeature::Resources TonemapFeatGLSL::getResources( const MaterialFeatureData &fd )
  1190. {
  1191. Resources res;
  1192. res.numTex = 1;
  1193. res.numTexReg = 1;
  1194. return res;
  1195. }
  1196. void TonemapFeatGLSL::setTexData( Material::StageData &stageDat,
  1197. const MaterialFeatureData &fd,
  1198. RenderPassData &passData,
  1199. U32 &texIndex )
  1200. {
  1201. GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
  1202. if ( tex )
  1203. {
  1204. passData.mTexType[ texIndex ] = Material::ToneMapTex;
  1205. passData.mSamplerNames[ texIndex ] = "toneMap";
  1206. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1207. }
  1208. }
  1209. U32 TonemapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1210. {
  1211. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1212. }
  1213. //****************************************************************************
  1214. // pureLIGHT Lighting
  1215. //****************************************************************************
  1216. void VertLitGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1217. const MaterialFeatureData &fd )
  1218. {
  1219. // If we have a lightMap or toneMap then our lighting will be
  1220. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1221. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1222. {
  1223. output = NULL;
  1224. return;
  1225. }
  1226. // Create vertex color connector if it doesn't exist.
  1227. Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1228. if( !outColor )
  1229. {
  1230. // Grab the connector color
  1231. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1232. Var *outColor = connectComp->getElement( RT_COLOR );
  1233. outColor->setName( "vertColor" );
  1234. outColor->setStructName( "OUT" );
  1235. outColor->setType( "vec4" );
  1236. // Search for vert color
  1237. Var *inColor = (Var*) LangElement::find( "diffuse" );
  1238. // If there isn't a vertex color then we can't do anything
  1239. if( !inColor )
  1240. {
  1241. output = NULL;
  1242. return;
  1243. }
  1244. output = new GenOp( " @ = @;\r\n", outColor, inColor );
  1245. }
  1246. else
  1247. output = NULL; // Nothing we need to do.
  1248. }
  1249. void VertLitGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1250. const MaterialFeatureData &fd )
  1251. {
  1252. // If we have a lightMap or toneMap then our lighting will be
  1253. // handled by the MFT_LightMap or MFT_ToneNamp feature instead
  1254. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
  1255. {
  1256. output = NULL;
  1257. return;
  1258. }
  1259. // Grab the connector color register
  1260. Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
  1261. if( !vertColor )
  1262. {
  1263. ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
  1264. AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
  1265. vertColor = connectComp->getElement( RT_COLOR );
  1266. vertColor->setName( "vertColor" );
  1267. vertColor->setStructName( "IN" );
  1268. vertColor->setType( "vec4" );
  1269. }
  1270. MultiLine * meta = new MultiLine;
  1271. // Defaults (no diffuse map)
  1272. Material::BlendOp blendOp = Material::Mul;
  1273. LangElement *outColor = vertColor;
  1274. // We do a different calculation if there is a diffuse map or not
  1275. if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
  1276. {
  1277. Var * finalVertColor = new Var;
  1278. finalVertColor->setName( "finalVertColor" );
  1279. finalVertColor->setType( "vec4" );
  1280. LangElement *finalVertColorDecl = new DecOp( finalVertColor );
  1281. // Reverse the tonemap
  1282. meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
  1283. // Set the blend op to tonemap
  1284. blendOp = Material::ToneMap;
  1285. outColor = finalVertColor;
  1286. }
  1287. // Add in the realtime lighting contribution, if applicable
  1288. if ( fd.features[MFT_RTLighting] )
  1289. {
  1290. Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
  1291. if(rtLightingColor != NULL)
  1292. {
  1293. bool bPreProcessedLighting = false;
  1294. AdvancedLightBinManager *lightBin;
  1295. if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
  1296. bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
  1297. // Assign value in d_lightcolor to toneMapColor if it exists. This is
  1298. // the dynamic light buffer, and it already has the baked-vertex-color
  1299. // included in it
  1300. if(bPreProcessedLighting)
  1301. outColor = new GenOp( "vec4(@.rgb, 1.0)", rtLightingColor );
  1302. else
  1303. outColor = new GenOp( "vec4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
  1304. }
  1305. }
  1306. // Output the color
  1307. if ( fd.features[MFT_LightbufferMRT] )
  1308. {
  1309. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
  1310. meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
  1311. }
  1312. else
  1313. meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
  1314. output = meta;
  1315. }
  1316. U32 VertLitGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
  1317. {
  1318. return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
  1319. }
  1320. //****************************************************************************
  1321. // Detail map
  1322. //****************************************************************************
  1323. void DetailFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1324. const MaterialFeatureData &fd )
  1325. {
  1326. MultiLine *meta = new MultiLine;
  1327. addOutDetailTexCoord( componentList,
  1328. meta,
  1329. fd.features[MFT_TexAnim] );
  1330. output = meta;
  1331. }
  1332. void DetailFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1333. const MaterialFeatureData &fd )
  1334. {
  1335. // Get the detail texture coord.
  1336. Var *inTex = getInTexCoord( "detCoord", "vec2", componentList );
  1337. // create texture var
  1338. Var *detailMap = new Var;
  1339. detailMap->setType( "sampler2D" );
  1340. detailMap->setName( "detailMap" );
  1341. detailMap->uniform = true;
  1342. detailMap->sampler = true;
  1343. detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1344. // We're doing the standard greyscale detail map
  1345. // technique which can darken and lighten the
  1346. // diffuse texture.
  1347. // TODO: We could add a feature to toggle between this
  1348. // and a simple multiplication with the detail map.
  1349. LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
  1350. if ( fd.features[MFT_isDeferred])
  1351. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
  1352. else
  1353. output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
  1354. }
  1355. ShaderFeature::Resources DetailFeatGLSL::getResources( const MaterialFeatureData &fd )
  1356. {
  1357. Resources res;
  1358. res.numTex = 1;
  1359. res.numTexReg = 1;
  1360. return res;
  1361. }
  1362. void DetailFeatGLSL::setTexData( Material::StageData &stageDat,
  1363. const MaterialFeatureData &fd,
  1364. RenderPassData &passData,
  1365. U32 &texIndex )
  1366. {
  1367. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1368. if ( tex )
  1369. {
  1370. passData.mSamplerNames[texIndex] = "detailMap";
  1371. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1372. }
  1373. }
  1374. //****************************************************************************
  1375. // Vertex position
  1376. //****************************************************************************
  1377. void VertPositionGLSL::determineFeature( Material *material,
  1378. const GFXVertexFormat *vertexFormat,
  1379. U32 stageNum,
  1380. const FeatureType &type,
  1381. const FeatureSet &features,
  1382. MaterialFeatureData *outFeatureData )
  1383. {
  1384. // This feature is always on!
  1385. outFeatureData->features.addFeature( type );
  1386. }
  1387. void VertPositionGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1388. const MaterialFeatureData &fd )
  1389. {
  1390. // First check for an input position from a previous feature
  1391. // then look for the default vertex position.
  1392. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  1393. if ( !inPosition )
  1394. inPosition = (Var*)LangElement::find( "position" );
  1395. // grab connector position
  1396. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1397. Var *outPosition = connectComp->getElement( RT_POSITION );
  1398. outPosition->setName( "gl_Position" );
  1399. MultiLine *meta = new MultiLine;
  1400. Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
  1401. meta->addStatement( new GenOp( " @ = tMul(@, vec4(@.xyz,1));\r\n",
  1402. outPosition, modelview, inPosition ) );
  1403. output = meta;
  1404. }
  1405. //****************************************************************************
  1406. // Reflect Cubemap
  1407. //****************************************************************************
  1408. void ReflectCubeFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1409. const MaterialFeatureData &fd )
  1410. {
  1411. // search for vert normal
  1412. Var *inNormal = (Var*) LangElement::find( "normal" );
  1413. if ( !inNormal )
  1414. return;
  1415. MultiLine * meta = new MultiLine;
  1416. // If a base or bump tex is present in the material, but not in the
  1417. // current pass - we need to add one to the current pass to use
  1418. // its alpha channel as a gloss map. Here we just need the tex coords.
  1419. if( !fd.features[MFT_DiffuseMap] &&
  1420. !fd.features[MFT_NormalMap] )
  1421. {
  1422. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1423. fd.materialFeatures[MFT_NormalMap] )
  1424. {
  1425. // find incoming texture var
  1426. Var *inTex = getVertTexCoord( "texCoord" );
  1427. // grab connector texcoord register
  1428. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1429. Var *outTex = connectComp->getElement( RT_TEXCOORD );
  1430. outTex->setName( "texCoord" );
  1431. outTex->setStructName( "OUT" );
  1432. outTex->setType( "vec2" );
  1433. // setup language elements to output incoming tex coords to output
  1434. meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
  1435. }
  1436. }
  1437. // create cubeTrans
  1438. bool useInstancing = fd.features[MFT_UseInstancing];
  1439. Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
  1440. // cube vert position
  1441. Var * cubeVertPos = new Var;
  1442. cubeVertPos->setName( "cubeVertPos" );
  1443. cubeVertPos->setType( "vec3" );
  1444. LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
  1445. meta->addStatement( new GenOp( " @ = tMul( @, float4(@,1)).xyz;\r\n",
  1446. cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
  1447. // cube normal
  1448. Var * cubeNormal = new Var;
  1449. cubeNormal->setName( "cubeNormal" );
  1450. cubeNormal->setType( "vec3" );
  1451. LangElement *cubeNormDecl = new DecOp( cubeNormal );
  1452. meta->addStatement( new GenOp( " @ = ( tMul( (@), vec4(@, 0) ) ).xyz;\r\n",
  1453. cubeNormDecl, cubeTrans, inNormal ) );
  1454. meta->addStatement( new GenOp( " @ = bool(length(@)) ? normalize(@) : @;\r\n",
  1455. cubeNormal, cubeNormal, cubeNormal, cubeNormal ) );
  1456. // grab the eye position
  1457. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1458. if ( !eyePos )
  1459. {
  1460. eyePos = new Var( "eyePosWorld", "vec3" );
  1461. eyePos->uniform = true;
  1462. eyePos->constSortPos = cspPass;
  1463. }
  1464. // eye to vert
  1465. Var * eyeToVert = new Var;
  1466. eyeToVert->setName( "eyeToVert" );
  1467. eyeToVert->setType( "vec3" );
  1468. LangElement *e2vDecl = new DecOp( eyeToVert );
  1469. meta->addStatement( new GenOp( " @ = @ - @;\r\n",
  1470. e2vDecl, cubeVertPos, eyePos ) );
  1471. // grab connector texcoord register
  1472. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1473. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1474. reflectVec->setName( "reflectVec" );
  1475. reflectVec->setStructName( "OUT" );
  1476. reflectVec->setType( "vec3" );
  1477. meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
  1478. output = meta;
  1479. }
  1480. void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1481. const MaterialFeatureData &fd )
  1482. {
  1483. MultiLine * meta = new MultiLine;
  1484. Var *glossColor = NULL;
  1485. // If a base or bump tex is present in the material, but not in the
  1486. // current pass - we need to add one to the current pass to use
  1487. // its alpha channel as a gloss map.
  1488. if( !fd.features[MFT_DiffuseMap] &&
  1489. !fd.features[MFT_NormalMap] )
  1490. {
  1491. if( fd.materialFeatures[MFT_DiffuseMap] ||
  1492. fd.materialFeatures[MFT_NormalMap] )
  1493. {
  1494. // grab connector texcoord register
  1495. Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
  1496. // create texture var
  1497. Var *newMap = new Var;
  1498. newMap->setType( "sampler2D" );
  1499. newMap->setName( "glossMap" );
  1500. newMap->uniform = true;
  1501. newMap->sampler = true;
  1502. newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1503. // create sample color
  1504. Var *color = new Var;
  1505. color->setType( "vec4" );
  1506. color->setName( "diffuseColor" );
  1507. LangElement *colorDecl = new DecOp( color );
  1508. glossColor = color;
  1509. meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
  1510. }
  1511. }
  1512. else
  1513. {
  1514. if (fd.features[MFT_isDeferred])
  1515. glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1516. if (!glossColor)
  1517. glossColor = (Var*)LangElement::find("specularColor");
  1518. if (!glossColor)
  1519. glossColor = (Var*)LangElement::find("diffuseColor");
  1520. if (!glossColor)
  1521. glossColor = (Var*)LangElement::find("bumpNormal");
  1522. }
  1523. // grab connector texcoord register
  1524. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1525. Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
  1526. reflectVec->setName( "reflectVec" );
  1527. reflectVec->setStructName( "IN" );
  1528. reflectVec->setType( "vec3" );
  1529. // create cubemap var
  1530. Var *cubeMap = new Var;
  1531. cubeMap->setType( "samplerCube" );
  1532. cubeMap->setName( "cubeMap" );
  1533. cubeMap->uniform = true;
  1534. cubeMap->sampler = true;
  1535. cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  1536. // TODO: Restore the lighting attenuation here!
  1537. Var *attn = NULL;
  1538. //if ( fd.materialFeatures[MFT_DynamicLight] )
  1539. //attn = (Var*)LangElement::find("attn");
  1540. //else
  1541. if ( fd.materialFeatures[MFT_RTLighting] )
  1542. attn =(Var*)LangElement::find("d_NL_Att");
  1543. LangElement *texCube = NULL;
  1544. Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
  1545. //first try and grab the gbuffer
  1546. if (fd.features[MFT_isDeferred] && matinfo)
  1547. {
  1548. if (fd.features[MFT_DeferredSpecMap])
  1549. texCube = new GenOp("textureLod( @, @, (@.a*5) )", cubeMap, reflectVec, matinfo);
  1550. else
  1551. texCube = new GenOp("textureLod( @, @, (([email protected])*6) )", cubeMap, reflectVec, matinfo);
  1552. }
  1553. else if(glossColor) //failing that, rtry and find color data
  1554. texCube = new GenOp("textureLod( @, @, @.a*5)", cubeMap, reflectVec, glossColor);
  1555. else
  1556. texCube = new GenOp("texture( @, @)", cubeMap, reflectVec);
  1557. LangElement *lerpVal = NULL;
  1558. Material::BlendOp blendOp = Material::LerpAlpha;
  1559. // Note that the lerpVal needs to be a float4 so that
  1560. // it will work with the LerpAlpha blend.
  1561. if (matinfo)
  1562. {
  1563. if (attn)
  1564. lerpVal = new GenOp("@ * saturate( @ )", matinfo, attn);
  1565. else
  1566. lerpVal = new GenOp("@", matinfo);
  1567. }
  1568. else if ( glossColor )
  1569. {
  1570. if ( attn )
  1571. lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
  1572. else
  1573. lerpVal = glossColor;
  1574. }
  1575. else
  1576. {
  1577. if ( attn )
  1578. lerpVal = new GenOp( "vec4( saturate( @ ) ).xxxx", attn );
  1579. else
  1580. blendOp = Material::Mul;
  1581. }
  1582. if (fd.features[MFT_isDeferred])
  1583. {
  1584. Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  1585. meta->addStatement(new GenOp(" @.rgb = lerp( @.rgb, (@).rgb, (@.b));\r\n", targ, targ, texCube, lerpVal));
  1586. }
  1587. else
  1588. meta->addStatement( new GenOp( " @;\r\n", assignColor( texCube, blendOp, lerpVal ) ) );
  1589. output = meta;
  1590. }
  1591. ShaderFeature::Resources ReflectCubeFeatGLSL::getResources( const MaterialFeatureData &fd )
  1592. {
  1593. Resources res;
  1594. if( fd.features[MFT_DiffuseMap] ||
  1595. fd.features[MFT_NormalMap] )
  1596. {
  1597. res.numTex = 1;
  1598. res.numTexReg = 1;
  1599. }
  1600. else
  1601. {
  1602. res.numTex = 2;
  1603. res.numTexReg = 2;
  1604. }
  1605. return res;
  1606. }
  1607. void ReflectCubeFeatGLSL::setTexData( Material::StageData &stageDat,
  1608. const MaterialFeatureData &stageFeatures,
  1609. RenderPassData &passData,
  1610. U32 &texIndex )
  1611. {
  1612. // set up a gloss map if one is not present in the current pass
  1613. // but is present in the current material stage
  1614. if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
  1615. !passData.mFeatureData.features[MFT_NormalMap] )
  1616. {
  1617. GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
  1618. if ( tex && stageFeatures.features[MFT_DiffuseMap] )
  1619. {
  1620. passData.mSamplerNames[ texIndex ] = "diffuseMap";
  1621. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1622. }
  1623. else
  1624. {
  1625. tex = stageDat.getTex( MFT_NormalMap );
  1626. if ( tex && stageFeatures.features[ MFT_NormalMap ] )
  1627. {
  1628. passData.mSamplerNames[ texIndex ] = "bumpMap";
  1629. passData.mTexSlot[ texIndex++ ].texObject = tex;
  1630. }
  1631. }
  1632. }
  1633. if( stageDat.getCubemap() )
  1634. {
  1635. passData.mCubeMap = stageDat.getCubemap();
  1636. passData.mSamplerNames[texIndex] = "cubeMap";
  1637. passData.mTexType[texIndex++] = Material::Cube;
  1638. }
  1639. else
  1640. {
  1641. if( stageFeatures.features[MFT_CubeMap] )
  1642. {
  1643. // assuming here that it is a scenegraph cubemap
  1644. passData.mSamplerNames[texIndex] = "cubeMap";
  1645. passData.mTexType[texIndex++] = Material::SGCube;
  1646. }
  1647. }
  1648. }
  1649. //****************************************************************************
  1650. // RTLighting
  1651. //****************************************************************************
  1652. RTLightingFeatGLSL::RTLightingFeatGLSL()
  1653. : mDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/gl/lighting.glsl" ))
  1654. {
  1655. addDependency( &mDep );
  1656. }
  1657. void RTLightingFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1658. const MaterialFeatureData &fd )
  1659. {
  1660. MultiLine *meta = new MultiLine;
  1661. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1662. // Special case for lighting imposters. We dont have a vert normal and may not
  1663. // have a normal map. Generate and pass the normal data the pixel shader needs.
  1664. if ( fd.features[MFT_ImposterVert] )
  1665. {
  1666. if ( !fd.features[MFT_NormalMap] )
  1667. {
  1668. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1669. if ( !eyePos )
  1670. {
  1671. eyePos = new Var( "eyePosWorld", "vec3" );
  1672. eyePos->uniform = true;
  1673. eyePos->constSortPos = cspPass;
  1674. }
  1675. Var *inPosition = (Var*)LangElement::find( "position" );
  1676. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1677. outNormal->setName( "wsNormal" );
  1678. outNormal->setStructName( "OUT" );
  1679. outNormal->setType( "vec3" );
  1680. // Transform the normal to world space.
  1681. meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );
  1682. }
  1683. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1684. output = meta;
  1685. return;
  1686. }
  1687. // Find the incoming vertex normal.
  1688. Var *inNormal = (Var*)LangElement::find( "normal" );
  1689. // Skip out on realtime lighting if we don't have a normal
  1690. // or we're doing some sort of baked lighting.
  1691. if ( !inNormal ||
  1692. fd.features[MFT_LightMap] ||
  1693. fd.features[MFT_ToneMap] ||
  1694. fd.features[MFT_VertLit] )
  1695. return;
  1696. // If there isn't a normal map then we need to pass
  1697. // the world space normal to the pixel shader ourselves.
  1698. if ( !fd.features[MFT_NormalMap] )
  1699. {
  1700. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  1701. outNormal->setName( "wsNormal" );
  1702. outNormal->setStructName( "OUT" );
  1703. outNormal->setType( "vec3" );
  1704. // Get the transform to world space.
  1705. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  1706. // Transform the normal to world space.
  1707. meta->addStatement( new GenOp( " @ = tMul( @, vec4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  1708. }
  1709. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1710. output = meta;
  1711. }
  1712. void RTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1713. const MaterialFeatureData &fd )
  1714. {
  1715. // Skip out on realtime lighting if we don't have a normal
  1716. // or we're doing some sort of baked lighting.
  1717. //
  1718. // TODO: We can totally detect for this in the material
  1719. // feature setup... we should move it out of here!
  1720. //
  1721. if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
  1722. return;
  1723. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1724. MultiLine *meta = new MultiLine;
  1725. // Look for a wsNormal or grab it from the connector.
  1726. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  1727. if ( !wsNormal )
  1728. {
  1729. wsNormal = connectComp->getElement( RT_TEXCOORD );
  1730. wsNormal->setName( "wsNormal" );
  1731. wsNormal->setStructName( "IN" );
  1732. wsNormal->setType( "vec3" );
  1733. // If we loaded the normal its our responsibility
  1734. // to normalize it... the interpolators won't.
  1735. //
  1736. // Note we cast to half here to get partial precision
  1737. // optimized code which is an acceptable loss of
  1738. // precision for normals and performs much better
  1739. // on older Geforce cards.
  1740. //
  1741. meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
  1742. }
  1743. // Now the wsPosition and wsView.
  1744. Var *wsPosition = getInWsPosition( componentList );
  1745. Var *wsView = getWsView( wsPosition, meta );
  1746. // Create temporaries to hold results of lighting.
  1747. Var *rtShading = new Var( "rtShading", "vec4" );
  1748. Var *specular = new Var( "specular", "vec4" );
  1749. meta->addStatement( new GenOp( " @; @;\r\n",
  1750. new DecOp( rtShading ), new DecOp( specular ) ) );
  1751. // Look for a light mask generated from a previous
  1752. // feature (this is done for BL terrain lightmaps).
  1753. LangElement *lightMask = LangElement::find( "lightMask" );
  1754. if ( !lightMask )
  1755. lightMask = new GenOp( "vec4( 1, 1, 1, 1 )" );
  1756. // Get all the light constants.
  1757. Var *inLightPos = new Var( "inLightPos", "vec4" );
  1758. inLightPos->uniform = true;
  1759. inLightPos->arraySize = 3;
  1760. inLightPos->constSortPos = cspPotentialPrimitive;
  1761. Var *inLightInvRadiusSq = new Var( "inLightInvRadiusSq", "vec4" );
  1762. inLightInvRadiusSq->uniform = true;
  1763. inLightInvRadiusSq->constSortPos = cspPotentialPrimitive;
  1764. Var *inLightColor = new Var( "inLightColor", "vec4" );
  1765. inLightColor->uniform = true;
  1766. inLightColor->arraySize = 4;
  1767. inLightColor->constSortPos = cspPotentialPrimitive;
  1768. Var *inLightSpotDir = new Var( "inLightSpotDir", "vec4" );
  1769. inLightSpotDir->uniform = true;
  1770. inLightSpotDir->arraySize = 3;
  1771. inLightSpotDir->constSortPos = cspPotentialPrimitive;
  1772. Var *inLightSpotAngle = new Var( "inLightSpotAngle", "vec4" );
  1773. inLightSpotAngle->uniform = true;
  1774. inLightSpotAngle->constSortPos = cspPotentialPrimitive;
  1775. Var *lightSpotFalloff = new Var( "inLightSpotFalloff", "vec4" );
  1776. lightSpotFalloff->uniform = true;
  1777. lightSpotFalloff->constSortPos = cspPotentialPrimitive;
  1778. Var *specularPower = new Var( "specularPower", "float" );
  1779. specularPower->uniform = true;
  1780. specularPower->constSortPos = cspPotentialPrimitive;
  1781. Var *specularColor = (Var*)LangElement::find( "specularColor" );
  1782. if ( !specularColor )
  1783. {
  1784. specularColor = new Var( "specularColor", "vec4" );
  1785. specularColor->uniform = true;
  1786. specularColor->constSortPos = cspPotentialPrimitive;
  1787. }
  1788. Var *ambient = new Var( "ambient", "vec4" );
  1789. ambient->uniform = true;
  1790. ambient->constSortPos = cspPass;
  1791. // Calculate the diffuse shading and specular powers.
  1792. meta->addStatement( new GenOp( " compute4Lights( @, @, @, @,\r\n"
  1793. " @, @, @, @, @, @, @, @,\r\n"
  1794. " @, @ );\r\n",
  1795. wsView, wsPosition, wsNormal, lightMask,
  1796. inLightPos, inLightInvRadiusSq, inLightColor, inLightSpotDir, inLightSpotAngle, lightSpotFalloff, specularPower, specularColor,
  1797. rtShading, specular ) );
  1798. // Apply the lighting to the diffuse color.
  1799. LangElement *lighting = new GenOp( "vec4( @.rgb + @.rgb, 1 )", rtShading, ambient );
  1800. meta->addStatement( new GenOp( " @;\r\n", assignColor( lighting, Material::Mul ) ) );
  1801. output = meta;
  1802. }
  1803. ShaderFeature::Resources RTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
  1804. {
  1805. Resources res;
  1806. // These features disable realtime lighting.
  1807. if ( !fd.features[MFT_LightMap] &&
  1808. !fd.features[MFT_ToneMap] &&
  1809. !fd.features[MFT_VertLit] )
  1810. {
  1811. // If enabled we pass the position.
  1812. res.numTexReg = 1;
  1813. // If there isn't a bump map then we pass the
  1814. // world space normal as well.
  1815. if ( !fd.features[MFT_NormalMap] )
  1816. res.numTexReg++;
  1817. }
  1818. return res;
  1819. }
  1820. //****************************************************************************
  1821. // Fog
  1822. //****************************************************************************
  1823. FogFeatGLSL::FogFeatGLSL()
  1824. : mFogDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/gl/torque.glsl" ))
  1825. {
  1826. addDependency( &mFogDep );
  1827. }
  1828. void FogFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1829. const MaterialFeatureData &fd )
  1830. {
  1831. MultiLine *meta = new MultiLine;
  1832. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1833. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1834. {
  1835. // Grab the eye position.
  1836. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1837. if ( !eyePos )
  1838. {
  1839. eyePos = new Var( "eyePosWorld", "vec3" );
  1840. eyePos->uniform = true;
  1841. eyePos->constSortPos = cspPass;
  1842. }
  1843. Var *fogData = new Var( "fogData", "vec3" );
  1844. fogData->uniform = true;
  1845. fogData->constSortPos = cspPass;
  1846. Var *wsPosition = new Var( "fogPos", "vec3" );
  1847. getWsPosition( componentList,
  1848. fd.features[MFT_UseInstancing],
  1849. meta,
  1850. new DecOp( wsPosition ) );
  1851. // We pass the fog amount to the pixel shader.
  1852. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1853. Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
  1854. fogAmount->setName( "fogAmount" );
  1855. fogAmount->setStructName( "OUT" );
  1856. fogAmount->setType( "float" );
  1857. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1858. fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
  1859. }
  1860. else
  1861. {
  1862. // We fog in world space... make sure the world space
  1863. // position is passed to the pixel shader. This is
  1864. // often already passed for lighting, so it takes up
  1865. // no extra output registers.
  1866. addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
  1867. }
  1868. output = meta;
  1869. }
  1870. void FogFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1871. const MaterialFeatureData &fd )
  1872. {
  1873. MultiLine *meta = new MultiLine;
  1874. Var *fogColor = new Var;
  1875. fogColor->setType( "vec4" );
  1876. fogColor->setName( "fogColor" );
  1877. fogColor->uniform = true;
  1878. fogColor->constSortPos = cspPass;
  1879. // Get the out color.
  1880. Var *color = (Var*) LangElement::find( "col" );
  1881. if ( !color )
  1882. {
  1883. color = new Var;
  1884. color->setType( "vec4" );
  1885. color->setName( "col" );
  1886. color->setStructName("OUT");
  1887. }
  1888. Var *fogAmount;
  1889. const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
  1890. if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
  1891. {
  1892. // Per-vertex.... just get the fog amount.
  1893. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1894. fogAmount = connectComp->getElement( RT_TEXCOORD );
  1895. fogAmount->setName( "fogAmount" );
  1896. fogAmount->setStructName( "IN" );
  1897. fogAmount->setType( "float" );
  1898. }
  1899. else
  1900. {
  1901. Var *wsPosition = getInWsPosition( componentList );
  1902. // grab the eye position
  1903. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  1904. if ( !eyePos )
  1905. {
  1906. eyePos = new Var( "eyePosWorld", "vec3" );
  1907. eyePos->uniform = true;
  1908. eyePos->constSortPos = cspPass;
  1909. }
  1910. Var *fogData = new Var( "fogData", "vec3" );
  1911. fogData->uniform = true;
  1912. fogData->constSortPos = cspPass;
  1913. /// Get the fog amount.
  1914. fogAmount = new Var( "fogAmount", "float" );
  1915. meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
  1916. new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
  1917. }
  1918. // Lerp between the fog color and diffuse color.
  1919. LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
  1920. meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
  1921. output = meta;
  1922. }
  1923. ShaderFeature::Resources FogFeatGLSL::getResources( const MaterialFeatureData &fd )
  1924. {
  1925. Resources res;
  1926. res.numTexReg = 1;
  1927. return res;
  1928. }
  1929. //****************************************************************************
  1930. // Visibility
  1931. //****************************************************************************
  1932. VisibilityFeatGLSL::VisibilityFeatGLSL()
  1933. : mTorqueDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/gl/torque.glsl" ))
  1934. {
  1935. addDependency( &mTorqueDep );
  1936. }
  1937. void VisibilityFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  1938. const MaterialFeatureData &fd )
  1939. {
  1940. MultiLine *meta = new MultiLine;
  1941. output = meta;
  1942. if ( fd.features[ MFT_UseInstancing ] )
  1943. {
  1944. // We pass the visibility to the pixel shader via
  1945. // another output register.
  1946. //
  1947. // TODO: We should see if we can share this register
  1948. // with some other common instanced data.
  1949. //
  1950. ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  1951. Var *outVisibility = conn->getElement( RT_TEXCOORD );
  1952. outVisibility->setStructName( "OUT" );
  1953. outVisibility->setName( "visibility" );
  1954. outVisibility->setType( "float" );
  1955. ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
  1956. Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
  1957. instVisibility->setStructName( "IN" );
  1958. instVisibility->setName( "inst_visibility" );
  1959. instVisibility->setType( "float" );
  1960. mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
  1961. meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
  1962. }
  1963. if ( fd.features[ MFT_IsTranslucent ] )
  1964. return;
  1965. addOutVpos( meta, componentList );
  1966. }
  1967. void VisibilityFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  1968. const MaterialFeatureData &fd )
  1969. {
  1970. // Get the visibility constant.
  1971. Var *visibility = NULL;
  1972. if ( fd.features[ MFT_UseInstancing ] )
  1973. visibility = getInTexCoord( "visibility", "float", componentList );
  1974. else
  1975. {
  1976. visibility = (Var*)LangElement::find( "visibility" );
  1977. if ( !visibility )
  1978. {
  1979. visibility = new Var();
  1980. visibility->setType( "float" );
  1981. visibility->setName( "visibility" );
  1982. visibility->uniform = true;
  1983. visibility->constSortPos = cspPotentialPrimitive;
  1984. }
  1985. }
  1986. MultiLine* meta = new MultiLine;
  1987. output = meta;
  1988. // Translucent objects do a simple alpha fade.
  1989. if ( fd.features[ MFT_IsTranslucent ] )
  1990. {
  1991. Var *color = (Var*) LangElement::find( "col" );
  1992. meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
  1993. return;
  1994. }
  1995. // Everything else does a fizzle.
  1996. Var *vPos = getInVpos( meta, componentList );
  1997. meta->addStatement( new GenOp( " fizzle( @, @ );\r\n", vPos, visibility ) );
  1998. }
  1999. ShaderFeature::Resources VisibilityFeatGLSL::getResources( const MaterialFeatureData &fd )
  2000. {
  2001. Resources res;
  2002. // TODO: Fix for instancing.
  2003. if ( !fd.features[ MFT_IsTranslucent ] )
  2004. res.numTexReg = 1;
  2005. return res;
  2006. }
  2007. //****************************************************************************
  2008. // AlphaTest
  2009. //****************************************************************************
  2010. void AlphaTestGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2011. const MaterialFeatureData &fd )
  2012. {
  2013. // If we're below SM3 and don't have a depth output
  2014. // feature then don't waste an instruction here.
  2015. if ( GFX->getPixelShaderVersion() < 3.0 &&
  2016. !fd.features[ MFT_EyeSpaceDepthOut ] &&
  2017. !fd.features[ MFT_DepthOut ] )
  2018. {
  2019. output = NULL;
  2020. return;
  2021. }
  2022. // If we don't have a color var then we cannot do an alpha test.
  2023. Var *color = (Var*)LangElement::find( "col1" );
  2024. if ( !color )
  2025. color = (Var*)LangElement::find("col");
  2026. if ( !color )
  2027. {
  2028. output = NULL;
  2029. return;
  2030. }
  2031. // Now grab the alpha test value.
  2032. Var *alphaTestVal = new Var;
  2033. alphaTestVal->setType( "float" );
  2034. alphaTestVal->setName( "alphaTestValue" );
  2035. alphaTestVal->uniform = true;
  2036. alphaTestVal->constSortPos = cspPotentialPrimitive;
  2037. // Do the clip.
  2038. output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
  2039. }
  2040. //****************************************************************************
  2041. // GlowMask
  2042. //****************************************************************************
  2043. void GlowMaskGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2044. const MaterialFeatureData &fd )
  2045. {
  2046. output = NULL;
  2047. // Get the output color... and make it black to mask out
  2048. // glow passes rendered before us.
  2049. //
  2050. // The shader compiler will optimize out all the other
  2051. // code above that doesn't contribute to the alpha mask.
  2052. Var *color = (Var*)LangElement::find( "col" );
  2053. if ( color )
  2054. output = new GenOp( " @.rgb = vec3(0);\r\n", color );
  2055. }
  2056. //****************************************************************************
  2057. // RenderTargetZero
  2058. //****************************************************************************
  2059. void RenderTargetZeroGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  2060. {
  2061. // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
  2062. // This will prevent a divide by zero causing an FP special on float render targets
  2063. output = new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(0.00001)" ), Material::None, NULL, mOutputTargetMask ) );
  2064. }
  2065. //****************************************************************************
  2066. // HDR Output
  2067. //****************************************************************************
  2068. HDROutGLSL::HDROutGLSL()
  2069. : mTorqueDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/gl/torque.glsl" ))
  2070. {
  2071. addDependency( &mTorqueDep );
  2072. }
  2073. void HDROutGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2074. const MaterialFeatureData &fd )
  2075. {
  2076. // Let the helper function do the work.
  2077. Var *color = (Var*)LangElement::find( "col" );
  2078. if ( color )
  2079. output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
  2080. }
  2081. //****************************************************************************
  2082. // FoliageFeatureGLSL
  2083. //****************************************************************************
  2084. #include "T3D/fx/groundCover.h"
  2085. FoliageFeatureGLSL::FoliageFeatureGLSL()
  2086. : mDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/gl/foliage.glsl" ))
  2087. {
  2088. addDependency( &mDep );
  2089. }
  2090. void FoliageFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2091. const MaterialFeatureData &fd )
  2092. {
  2093. // Get the input variables we need.
  2094. Var *inPosition = (Var*)LangElement::find( "inPosition" );
  2095. if ( !inPosition )
  2096. inPosition = (Var*)LangElement::find( "position" );
  2097. Var *inColor = (Var*)LangElement::find( "diffuse" );
  2098. Var *inParams = (Var*)LangElement::find( "texCoord" );
  2099. MultiLine *meta = new MultiLine;
  2100. // Declare the normal and tangent variables since they do not exist
  2101. // in this vert type, but we do need to set them up for others.
  2102. Var *normal = (Var*)LangElement::find( "normal" );
  2103. AssertFatal( normal, "FoliageFeatureGLSL requires vert normal!" );
  2104. Var *tangent = new Var;
  2105. tangent->setType( "vec3" );
  2106. tangent->setName( "T" );
  2107. LangElement *tangentDec = new DecOp( tangent );
  2108. meta->addStatement( new GenOp( " @;\n", tangentDec ) );
  2109. // We add a float foliageFade to the OUT structure.
  2110. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2111. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2112. fade->setName( "foliageFade" );
  2113. fade->setStructName( "OUT" );
  2114. fade->setType( "float" );
  2115. // grab the eye position
  2116. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2117. if ( !eyePos )
  2118. {
  2119. eyePos = new Var( "eyePosWorld", "vec3" );
  2120. eyePos->uniform = true;
  2121. eyePos->constSortPos = cspPass;
  2122. }
  2123. // All actual work is offloaded to this method.
  2124. meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
  2125. // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
  2126. meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
  2127. output = meta;
  2128. }
  2129. void FoliageFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2130. const MaterialFeatureData &fd )
  2131. {
  2132. // Find / create IN.foliageFade
  2133. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2134. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2135. fade->setName( "foliageFade" );
  2136. fade->setStructName( "IN" );
  2137. fade->setType( "float" );
  2138. // Find / create visibility
  2139. Var *visibility = (Var*) LangElement::find( "visibility" );
  2140. if ( !visibility )
  2141. {
  2142. visibility = new Var();
  2143. visibility->setType( "float" );
  2144. visibility->setName( "visibility" );
  2145. visibility->uniform = true;
  2146. visibility->constSortPos = cspPotentialPrimitive;
  2147. }
  2148. MultiLine *meta = new MultiLine;
  2149. // Multiply foliageFade into visibility.
  2150. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2151. output = meta;
  2152. }
  2153. void FoliageFeatureGLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
  2154. {
  2155. // This isn't really necessary since the outFeatureData will be filtered after
  2156. // this call.
  2157. if ( features.hasFeature( MFT_Foliage ) )
  2158. outFeatureData->features.addFeature( type );
  2159. }
  2160. ShaderFeatureConstHandles* FoliageFeatureGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  2161. {
  2162. GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
  2163. AssertFatal( gcover != NULL, "FoliageFeatureGLSL::createConstHandles - userObject was not valid!" );
  2164. GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
  2165. handles->mGroundCover = gcover;
  2166. handles->init( shader );
  2167. return handles;
  2168. }
  2169. void ParticleNormalFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
  2170. {
  2171. MultiLine *meta = new MultiLine;
  2172. output = meta;
  2173. // Calculate normal and tangent values since we want to keep particle verts
  2174. // as light-weight as possible
  2175. Var *normal = (Var*) LangElement::find("normal");
  2176. if(normal == NULL)
  2177. {
  2178. normal = new Var;
  2179. normal->setType( "vec3" );
  2180. normal->setName( "normal" );
  2181. // These values are not accidental. It is slightly adjusted from facing straight into the
  2182. // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
  2183. // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
  2184. // [11/23/2009 Pat]
  2185. meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
  2186. }
  2187. Var *T = (Var*) LangElement::find( "T" );
  2188. if(T == NULL)
  2189. {
  2190. T = new Var;
  2191. T->setType( "vec3" );
  2192. T->setName( "T" );
  2193. meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
  2194. }
  2195. }
  2196. //****************************************************************************
  2197. // ImposterVertFeatureGLSL
  2198. //****************************************************************************
  2199. ImposterVertFeatureGLSL::ImposterVertFeatureGLSL()
  2200. : mDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/gl/imposter.glsl" ))
  2201. {
  2202. addDependency( &mDep );
  2203. }
  2204. void ImposterVertFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
  2205. const MaterialFeatureData &fd )
  2206. {
  2207. MultiLine *meta = new MultiLine;
  2208. output = meta;
  2209. // Get the input vertex variables.
  2210. Var *inPosition = (Var*)LangElement::find( "position" );
  2211. Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
  2212. Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
  2213. Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
  2214. // Get the input shader constants.
  2215. Var *imposterLimits = new Var;
  2216. imposterLimits->setType( "vec4" );
  2217. imposterLimits->setName( "imposterLimits" );
  2218. imposterLimits->uniform = true;
  2219. imposterLimits->constSortPos = cspPotentialPrimitive;
  2220. Var *imposterUVs = new Var;
  2221. imposterUVs->setType( "vec4" );
  2222. imposterUVs->setName( "imposterUVs" );
  2223. imposterUVs->arraySize = 64; // See imposter.glsl
  2224. imposterUVs->uniform = true;
  2225. imposterUVs->constSortPos = cspPotentialPrimitive;
  2226. Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
  2227. if ( !eyePos )
  2228. {
  2229. eyePos = new Var( "eyePosWorld", "vec3" );
  2230. eyePos->uniform = true;
  2231. eyePos->constSortPos = cspPass;
  2232. }
  2233. // Declare the outputs from this feature.
  2234. Var *outInPosition = new Var;
  2235. outInPosition->setType( "vec3" );
  2236. outInPosition->setName( "inPosition" );
  2237. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
  2238. Var *outTexCoord = new Var;
  2239. outTexCoord->setType( "vec2" );
  2240. outTexCoord->setName( "texCoord" );
  2241. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
  2242. Var *outWorldToTangent = new Var;
  2243. outWorldToTangent->setType( "float3x3" );
  2244. outWorldToTangent->setName( "worldToTangent" );
  2245. meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
  2246. // Add imposterFade to the OUT structure.
  2247. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2248. Var *outFade = connectComp->getElement( RT_TEXCOORD );
  2249. outFade->setName( "imposterFade" );
  2250. outFade->setStructName( "OUT" );
  2251. outFade->setType( "float" );
  2252. // Assign OUT.imposterFade
  2253. meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
  2254. // All actual work is done in this method.
  2255. meta->addStatement( new GenOp( " imposter_v( @.xyz, int(@.w), @.x * length(@), normalize(@), normalize(@), int(@.y), int(@.x), @.z, bool(@.w), @, @, @, @, @ );\r\n",
  2256. inPosition,
  2257. inPosition,
  2258. inMiscParams,
  2259. inRightVec,
  2260. inUpVec,
  2261. inRightVec,
  2262. imposterLimits,
  2263. imposterLimits,
  2264. imposterLimits,
  2265. imposterLimits,
  2266. eyePos,
  2267. imposterUVs,
  2268. outInPosition,
  2269. outTexCoord,
  2270. outWorldToTangent ) );
  2271. // Copy the position to wsPosition for use in shaders
  2272. // down stream instead of looking for objTrans.
  2273. Var *wsPosition = new Var;
  2274. wsPosition->setType( "vec3" );
  2275. wsPosition->setName( "wsPosition" );
  2276. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
  2277. // If we new viewToTangent... its the same as the
  2278. // world to tangent for an imposter.
  2279. Var *viewToTangent = new Var;
  2280. viewToTangent->setType( "float3x3" );
  2281. viewToTangent->setName( "viewToTangent" );
  2282. meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
  2283. }
  2284. void ImposterVertFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
  2285. const MaterialFeatureData &fd )
  2286. {
  2287. // Find / create IN.imposterFade
  2288. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  2289. Var *fade = connectComp->getElement( RT_TEXCOORD );
  2290. fade->setName( "imposterFade" );
  2291. fade->setStructName( "IN" );
  2292. fade->setType( "float" );
  2293. // Find / create visibility
  2294. Var *visibility = (Var*) LangElement::find( "visibility" );
  2295. if ( !visibility )
  2296. {
  2297. visibility = new Var();
  2298. visibility->setType( "float" );
  2299. visibility->setName( "visibility" );
  2300. visibility->uniform = true;
  2301. visibility->constSortPos = cspPotentialPrimitive;
  2302. }
  2303. MultiLine *meta = new MultiLine;
  2304. // Multiply foliageFade into visibility.
  2305. meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
  2306. output = meta;
  2307. }
  2308. void ImposterVertFeatureGLSL::determineFeature( Material *material,
  2309. const GFXVertexFormat *vertexFormat,
  2310. U32 stageNum,
  2311. const FeatureType &type,
  2312. const FeatureSet &features,
  2313. MaterialFeatureData *outFeatureData )
  2314. {
  2315. if ( features.hasFeature( MFT_ImposterVert ) )
  2316. outFeatureData->features.addFeature( MFT_ImposterVert );
  2317. }
  2318. //****************************************************************************
  2319. // HardwareSkinningFeatureGLSL
  2320. //****************************************************************************
  2321. void HardwareSkinningFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList,
  2322. const MaterialFeatureData &fd)
  2323. {
  2324. MultiLine *meta = new MultiLine;
  2325. Var *inPosition = (Var*)LangElement::find("inPosition");
  2326. Var *inNormal = (Var*)LangElement::find("inNormal");
  2327. if (!inPosition)
  2328. inPosition = (Var*)LangElement::find("position");
  2329. if (!inNormal)
  2330. inNormal = (Var*)LangElement::find("normal");
  2331. Var* posePos = new Var("posePos", "vec3");
  2332. Var* poseNormal = new Var("poseNormal", "vec3");
  2333. Var* poseMat = new Var("poseMat", "mat4x3");
  2334. Var* poseRotMat = new Var("poseRotMat", "mat3x3");
  2335. Var* nodeTransforms = (Var*)LangElement::find("nodeTransforms");
  2336. if (!nodeTransforms)
  2337. {
  2338. nodeTransforms = new Var("nodeTransforms", "mat4x3");
  2339. nodeTransforms->uniform = true;
  2340. nodeTransforms->arraySize = TSShape::smMaxSkinBones;
  2341. nodeTransforms->constSortPos = cspPrimitive;
  2342. }
  2343. U32 numIndices = mVertexFormat->getNumBlendIndices();
  2344. meta->addStatement(new GenOp(" @ = vec3(0.0);\r\n", new DecOp(posePos)));
  2345. meta->addStatement(new GenOp(" @ = vec3(0.0);\r\n", new DecOp(poseNormal)));
  2346. meta->addStatement(new GenOp(" @;\r\n", new DecOp(poseMat)));
  2347. meta->addStatement(new GenOp(" @;\r\n int i;\r\n", new DecOp(poseRotMat)));
  2348. for (U32 i = 0; i<numIndices; i++)
  2349. {
  2350. // NOTE: To keep things simple, we assume all 4 bone indices are used in each element chunk.
  2351. LangElement* inIndices = (Var*)LangElement::find(String::ToString("vBlendIndex%d", i));
  2352. LangElement* inWeights = (Var*)LangElement::find(String::ToString("vBlendWeight%d", i));
  2353. AssertFatal(inIndices && inWeights, "Something went wrong here");
  2354. AssertFatal(poseMat && nodeTransforms && posePos && inPosition && inWeights && poseNormal && inNormal && poseRotMat, "Something went REALLY wrong here");
  2355. meta->addStatement(new GenOp(" for (i=0; i<4; i++) {\r\n"));
  2356. meta->addStatement(new GenOp(" int poseIdx = int(@[i]);\r\n", inIndices));
  2357. meta->addStatement(new GenOp(" float poseWeight = @[i];\r\n", inWeights));
  2358. meta->addStatement(new GenOp(" @ = @[poseIdx];\r\n", poseMat, nodeTransforms));
  2359. meta->addStatement(new GenOp(" @ = mat3x3(@);\r\n", poseRotMat, poseMat));
  2360. meta->addStatement(new GenOp(" @ += (@ * vec4(@, 1)).xyz * poseWeight;\r\n", posePos, poseMat, inPosition));
  2361. meta->addStatement(new GenOp(" @ += ((@ * @) * poseWeight);\r\n", poseNormal, poseRotMat, inNormal));
  2362. meta->addStatement(new GenOp(" }\r\n"));
  2363. }
  2364. // Assign new position and normal
  2365. meta->addStatement(new GenOp(" @ = @;\r\n", inPosition, posePos));
  2366. meta->addStatement(new GenOp(" @ = normalize(@);\r\n", inNormal, poseNormal));
  2367. output = meta;
  2368. }