12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "shaderGen/GLSL/shaderFeatureGLSL.h"
- #include "shaderGen/langElement.h"
- #include "shaderGen/shaderOp.h"
- #include "shaderGen/shaderGenVars.h"
- #include "gfx/gfxDevice.h"
- #include "materials/matInstance.h"
- #include "materials/processedMaterial.h"
- #include "materials/materialFeatureTypes.h"
- #include "core/util/autoPtr.h"
- #include "lighting/advanced/advancedLightBinManager.h"
- #include "ts/tsShape.h"
- #include "shaderGen/shaderGen.h"
- LangElement * ShaderFeatureGLSL::setupTexSpaceMat( Vector<ShaderComponent*> &, // componentList
- Var **texSpaceMat )
- {
- Var *N = (Var*) LangElement::find( "normal" );
- Var *B = (Var*) LangElement::find( "B" );
- Var *T = (Var*) LangElement::find( "T" );
-
- Var *tangentW = (Var*) LangElement::find( "tangentW" );
-
- // setup matrix var
- *texSpaceMat = new Var;
- (*texSpaceMat)->setType( "float3x3" );
- (*texSpaceMat)->setName( "objToTangentSpace" );
- MultiLine * meta = new MultiLine;
- meta->addStatement( new GenOp( " @ = float3x3(1,0,0, 0,1,0, 0,0,1);\r\n", new DecOp( *texSpaceMat ) ) );
-
- // Protect against missing normal and tangent.
- if ( !N || !T )
- {
- meta->addStatement( new GenOp( " tSetMatrixRow(@, 0, float3( 1, 0, 0 )); tSetMatrixRow(@, 1,float3( 0, 1, 0 )); tSetMatrixRow(@,2, float3( 0, 0, 1 ));\r\n",
- *texSpaceMat, *texSpaceMat, *texSpaceMat ) );
- return meta;
- }
- meta->addStatement( new GenOp( " tSetMatrixRow(@, 0, @);\r\n", *texSpaceMat, T ) );
- if ( B )
- meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, @);\r\n", *texSpaceMat, B ) );
- else
- {
- if(dStricmp((char*)T->type, "vec4") == 0)
- meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, cross( @, normalize(@) ) * @.w);\r\n", *texSpaceMat, T, N, T ) );
- else if(tangentW)
- meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, cross( @, normalize(@) ) * @);\r\n", *texSpaceMat, T, N, tangentW ) );
- else
- meta->addStatement( new GenOp( " tSetMatrixRow(@, 1, cross( @, normalize(@) ));\r\n", *texSpaceMat, T, N ) );
- }
- meta->addStatement( new GenOp( " tSetMatrixRow(@, 2, normalize(@));\r\n", *texSpaceMat, N ) );
- return meta;
- }
- LangElement* ShaderFeatureGLSL::assignColor( LangElement *elem,
- Material::BlendOp blend,
- LangElement *lerpElem,
- ShaderFeature::OutputTarget outputTarget )
- {
- // search for color var
- Var *color = (Var*) LangElement::find( getOutputTargetVarName(outputTarget) );
- if ( !color )
- {
- // create color var
- color = new Var;
- color->setType( "vec4" );
- color->setName( getOutputTargetVarName( outputTarget ) );
- color->setStructName( "OUT" );
- return new GenOp( "@ = @", color, elem );
- }
- LangElement *assign;
- switch ( blend )
- {
- case Material::Add:
- assign = new GenOp( "@ += @", color, elem );
- break;
- case Material::Sub:
- assign = new GenOp( "@ -= @", color, elem );
- break;
- case Material::Mul:
- assign = new GenOp( "@ *= @", color, elem );
- break;
- case Material::PreMul:
- assign = new GenOp("@.rgb = @.rgb + (@.rgb*([email protected]))", color, elem, color, elem);
- break;
- case Material::AddAlpha:
- assign = new GenOp( "@ += @ * @.a", color, elem, elem );
- break;
- case Material::LerpAlpha:
- if ( !lerpElem )
- lerpElem = elem;
- assign = new GenOp( "@.rgb = lerp( @.rgb, (@).rgb, (@).a )", color, color, elem, lerpElem );
- break;
-
- case Material::ToneMap:
- assign = new GenOp( "@ = 1.0 - exp(-1.0 * @ * @)", color, color, elem );
- break;
-
- default:
- AssertFatal(false, "Unrecognized color blendOp");
- // Fallthru
- case Material::None:
- assign = new GenOp( "@ = @", color, elem );
- break;
- }
-
- return assign;
- }
- LangElement *ShaderFeatureGLSL::expandNormalMap( LangElement *sampleNormalOp,
- LangElement *normalDecl,
- LangElement *normalVar,
- const MaterialFeatureData &fd )
- {
- MultiLine *meta = new MultiLine;
- const bool hasBc3 = fd.features.hasFeature(MFT_IsBC3nm, getProcessIndex());
- const bool hasBc5 = fd.features.hasFeature(MFT_IsBC5nm, getProcessIndex());
- if (hasBc3 || hasBc5)
- {
- if ( fd.features[MFT_ImposterVert] )
- {
- // The imposter system uses object space normals and
- // encodes them with the z axis in the alpha component.
- meta->addStatement( new GenOp( " @ = float4( normalize( @.xyw * 2.0 - 1.0 ), 0.0 ); // Obj DXTnm\r\n", normalDecl, sampleNormalOp ) );
- }
- else if (hasBc3)
- {
- // BC3 Swizzle trick
- meta->addStatement( new GenOp( " @ = float4( @.ag * 2.0 - 1.0, 0.0, 0.0 ); // DXTnm\r\n", normalDecl, sampleNormalOp ) );
- meta->addStatement( new GenOp( " @.z = sqrt( 1.0 - dot( @.xy, @.xy ) ); // DXTnm\r\n", normalVar, normalVar, normalVar ) );
- }
- else if (hasBc5)
- {
- // BC5
- meta->addStatement(new GenOp(" @ = float4( @.gr * 2.0 - 1.0, 0.0, 0.0 ); // bc5nm\r\n", normalDecl, sampleNormalOp ) );
- meta->addStatement(new GenOp(" @.z = sqrt( 1.0 - dot( @.xy, @.xy ) ); // bc5nm\r\n", normalVar, normalVar, normalVar ) );
- }
- }
- else
- {
- meta->addStatement( new GenOp( " @ = @;\r\n", normalDecl, sampleNormalOp ) );
- meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 1.0;\r\n", normalVar, normalVar ) );
- }
- return meta;
- }
- ShaderFeatureGLSL::ShaderFeatureGLSL()
- {
- output = NULL;
- }
- Var * ShaderFeatureGLSL::getVertTexCoord( const String &name )
- {
- Var *inTex = NULL;
- for( U32 i=0; i<LangElement::elementList.size(); i++ )
- {
- if( !String::compare( (char*)LangElement::elementList[i]->name, name.c_str() ) )
- {
- inTex = dynamic_cast<Var*>( LangElement::elementList[i] );
- if ( inTex )
- {
- // NOTE: This used to do this check...
- //
- // String::compare( (char*)inTex->structName, "IN" )
- //
- // ... to ensure that the var was from the input
- // vertex structure, but this kept some features
- // ( ie. imposter vert ) from decoding their own
- // coords for other features to use.
- //
- // If we run into issues with collisions between
- // IN vars and local vars we may need to revise.
-
- break;
- }
- }
- }
- return inTex;
- }
- Var* ShaderFeatureGLSL::getOutObjToTangentSpace( Vector<ShaderComponent*> &componentList,
- MultiLine *meta,
- const MaterialFeatureData &fd )
- {
- Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
- if ( !outObjToTangentSpace )
- meta->addStatement( setupTexSpaceMat( componentList, &outObjToTangentSpace ) );
- return outObjToTangentSpace;
- }
- Var* ShaderFeatureGLSL::getOutWorldToTangent( Vector<ShaderComponent*> &componentList,
- MultiLine *meta,
- const MaterialFeatureData &fd )
- {
- Var *outWorldToTangent = (Var*)LangElement::find( "outWorldToTangent" );
- if ( outWorldToTangent )
- return outWorldToTangent;
-
- Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
- if ( !worldToTangent )
- {
- Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
-
- if(!fd.features[MFT_ParticleNormal])
- {
- // turn obj->tangent into world->tangent
- worldToTangent = new Var;
- worldToTangent->setType( "float3x3" );
- worldToTangent->setName( "worldToTangent" );
- LangElement *worldToTangentDecl = new DecOp( worldToTangent );
-
- // Get the world->obj transform
- Var *worldToObj = (Var*)LangElement::find( "worldToObj" );
- if ( !worldToObj )
- {
- worldToObj = new Var;
- worldToObj->setName( "worldToObj" );
- if ( fd.features[MFT_UseInstancing] )
- {
- // We just use transpose to convert the 3x3 portion of
- // the object transform to its inverse.
- worldToObj->setType( "float3x3" );
- Var *objTrans = getObjTrans( componentList, true, meta );
- meta->addStatement( new GenOp( " @ = transpose( float3x3(@) ); // Instancing!\r\n", new DecOp( worldToObj ), objTrans ) );
- }
- else
- {
- worldToObj->setType( "float4x4" );
- worldToObj->uniform = true;
- worldToObj->constSortPos = cspPrimitive;
- }
- }
-
- // assign world->tangent transform
- meta->addStatement( new GenOp( " @ = tMul( @, float3x3(@) );\r\n", worldToTangentDecl, texSpaceMat, worldToObj ) );
- }
- else
- {
- // Assume particle normal generation has set this up in the proper space
- worldToTangent = texSpaceMat;
- }
- }
-
- // send transform to pixel shader
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-
- outWorldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
- outWorldToTangent->setName( "outWorldToTangent" );
- outWorldToTangent->setStructName( "OUT" );
- outWorldToTangent->setType( "float3x3" );
- meta->addStatement( new GenOp( " @ = @;\r\n", outWorldToTangent, worldToTangent ) );
-
- return outWorldToTangent;
- }
- Var* ShaderFeatureGLSL::getOutViewToTangent( Vector<ShaderComponent*> &componentList,
- MultiLine *meta,
- const MaterialFeatureData &fd )
- {
- Var *outViewToTangent = (Var*)LangElement::find( "outViewToTangent" );
- if ( outViewToTangent )
- return outViewToTangent;
-
- Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
- if ( !viewToTangent )
- {
- Var *texSpaceMat = getOutObjToTangentSpace( componentList, meta, fd );
-
- if(!fd.features[MFT_ParticleNormal])
- {
- // turn obj->tangent into world->tangent
- viewToTangent = new Var;
- viewToTangent->setType( "float3x3" );
- viewToTangent->setName( "viewToTangent" );
- LangElement *viewToTangentDecl = new DecOp( viewToTangent );
-
- // Get the view->obj transform
- Var *viewToObj = getInvWorldView( componentList, fd.features[MFT_UseInstancing], meta );
-
- // assign world->tangent transform
- meta->addStatement( new GenOp( " @ = tMul( (@), float3x3(@) );\r\n", viewToTangentDecl, texSpaceMat, viewToObj ) );
- }
- else
- {
- // Assume particle normal generation has set this up in the proper space
- viewToTangent = texSpaceMat;
- }
- }
-
- // send transform to pixel shader
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-
- outViewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
- outViewToTangent->setName( "outViewToTangent" );
- outViewToTangent->setStructName( "OUT" );
- outViewToTangent->setType( "float3x3" );
- meta->addStatement( new GenOp( " @ = @;\r\n", outViewToTangent, viewToTangent ) );
-
- return outViewToTangent;
- }
- Var* ShaderFeatureGLSL::getOutTexCoord( const char *name,
- const char *type,
- bool useTexAnim,
- MultiLine *meta,
- Vector<ShaderComponent*> &componentList )
- {
- String outTexName = String::ToString( "out_%s", name );
- Var *texCoord = (Var*)LangElement::find( outTexName );
- if ( !texCoord )
- {
- Var *inTex = getVertTexCoord( name );
- AssertFatal( inTex, "ShaderFeatureGLSL::getOutTexCoord - Unknown vertex input coord!" );
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- texCoord = connectComp->getElement( RT_TEXCOORD );
- texCoord->setName( outTexName );
- texCoord->setStructName( "OUT" );
- texCoord->setType( type );
- if( useTexAnim )
- {
- inTex->setType( "vec4" );
-
- // create texture mat var
- Var *texMat = new Var;
- texMat->setType( "float4x4" );
- texMat->setName( "texMat" );
- texMat->uniform = true;
- texMat->constSortPos = cspPass;
-
- // Statement allows for casting of different types which
- // eliminates vector truncation problems.
- String statement = String::ToString( " @ = %s(tMul(@, @).xy);\r\n", type );
- meta->addStatement( new GenOp( statement , texCoord, texMat, inTex ) );
- }
- else
- {
- // Statement allows for casting of different types which
- // eliminates vector truncation problems.
- String statement = String::ToString( " @ = %s(@);\r\n", type );
- meta->addStatement( new GenOp( statement, texCoord, inTex ) );
- }
- }
- AssertFatal( String::compare( type, (const char*)texCoord->type ) == 0,
- "ShaderFeatureGLSL::getOutTexCoord - Type mismatch!" );
- return texCoord;
- }
- Var* ShaderFeatureGLSL::getInTexCoord( const char *name,
- const char *type,
- Vector<ShaderComponent*> &componentList )
- {
- Var* texCoord = (Var*)LangElement::find( name );
- if ( !texCoord )
- {
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
- texCoord = connectComp->getElement( RT_TEXCOORD );
- texCoord->setName( name );
- texCoord->setStructName( "IN" );
- texCoord->setType( type );
- }
- AssertFatal( String::compare( type, (const char*)texCoord->type ) == 0,
- "ShaderFeatureGLSL::getInTexCoord - Type mismatch!" );
- return texCoord;
- }
- Var* ShaderFeatureGLSL::getInColor( const char *name,
- const char *type,
- Vector<ShaderComponent*> &componentList )
- {
- Var *inColor = (Var*)LangElement::find( name );
- if ( !inColor )
- {
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
- inColor = connectComp->getElement( RT_COLOR );
- inColor->setName( name );
- inColor->setStructName( "IN" );
- inColor->setType( type );
- }
- AssertFatal( String::compare( type, (const char*)inColor->type ) == 0,
- "ShaderFeatureGLSL::getInColor - Type mismatch!" );
- return inColor;
- }
- Var* ShaderFeatureGLSL::addOutVpos( MultiLine *meta,
- Vector<ShaderComponent*> &componentList )
- {
- /*
- // Nothing to do if we're on SM 3.0... we use the real vpos.
- if ( GFX->getPixelShaderVersion() >= 3.0f )
- return NULL;
- */
- // For SM 2.x we need to generate the vpos in the vertex shader
- // and pass it as a texture coord to the pixel shader.
- Var *outVpos = (Var*)LangElement::find( "outVpos" );
- if ( !outVpos )
- {
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- outVpos = connectComp->getElement( RT_TEXCOORD );
- outVpos->setName( "outVpos" );
- outVpos->setStructName( "OUT" );
- outVpos->setType( "vec4" );
- Var *outPosition = (Var*) LangElement::find( "gl_Position" );
- AssertFatal( outPosition, "ShaderFeatureGLSL::addOutVpos - Didn't find the output position." );
- meta->addStatement( new GenOp( " @ = @;\r\n", outVpos, outPosition ) );
- }
- return outVpos;
- }
- Var* ShaderFeatureGLSL::getInVpos( MultiLine *meta,
- Vector<ShaderComponent*> &componentList )
- {
- Var *inVpos = (Var*)LangElement::find( "vpos" );
- if ( inVpos )
- return inVpos;
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector*>( componentList[C_CONNECTOR] );
- inVpos = connectComp->getElement( RT_TEXCOORD );
- inVpos->setName( "inVpos" );
- inVpos->setStructName( "IN" );
- inVpos->setType( "vec4" );
- return inVpos;
- }
- Var* ShaderFeatureGLSL::getInWorldToTangent( Vector<ShaderComponent*> &componentList )
- {
- Var *worldToTangent = (Var*)LangElement::find( "worldToTangent" );
- if ( !worldToTangent )
- {
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- worldToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
- worldToTangent->setName( "worldToTangent" );
- worldToTangent->setStructName( "IN" );
- worldToTangent->setType( "float3x3" );
- }
- return worldToTangent;
- }
- Var* ShaderFeatureGLSL::getInViewToTangent( Vector<ShaderComponent*> &componentList )
- {
- Var *viewToTangent = (Var*)LangElement::find( "viewToTangent" );
- if ( !viewToTangent )
- {
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- viewToTangent = connectComp->getElement( RT_TEXCOORD, 1, 3 );
- viewToTangent->setName( "viewToTangent" );
- viewToTangent->setStructName( "IN" );
- viewToTangent->setType( "float3x3" );
- }
- return viewToTangent;
- }
- Var* ShaderFeatureGLSL::getNormalMapTex()
- {
- Var *normalMap = (Var*)LangElement::find( "bumpMap" );
- if ( !normalMap )
- {
- normalMap = new Var;
- normalMap->setType( "sampler2D" );
- normalMap->setName( "bumpMap" );
- normalMap->uniform = true;
- normalMap->sampler = true;
- normalMap->constNum = Var::getTexUnitNum();
- }
- return normalMap;
- }
- Var* ShaderFeatureGLSL::getObjTrans( Vector<ShaderComponent*> &componentList,
- bool useInstancing,
- MultiLine *meta )
- {
- Var *objTrans = (Var*)LangElement::find( "objTrans" );
- if ( objTrans )
- return objTrans;
- if ( useInstancing )
- {
- ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
- Var *instObjTrans = vertStruct->getElement( RT_TEXCOORD, 4, 4 );
- instObjTrans->setStructName( "IN" );
- instObjTrans->setName( "inst_objectTrans" );
- mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+0 );
- mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+1 );
- mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+2 );
- mInstancingFormat->addElement( "objTrans", GFXDeclType_Float4, instObjTrans->constNum+3 );
- objTrans = new Var;
- objTrans->setType( "mat4x4" );
- objTrans->setName( "objTrans" );
- meta->addStatement( new GenOp( " @ = mat4x4( // Instancing!\r\n", new DecOp( objTrans ), instObjTrans ) );
- meta->addStatement( new GenOp( " @[0],\r\n", instObjTrans ) );
- meta->addStatement( new GenOp( " @[1],\r\n", instObjTrans ) );
- meta->addStatement( new GenOp( " @[2],\r\n",instObjTrans ) );
- meta->addStatement( new GenOp( " @[3] );\r\n", instObjTrans ) );
- }
- else
- {
- objTrans = new Var;
- objTrans->setType( "float4x4" );
- objTrans->setName( "objTrans" );
- objTrans->uniform = true;
- objTrans->constSortPos = cspPrimitive;
- }
-
- return objTrans;
- }
- Var* ShaderFeatureGLSL::getModelView( Vector<ShaderComponent*> &componentList,
- bool useInstancing,
- MultiLine *meta )
- {
- Var *modelview = (Var*)LangElement::find( "modelview" );
- if ( modelview )
- return modelview;
-
- if ( useInstancing )
- {
- Var *objTrans = getObjTrans( componentList, useInstancing, meta );
- Var *viewProj = (Var*)LangElement::find( "viewProj" );
- if ( !viewProj )
- {
- viewProj = new Var;
- viewProj->setType( "float4x4" );
- viewProj->setName( "viewProj" );
- viewProj->uniform = true;
- viewProj->constSortPos = cspPass;
- }
- modelview = new Var;
- modelview->setType( "float4x4" );
- modelview->setName( "modelview" );
- meta->addStatement( new GenOp( " @ = tMul( @, @ ); // Instancing!\r\n", new DecOp( modelview ), viewProj, objTrans ) );
- }
- else
- {
- modelview = new Var;
- modelview->setType( "float4x4" );
- modelview->setName( "modelview" );
- modelview->uniform = true;
- modelview->constSortPos = cspPrimitive;
- }
-
- return modelview;
- }
- Var* ShaderFeatureGLSL::getWorldView( Vector<ShaderComponent*> &componentList,
- bool useInstancing,
- MultiLine *meta )
- {
- Var *worldView = (Var*)LangElement::find( "worldViewOnly" );
- if ( worldView )
- return worldView;
-
- if ( useInstancing )
- {
- Var *objTrans = getObjTrans( componentList, useInstancing, meta );
- Var *worldToCamera = (Var*)LangElement::find( "worldToCamera" );
- if ( !worldToCamera )
- {
- worldToCamera = new Var;
- worldToCamera->setType( "float4x4" );
- worldToCamera->setName( "worldToCamera" );
- worldToCamera->uniform = true;
- worldToCamera->constSortPos = cspPass;
- }
- worldView = new Var;
- worldView->setType( "float4x4" );
- worldView->setName( "worldViewOnly" );
- meta->addStatement( new GenOp( " @ = tMul( @, @ ); // Instancing!\r\n", new DecOp( worldView ), worldToCamera, objTrans ) );
- }
- else
- {
- worldView = new Var;
- worldView->setType( "float4x4" );
- worldView->setName( "worldViewOnly" );
- worldView->uniform = true;
- worldView->constSortPos = cspPrimitive;
- }
-
- return worldView;
- }
- Var* ShaderFeatureGLSL::getInvWorldView( Vector<ShaderComponent*> &componentList,
- bool useInstancing,
- MultiLine *meta )
- {
- Var *viewToObj = (Var*)LangElement::find( "viewToObj" );
- if ( viewToObj )
- return viewToObj;
-
- if ( useInstancing )
- {
- Var *worldView = getWorldView( componentList, useInstancing, meta );
- viewToObj = new Var;
- viewToObj->setType( "float3x3" );
- viewToObj->setName( "viewToObj" );
- // We just use transpose to convert the 3x3 portion
- // of the world view transform into its inverse.
- meta->addStatement( new GenOp( " @ = transpose( float3x3(@) ); // Instancing!\r\n", new DecOp( viewToObj ), worldView ) );
- }
- else
- {
- viewToObj = new Var;
- viewToObj->setType( "float4x4" );
- viewToObj->setName( "viewToObj" );
- viewToObj->uniform = true;
- viewToObj->constSortPos = cspPrimitive;
- }
-
- return viewToObj;
- }
- void ShaderFeatureGLSL::getWsPosition( Vector<ShaderComponent*> &componentList,
- bool useInstancing,
- MultiLine *meta,
- LangElement *wsPosition )
- {
- Var *inPosition = (Var*)LangElement::find( "wsPosition" );
- if ( inPosition )
- {
- meta->addStatement( new GenOp( " @ = @.xyz;\r\n",
- wsPosition, inPosition ) );
- return;
- }
-
- // Get the input position.
- inPosition = (Var*)LangElement::find( "inPosition" );
- if ( !inPosition )
- inPosition = (Var*)LangElement::find( "position" );
-
- AssertFatal( inPosition, "ShaderFeatureGLSL::getWsPosition - The vertex position was not found!" );
-
- Var *objTrans = getObjTrans( componentList, useInstancing, meta );
-
- meta->addStatement( new GenOp( " @ = tMul( @, vec4( @.xyz, 1 ) ).xyz;\r\n",
- wsPosition, objTrans, inPosition ) );
- }
- Var* ShaderFeatureGLSL::addOutWsPosition( Vector<ShaderComponent*> &componentList,
- bool useInstancing,
- MultiLine *meta )
- {
- Var *outWsPosition = (Var*)LangElement::find( "outWsPosition" );
- if ( !outWsPosition )
- {
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- outWsPosition = connectComp->getElement( RT_TEXCOORD );
- outWsPosition->setName( "outWsPosition" );
- outWsPosition->setStructName( "OUT" );
- outWsPosition->setType( "vec3" );
-
- getWsPosition( componentList, useInstancing, meta, outWsPosition );
- }
-
- return outWsPosition;
- }
- Var* ShaderFeatureGLSL::getInWsPosition( Vector<ShaderComponent*> &componentList )
- {
- Var *wsPosition = (Var*)LangElement::find( "wsPosition" );
- if ( !wsPosition )
- {
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- wsPosition = connectComp->getElement( RT_TEXCOORD );
- wsPosition->setName( "wsPosition" );
- wsPosition->setStructName( "IN" );
- wsPosition->setType( "vec3" );
- }
-
- return wsPosition;
- }
- Var* ShaderFeatureGLSL::getWsView( Var *wsPosition, MultiLine *meta )
- {
- Var *wsView = (Var*)LangElement::find( "wsView" );
- if ( !wsView )
- {
- wsView = new Var( "wsView", "vec3" );
-
- Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
- if ( !eyePos )
- {
- eyePos = new Var;
- eyePos->setType( "vec3" );
- eyePos->setName( "eyePosWorld" );
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
-
- meta->addStatement( new GenOp( " @ = @ - @;\r\n",
- new DecOp( wsView ), eyePos, wsPosition ) );
- }
-
- return wsView;
- }
- Var* ShaderFeatureGLSL::getInWorldNormal(Vector<ShaderComponent*>& componentList)
- {
- Var* wsNormal = (Var*)LangElement::find("wsNormal");
- if (!wsNormal)
- {
- ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(componentList[C_CONNECTOR]);
- wsNormal = connectComp->getElement(RT_TEXCOORD);
- wsNormal->setName("wsNormal");
- wsNormal->setStructName("IN");
- wsNormal->setType("float3");
- }
- return wsNormal;
- }
- Var* ShaderFeatureGLSL::addOutDetailTexCoord( Vector<ShaderComponent*> &componentList,
- MultiLine *meta,
- bool useTexAnim,
- bool useFoliageTexCoord)
- {
- // Check if its already added.
- Var *outTex = (Var*)LangElement::find( "detCoord" );
- if ( outTex )
- return outTex;
-
- // Grab incoming texture coords.
- Var *inTex = getVertTexCoord( "texCoord" );
- if(useFoliageTexCoord)
- inTex->setType("float4");
-
- // create detail variable
- Var *detScale = new Var;
- detScale->setType( "vec2" );
- detScale->setName( "detailScale" );
- detScale->uniform = true;
- detScale->constSortPos = cspPotentialPrimitive;
-
- // grab connector texcoord register
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- outTex = connectComp->getElement( RT_TEXCOORD );
- outTex->setName( "detCoord" );
- outTex->setStructName( "OUT" );
- outTex->setType( "vec2" );
-
- if ( useTexAnim )
- {
- inTex->setType( "vec4" );
-
- // Find or create the texture matrix.
- Var *texMat = (Var*)LangElement::find( "texMat" );
- if ( !texMat )
- {
- texMat = new Var;
- texMat->setType( "float4x4" );
- texMat->setName( "texMat" );
- texMat->uniform = true;
- texMat->constSortPos = cspPass;
- }
-
- meta->addStatement( new GenOp( " @ = tMul(@.xy, @).xy * @;\r\n", outTex, texMat, inTex, detScale ) );
- }
- else
- {
- // setup output to mul texCoord by detail scale
- meta->addStatement( new GenOp( " @ = @.xy * @;\r\n", outTex, inTex, detScale ) );
- }
-
- return outTex;
- }
- Var* ShaderFeatureGLSL::getSurface(Vector<ShaderComponent*>& componentList, MultiLine* meta, const MaterialFeatureData& fd)
- {
- Var *surface = (Var *)LangElement::find("surface");
- if (!surface)
- {
- Var* diffuseColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
-
- Var* ormConfig = (Var*)LangElement::find("ORMConfig");
- if (!ormConfig)
- {
- Var* metalness = (Var*)LangElement::find("metalness");
- if (!metalness)
- {
- metalness = new Var("metalness", "float");
- metalness->uniform = true;
- metalness->constSortPos = cspPotentialPrimitive;
- }
-
- Var* roughness = (Var*)LangElement::find("roughness");
- if (!roughness)
- {
- roughness = new Var("roughness", "float");
- roughness->uniform = true;
- roughness->constSortPos = cspPotentialPrimitive;
- }
-
- ormConfig = new Var("ORMConfig", "vec4");
- LangElement* colorDecl = new DecOp(ormConfig);
- meta->addStatement(new GenOp(" @ = vec4(0.0,1.0,@,@);\r\n", colorDecl, roughness, metalness)); //reconstruct ormConfig, no ao darkening
- }
-
- Var* normal = (Var*)LangElement::find("normal");
- if (!normal)
- {
- normal = new Var("normal", "vec3");
- meta->addStatement(new GenOp(" @;\r\n\n", new DecOp(normal)));
-
- Var *wsNormal = (Var *)LangElement::find("wsNormal");
- if (!wsNormal)
- wsNormal = getInWorldNormal(componentList);
- meta->addStatement(new GenOp(" @ = normalize( @ );\r\n", normal, wsNormal));
- }
-
- Var* wsEyePos = (Var*)LangElement::find("eyePosWorld");
-
- if (!wsEyePos)
- {
- wsEyePos = new Var("eyePosWorld", "vec3");
- wsEyePos->uniform = true;
- wsEyePos->constSortPos = cspPass;
- }
-
- Var* wsPosition = getInWsPosition(componentList);
- Var* wsView = getWsView(wsPosition, meta);
- surface = new Var("surface", "Surface");
- meta->addStatement(new GenOp(" @ = createForwardSurface(@,normalize(@),@,@,@,@);\r\n\n", new DecOp(surface), diffuseColor, normal, ormConfig,
- wsPosition, wsEyePos, wsView));
- }
- return surface;
- }
- //****************************************************************************
- // Base Texture
- //****************************************************************************
- DiffuseMapFeatGLSL::DiffuseMapFeatGLSL()
- : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl"))
- {
- addDependency(&mTorqueDep);
- }
- void DiffuseMapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- MultiLine *meta = new MultiLine;
- getOutTexCoord( "texCoord",
- "vec2",
- fd.features[MFT_TexAnim],
- meta,
- componentList );
- output = meta;
- }
- U32 DiffuseMapFeatGLSL::getOutputTargets(const MaterialFeatureData &fd) const
- {
- return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
- }
- void DiffuseMapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // grab connector texcoord register
- Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
- //determine output target
- ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
- if (fd.features[MFT_isDeferred])
- targ = ShaderFeature::RenderTarget1;
- // create texture var
- Var *diffuseMap = new Var;
- diffuseMap->setType( "sampler2D" );
- diffuseMap->setName( "diffuseMap" );
- diffuseMap->uniform = true;
- diffuseMap->sampler = true;
- diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
- // create sample color var
- Var *diffColor = new Var;
- diffColor->setType("vec4");
- diffColor->setName("diffuseColor");
- LangElement *colorDecl = new DecOp( diffColor );
- MultiLine * meta = new MultiLine;
- output = meta;
- if ( fd.features[MFT_CubeMap] )
- {
- meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n",
- colorDecl,
- diffuseMap,
- inTex ) );
- meta->addStatement( new GenOp( " @;\r\n", assignColor( diffColor, Material::Mul, NULL, targ) ) );
- }
- else if(fd.features[MFT_DiffuseMapAtlas])
- {
- // Handle atlased textures
- // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
- Var *atlasedTex = new Var;
- atlasedTex->setName("atlasedTexCoord");
- atlasedTex->setType("vec2");
- LangElement *atDecl = new DecOp(atlasedTex);
- // Parameters of the texture atlas
- Var *atParams = new Var;
- atParams->setType("vec4");
- atParams->setName("diffuseAtlasParams");
- atParams->uniform = true;
- atParams->constSortPos = cspPotentialPrimitive;
- // Parameters of the texture (tile) this object is using in the atlas
- Var *tileParams = new Var;
- tileParams->setType("vec4");
- tileParams->setName("diffuseAtlasTileParams");
- tileParams->uniform = true;
- tileParams->constSortPos = cspPotentialPrimitive;
- const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
- if(is_sm3)
- {
- // Figure out the mip level
- meta->addStatement(new GenOp(" float2 _dx = ddx(@ * @.z);\r\n", inTex, atParams));
- meta->addStatement(new GenOp(" float2 _dy = ddy(@ * @.z);\r\n", inTex, atParams));
- meta->addStatement(new GenOp(" float mipLod = 0.5 * log2(max(dot(_dx, _dx), dot(_dy, _dy)));\r\n"));
- meta->addStatement(new GenOp(" mipLod = clamp(mipLod, 0.0, @.w);\r\n", atParams));
- // And the size of the mip level
- meta->addStatement(new GenOp(" float mipPixSz = pow(2.0, @.w - mipLod);\r\n", atParams));
- meta->addStatement(new GenOp(" float2 mipSz = mipPixSz / @.xy;\r\n", atParams));
- }
- else
- {
- meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
- }
- // Tiling mode
- // TODO: Select wrap or clamp somehow
- if( true ) // Wrap
- meta->addStatement(new GenOp(" @ = frac(@);\r\n", atDecl, inTex));
- else // Clamp
- meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, inTex));
- // Finally scale/offset, and correct for filtering
- meta->addStatement(new GenOp(" @ = @ * ((mipSz * @.xy - 1.0) / mipSz) + 0.5 / mipSz + @.xy * @.xy;\r\n",
- atlasedTex, atlasedTex, atParams, atParams, tileParams));
- // Add a newline
- meta->addStatement(new GenOp( "\r\n"));
- // For the rest of the feature...
- inTex = atlasedTex;
- // To dump out UV coords...
- //#define DEBUG_ATLASED_UV_COORDS
- #ifdef DEBUG_ATLASED_UV_COORDS
- if(!fd.features[MFT_DeferredConditioner])
- {
- meta->addStatement(new GenOp(" @ = vec4(@.xy, mipLod / @.w, 1.0);\r\n", new DecOp(diffColor), inTex, atParams));
- meta->addStatement(new GenOp(" @; return OUT;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
- return;
- }
- #endif
- meta->addStatement(new GenOp( " @ = tex2Dlod(@, float4(@, 0.0, mipLod));\r\n",
- new DecOp(diffColor), diffuseMap, inTex));
- meta->addStatement(new GenOp( " @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ) ) );
- }
- else
- {
- meta->addStatement(new GenOp("@ = tex2D(@, @);\r\n", colorDecl, diffuseMap, inTex));
- meta->addStatement(new GenOp(" @;\r\n", assignColor(diffColor, Material::Mul, NULL, targ)));
- }
- }
- ShaderFeature::Resources DiffuseMapFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- res.numTex = 1;
- res.numTexReg = 1;
- return res;
- }
- void DiffuseMapFeatGLSL::setTexData( Material::StageData &stageDat,
- const MaterialFeatureData &fd,
- RenderPassData &passData,
- U32 &texIndex )
- {
- GFXTextureObject *tex = stageDat.getTex( MFT_DiffuseMap );
- passData.mSamplerNames[ texIndex ] = "diffuseMap";
- passData.mTexSlot[ texIndex++ ].texObject = tex;
- }
- //****************************************************************************
- // Overlay Texture
- //****************************************************************************
- void OverlayTexFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- Var *inTex = getVertTexCoord( "texCoord2" );
- AssertFatal( inTex, "OverlayTexFeatGLSL::processVert() - The second UV set was not found!" );
- // grab connector texcoord register
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *outTex = connectComp->getElement( RT_TEXCOORD );
- outTex->setName( "outTexCoord2" );
- outTex->setStructName( "OUT" );
- outTex->setType( "vec2" );
- if( fd.features[MFT_TexAnim] )
- {
- inTex->setType( "vec4" );
- // Find or create the texture matrix.
- Var *texMat = (Var*)LangElement::find( "texMat" );
- if ( !texMat )
- {
- texMat = new Var;
- texMat->setType( "float4x4" );
- texMat->setName( "texMat" );
- texMat->uniform = true;
- texMat->constSortPos = cspPass;
- }
-
- output = new GenOp( " @ = tMul(@, @);\r\n", outTex, texMat, inTex );
- return;
- }
-
- // setup language elements to output incoming tex coords to output
- output = new GenOp( " @ = @;\r\n", outTex, inTex );
- }
- void OverlayTexFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // grab connector texcoord register
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *inTex = connectComp->getElement( RT_TEXCOORD );
- inTex->setName( "texCoord2" );
- inTex->setStructName( "IN" );
- inTex->setType( "vec2" );
- // create texture var
- Var *diffuseMap = new Var;
- diffuseMap->setType( "sampler2D" );
- diffuseMap->setName( "overlayMap" );
- diffuseMap->uniform = true;
- diffuseMap->sampler = true;
- diffuseMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
- LangElement *statement = new GenOp( "tex2D(@, @)", diffuseMap, inTex );
- output = new GenOp( " @;\r\n", assignColor( statement, Material::LerpAlpha ) );
- }
- ShaderFeature::Resources OverlayTexFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- res.numTex = 1;
- res.numTexReg = 1;
- return res;
- }
- void OverlayTexFeatGLSL::setTexData( Material::StageData &stageDat,
- const MaterialFeatureData &fd,
- RenderPassData &passData,
- U32 &texIndex )
- {
- GFXTextureObject *tex = stageDat.getTex( MFT_OverlayMap );
- if ( tex )
- {
- passData.mSamplerNames[ texIndex ] = "overlayMap";
- passData.mTexSlot[ texIndex++ ].texObject = tex;
- }
- }
- //****************************************************************************
- // Diffuse color
- //****************************************************************************
- U32 DiffuseFeatureGLSL::getOutputTargets(const MaterialFeatureData &fd) const
- {
- return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget1 : ShaderFeature::DefaultTarget;
- }
- void DiffuseFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- Var* diffuseMaterialColor = new Var;
- diffuseMaterialColor->setType( "vec4" );
- diffuseMaterialColor->setName( "diffuseMaterialColor" );
- diffuseMaterialColor->uniform = true;
- diffuseMaterialColor->constSortPos = cspPotentialPrimitive;
- MultiLine* meta = new MultiLine;
- Var *col = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- ShaderFeature::OutputTarget targ = ShaderFeature::DefaultTarget;
- if (fd.features[MFT_isDeferred])
- {
- targ = ShaderFeature::RenderTarget1;
- col = (Var*)LangElement::find(getOutputTargetVarName(targ));
- meta = new MultiLine;
- if (!col)
- {
- // create color var
- col = new Var;
- col->setType("vec4");
- col->setName(getOutputTargetVarName(targ));
- col->setStructName("OUT");
- meta->addStatement(new GenOp(" @ = vec4(1.0,1.0,1.0,1.0);\r\n", col));
- }
- }
- Material::BlendOp op;
- if (fd.features[MFT_DiffuseMap])
- op = Material::Mul;
- else
- op = Material::None;
- meta->addStatement(new GenOp(" @;\r\n", assignColor(diffuseMaterialColor, op, NULL, targ)));
- output = meta;
- }
- //****************************************************************************
- // Diffuse vertex color
- //****************************************************************************
- void DiffuseVertColorFeatureGLSL::processVert( Vector< ShaderComponent* >& componentList,
- const MaterialFeatureData& fd )
- {
- // Create vertex color connector if it doesn't exist.
-
- Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
- if( !outColor )
- {
- // Search for vert color.
-
- Var* inColor = dynamic_cast< Var* >( LangElement::find( "diffuse" ) );
- if( !inColor )
- {
- output = NULL;
- return;
- }
-
- // Create connector.
- ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
- AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
- outColor = connectComp->getElement( RT_COLOR );
- outColor->setName( "vertColor" );
- outColor->setStructName( "OUT" );
- outColor->setType( "vec4" );
- output = new GenOp( " @ = @;\r\n", outColor, inColor );
- }
- else
- output = NULL; // Nothing we need to do.
- }
- void DiffuseVertColorFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
- if( !vertColor )
- {
- ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
- AssertFatal( connectComp, "DiffuseVertColorFeatureGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
- vertColor = connectComp->getElement( RT_COLOR );
- vertColor->setName( "vertColor" );
- vertColor->setStructName( "IN" );
- vertColor->setType( "vec4" );
- }
-
- MultiLine* meta = new MultiLine;
- if (fd.features[MFT_isDeferred])
- meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul, NULL, ShaderFeature::RenderTarget1)));
- else
- meta->addStatement(new GenOp(" @;\r\n", assignColor(vertColor, Material::Mul)));
- output = meta;
- }
- //****************************************************************************
- // Lightmap
- //****************************************************************************
- void LightmapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // grab tex register from incoming vert
- Var *inTex = getVertTexCoord( "texCoord2" );
- // grab connector texcoord register
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *outTex = connectComp->getElement( RT_TEXCOORD );
- outTex->setName( "texCoord2" );
- outTex->setStructName( "OUT" );
- outTex->setType( "vec2" );
- // setup language elements to output incoming tex coords to output
- output = new GenOp( " @ = @;\r\n", outTex, inTex );
- }
- void LightmapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // grab connector texcoord register
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *inTex = connectComp->getElement( RT_TEXCOORD );
- inTex->setName( "texCoord2" );
- inTex->setStructName( "IN" );
- inTex->setType( "vec2" );
- // create texture var
- Var *lightMap = new Var;
- lightMap->setType( "sampler2D" );
- lightMap->setName( "lightMap" );
- lightMap->uniform = true;
- lightMap->sampler = true;
- lightMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
-
- // argh, pixel specular should prob use this too
- if( fd.features[MFT_NormalMap] )
- {
- Var *lmColor = new Var;
- lmColor->setName( "lmColor" );
- lmColor->setType( "vec4" );
- LangElement *lmColorDecl = new DecOp( lmColor );
-
- output = new GenOp( " @ = tex2D(@, @);\r\n", lmColorDecl, lightMap, inTex );
- return;
- }
-
- // Add realtime lighting, if it is available
- LangElement *statement = NULL;
- if( fd.features[MFT_RTLighting] )
- {
- // Advanced lighting is the only dynamic lighting supported right now
- Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
- if(inColor != NULL)
- {
- // Find out if RTLighting should be added or substituted
- bool bPreProcessedLighting = false;
- AdvancedLightBinManager *lightBin;
- if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
- bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
- // Lightmap has already been included in the advanced light bin, so
- // no need to do any sampling or anything
- if(bPreProcessedLighting)
- statement = new GenOp( "vec4(@, 1.0)", inColor );
- else
- statement = new GenOp( "tex2D(@, @) + vec4(@.rgb, 0.0)", lightMap, inTex, inColor );
- }
- }
-
- // If we still don't have it... then just sample the lightmap.
- if ( !statement )
- statement = new GenOp( "tex2D(@, @)", lightMap, inTex );
-
- // Assign to proper render target
- MultiLine *meta = new MultiLine;
- if( fd.features[MFT_LightbufferMRT] )
- {
- meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
- meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
- }
- else
- meta->addStatement( new GenOp( " @;\r\n", assignColor( statement, Material::Mul ) ) );
- output = meta;
- }
- ShaderFeature::Resources LightmapFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- res.numTex = 1;
- res.numTexReg = 1;
- return res;
- }
- void LightmapFeatGLSL::setTexData( Material::StageData &stageDat,
- const MaterialFeatureData &fd,
- RenderPassData &passData,
- U32 &texIndex )
- {
- GFXTextureObject *tex = stageDat.getTex( MFT_LightMap );
- passData.mSamplerNames[ texIndex ] = "lightMap";
- if ( tex )
- passData.mTexSlot[ texIndex++ ].texObject = tex;
- else
- passData.mTexType[ texIndex++ ] = Material::Lightmap;
- }
- U32 LightmapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
- {
- return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
- }
- //****************************************************************************
- // Tonemap
- //****************************************************************************
- void TonemapFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Grab the connector
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- // Set up the second set of texCoords
- Var *inTex2 = getVertTexCoord( "texCoord2" );
- if ( inTex2 )
- {
- Var *outTex2 = connectComp->getElement( RT_TEXCOORD );
- outTex2->setName( "texCoord2" );
- outTex2->setStructName( "OUT" );
- outTex2->setType( "vec2" );
- output = new GenOp( " @ = @;\r\n", outTex2, inTex2 );
- }
- }
- void TonemapFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Grab connector
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *inTex2 = connectComp->getElement( RT_TEXCOORD );
- inTex2->setName( "texCoord2" );
- inTex2->setStructName( "IN" );
- inTex2->setType( "vec2" );
- // create texture var
- Var *toneMap = new Var;
- toneMap->setType( "sampler2D" );
- toneMap->setName( "toneMap" );
- toneMap->uniform = true;
- toneMap->sampler = true;
- toneMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
- MultiLine * meta = new MultiLine;
- // First get the toneMap color
- Var *toneMapColor = new Var;
- toneMapColor->setType( "vec4" );
- toneMapColor->setName( "toneMapColor" );
- LangElement *toneMapColorDecl = new DecOp( toneMapColor );
- meta->addStatement( new GenOp( " @ = tex2D(@, @);\r\n", toneMapColorDecl, toneMap, inTex2 ) );
- // We do a different calculation if there is a diffuse map or not
- Material::BlendOp blendOp = Material::Mul;
- if ( fd.features[MFT_DiffuseMap] )
- {
- // Reverse the tonemap
- meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", toneMapColor, toneMapColor ) );
- // Re-tonemap with the current color factored in
- blendOp = Material::ToneMap;
- }
- // Find out if RTLighting should be added
- bool bPreProcessedLighting = false;
- AdvancedLightBinManager *lightBin;
- if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
- bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
-
- // Add in the realtime lighting contribution
- if ( fd.features[MFT_RTLighting] )
- {
- // Right now, only Advanced Lighting is supported
- Var *inColor = (Var*) LangElement::find( "d_lightcolor" );
- if(inColor != NULL)
- {
- // Assign value in d_lightcolor to toneMapColor if it exists. This is
- // the dynamic light buffer, and it already has the tonemap included
- if(bPreProcessedLighting)
- meta->addStatement( new GenOp( " @.rgb = @;\r\n", toneMapColor, inColor ) );
- else
- meta->addStatement( new GenOp( " @.rgb += @.rgb;\r\n", toneMapColor, inColor ) );
- }
- }
- // Assign to proper render target
- if( fd.features[MFT_LightbufferMRT] )
- {
- meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
- meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
- }
- else
- meta->addStatement( new GenOp( " @;\r\n", assignColor( toneMapColor, blendOp ) ) );
-
- output = meta;
- }
- ShaderFeature::Resources TonemapFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- res.numTex = 1;
- res.numTexReg = 1;
- return res;
- }
- void TonemapFeatGLSL::setTexData( Material::StageData &stageDat,
- const MaterialFeatureData &fd,
- RenderPassData &passData,
- U32 &texIndex )
- {
- GFXTextureObject *tex = stageDat.getTex( MFT_ToneMap );
- if ( tex )
- {
- passData.mTexType[ texIndex ] = Material::ToneMapTex;
- passData.mSamplerNames[ texIndex ] = "toneMap";
- passData.mTexSlot[ texIndex++ ].texObject = tex;
- }
- }
- U32 TonemapFeatGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
- {
- return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
- }
- //****************************************************************************
- // pureLIGHT Lighting
- //****************************************************************************
- void VertLitGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // If we have a lightMap or toneMap then our lighting will be
- // handled by the MFT_LightMap or MFT_ToneNamp feature instead
- if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
- {
- output = NULL;
- return;
- }
- // Create vertex color connector if it doesn't exist.
-
- Var* outColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
- if( !outColor )
- {
- // Grab the connector color
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- outColor = connectComp->getElement( RT_COLOR );
- outColor->setName( "vertColor" );
- outColor->setStructName( "OUT" );
- outColor->setType( "vec4" );
-
- // Search for vert color
- Var *inColor = (Var*) LangElement::find( "diffuse" );
- // If there isn't a vertex color then we can't do anything
- if( !inColor )
- {
- output = NULL;
- return;
- }
- output = new GenOp( " @ = @;\r\n", outColor, inColor );
- }
- else
- output = NULL; // Nothing we need to do.
- }
- void VertLitGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // If we have a lightMap or toneMap then our lighting will be
- // handled by the MFT_LightMap or MFT_ToneNamp feature instead
- if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] )
- {
- output = NULL;
- return;
- }
-
- // Grab the connector color register
- Var* vertColor = dynamic_cast< Var* >( LangElement::find( "vertColor" ) );
- if( !vertColor )
- {
- ShaderConnector* connectComp = dynamic_cast< ShaderConnector* >( componentList[ C_CONNECTOR ] );
- AssertFatal( connectComp, "VertLitGLSL::processVert - C_CONNECTOR is not a ShaderConnector" );
- vertColor = connectComp->getElement( RT_COLOR );
- vertColor->setName( "vertColor" );
- vertColor->setStructName( "IN" );
- vertColor->setType( "vec4" );
- }
- MultiLine * meta = new MultiLine;
-
- // Defaults (no diffuse map)
- Material::BlendOp blendOp = Material::Mul;
- LangElement *outColor = vertColor;
- // We do a different calculation if there is a diffuse map or not
- if ( fd.features[MFT_DiffuseMap] || fd.features[MFT_VertLitTone] )
- {
- Var * finalVertColor = new Var;
- finalVertColor->setName( "finalVertColor" );
- finalVertColor->setType( "vec4" );
- LangElement *finalVertColorDecl = new DecOp( finalVertColor );
-
- // Reverse the tonemap
- meta->addStatement( new GenOp( " @ = -1.0f * log(1.0f - @);\r\n", finalVertColorDecl, vertColor ) );
-
- // Set the blend op to tonemap
- blendOp = Material::ToneMap;
- outColor = finalVertColor;
- }
-
- // Add in the realtime lighting contribution, if applicable
- if ( fd.features[MFT_RTLighting] )
- {
- Var *rtLightingColor = (Var*) LangElement::find( "d_lightcolor" );
- if(rtLightingColor != NULL)
- {
- bool bPreProcessedLighting = false;
- AdvancedLightBinManager *lightBin;
- if ( Sim::findObject( "AL_LightBinMgr", lightBin ) )
- bPreProcessedLighting = lightBin->MRTLightmapsDuringDeferred();
-
- // Assign value in d_lightcolor to toneMapColor if it exists. This is
- // the dynamic light buffer, and it already has the baked-vertex-color
- // included in it
- if(bPreProcessedLighting)
- outColor = new GenOp( "vec4(@.rgb, 1.0)", rtLightingColor );
- else
- outColor = new GenOp( "vec4(@.rgb + @.rgb, 1.0)", rtLightingColor, outColor );
- }
- }
-
- // Output the color
- if ( fd.features[MFT_LightbufferMRT] )
- {
- meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, Material::None, NULL, ShaderFeature::RenderTarget3 ) ) );
- meta->addStatement( new GenOp( " @.a = 0.0001;\r\n", LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget3) ) ) );
- }
- else
- meta->addStatement( new GenOp( " @;\r\n", assignColor( outColor, blendOp ) ) );
-
- output = meta;
- }
- U32 VertLitGLSL::getOutputTargets( const MaterialFeatureData &fd ) const
- {
- return fd.features[MFT_LightbufferMRT] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
- }
- //****************************************************************************
- // Detail map
- //****************************************************************************
- void DetailFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- MultiLine *meta = new MultiLine;
- addOutDetailTexCoord( componentList,
- meta,
- fd.features[MFT_TexAnim], fd.features[MFT_Foliage]);
- output = meta;
- }
- void DetailFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Get the detail texture coord.
- Var *inTex = getInTexCoord( "detCoord", "vec2", componentList );
- // create texture var
- Var *detailMap = new Var;
- detailMap->setType( "sampler2D" );
- detailMap->setName( "detailMap" );
- detailMap->uniform = true;
- detailMap->sampler = true;
- detailMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
- // We're doing the standard greyscale detail map
- // technique which can darken and lighten the
- // diffuse texture.
- // TODO: We could add a feature to toggle between this
- // and a simple multiplication with the detail map.
- LangElement *statement = new GenOp( "( tex2D(@, @) * 2.0 ) - 1.0", detailMap, inTex );
- if ( fd.features[MFT_isDeferred])
- output = new GenOp( " @;\r\n", assignColor( statement, Material::Add, NULL, ShaderFeature::RenderTarget1 ) );
- else
- output = new GenOp( " @;\r\n", assignColor( statement, Material::Add ) );
- }
- ShaderFeature::Resources DetailFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- res.numTex = 1;
- res.numTexReg = 1;
- return res;
- }
- void DetailFeatGLSL::setTexData( Material::StageData &stageDat,
- const MaterialFeatureData &fd,
- RenderPassData &passData,
- U32 &texIndex )
- {
- GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
- if ( tex )
- {
- passData.mSamplerNames[texIndex] = "detailMap";
- passData.mTexSlot[ texIndex++ ].texObject = tex;
- }
- }
- //****************************************************************************
- // Vertex position
- //****************************************************************************
- void VertPositionGLSL::determineFeature( Material *material,
- const GFXVertexFormat *vertexFormat,
- U32 stageNum,
- const FeatureType &type,
- const FeatureSet &features,
- MaterialFeatureData *outFeatureData )
- {
- // This feature is always on!
- outFeatureData->features.addFeature( type );
- }
- void VertPositionGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // First check for an input position from a previous feature
- // then look for the default vertex position.
- Var *inPosition = (Var*)LangElement::find( "inPosition" );
- if ( !inPosition )
- inPosition = (Var*)LangElement::find( "position" );
- // grab connector position
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *outPosition = connectComp->getElement( RT_POSITION );
- outPosition->setName( "gl_Position" );
-
- MultiLine *meta = new MultiLine;
-
- Var *modelview = getModelView( componentList, fd.features[MFT_UseInstancing], meta );
-
- meta->addStatement( new GenOp( " @ = tMul(@, vec4(@.xyz,1));\r\n",
- outPosition, modelview, inPosition ) );
- if (fd.materialFeatures[MFT_isBackground])
- {
- meta->addStatement(new GenOp(" @ = @.xyww;\r\n", outPosition, outPosition));
- }
- output = meta;
- }
- //****************************************************************************
- // Reflect Cubemap
- //****************************************************************************
- void ReflectCubeFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // search for vert normal
- Var *inNormal = (Var*) LangElement::find( "normal" );
- if ( !inNormal )
- return;
- MultiLine * meta = new MultiLine;
- // If a base or bump tex is present in the material, but not in the
- // current pass - we need to add one to the current pass to use
- // its alpha channel as a gloss map. Here we just need the tex coords.
- if( !fd.features[MFT_DiffuseMap] &&
- !fd.features[MFT_NormalMap] )
- {
- if( fd.materialFeatures[MFT_DiffuseMap] ||
- fd.materialFeatures[MFT_NormalMap] )
- {
- // find incoming texture var
- Var *inTex = getVertTexCoord( "texCoord" );
- // grab connector texcoord register
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *outTex = connectComp->getElement( RT_TEXCOORD );
- outTex->setName( "texCoord" );
- outTex->setStructName( "OUT" );
- outTex->setType( "vec2" );
- // setup language elements to output incoming tex coords to output
- meta->addStatement( new GenOp( " @ = @;\r\n", outTex, inTex ) );
- }
- }
- // create cubeTrans
- bool useInstancing = fd.features[MFT_UseInstancing];
- Var *cubeTrans = getObjTrans( componentList, useInstancing, meta );
- // cube vert position
- Var * cubeVertPos = new Var;
- cubeVertPos->setName( "cubeVertPos" );
- cubeVertPos->setType( "vec3" );
- LangElement *cubeVertPosDecl = new DecOp( cubeVertPos );
- meta->addStatement( new GenOp( " @ = tMul( @, float4(@,1)).xyz;\r\n",
- cubeVertPosDecl, cubeTrans, LangElement::find( "position" ) ) );
- // cube normal
- Var * cubeNormal = new Var;
- cubeNormal->setName( "cubeNormal" );
- cubeNormal->setType( "vec3" );
- LangElement *cubeNormDecl = new DecOp( cubeNormal );
-
- meta->addStatement( new GenOp( " @ = ( tMul( (@), vec4(@, 0) ) ).xyz;\r\n",
- cubeNormDecl, cubeTrans, inNormal ) );
- meta->addStatement( new GenOp( " @ = bool(length(@)) ? normalize(@) : @;\r\n",
- cubeNormal, cubeNormal, cubeNormal, cubeNormal ) );
- // grab the eye position
- Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
- if ( !eyePos )
- {
- eyePos = new Var( "eyePosWorld", "vec3" );
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
- // eye to vert
- Var * eyeToVert = new Var;
- eyeToVert->setName( "eyeToVert" );
- eyeToVert->setType( "vec3" );
- LangElement *e2vDecl = new DecOp( eyeToVert );
- meta->addStatement( new GenOp( " @ = @ - @;\r\n",
- e2vDecl, cubeVertPos, eyePos ) );
- // grab connector texcoord register
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
- reflectVec->setName( "reflectVec" );
- reflectVec->setStructName( "OUT" );
- reflectVec->setType( "vec3" );
- meta->addStatement( new GenOp( " @ = reflect(@, @);\r\n", reflectVec, eyeToVert, cubeNormal ) );
- output = meta;
- }
- void ReflectCubeFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- MultiLine * meta = new MultiLine;
- Var *glossColor = NULL;
-
- // If a base or bump tex is present in the material, but not in the
- // current pass - we need to add one to the current pass to use
- // its alpha channel as a gloss map.
- if( !fd.features[MFT_DiffuseMap] &&
- !fd.features[MFT_NormalMap] )
- {
- if( fd.materialFeatures[MFT_DiffuseMap] ||
- fd.materialFeatures[MFT_NormalMap] )
- {
- // grab connector texcoord register
- Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
-
- // create texture var
- Var *newMap = new Var;
- newMap->setType( "sampler2D" );
- newMap->setName( "glossMap" );
- newMap->uniform = true;
- newMap->sampler = true;
- newMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
-
- // create sample color
- Var *color = new Var;
- color->setType( "vec4" );
- color->setName( "diffuseColor" );
- LangElement *colorDecl = new DecOp( color );
- glossColor = color;
-
- meta->addStatement( new GenOp( " @ = tex2D( @, @ );\r\n", colorDecl, newMap, inTex ) );
- }
- }
- if (!glossColor)
- {
- if (fd.features[MFT_isDeferred])
- glossColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
- if (!glossColor)
- glossColor = (Var*)LangElement::find("diffuseColor");
- if (!glossColor)
- glossColor = (Var*)LangElement::find("bumpNormal");
- }
- // grab connector texcoord register
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *reflectVec = connectComp->getElement( RT_TEXCOORD );
- reflectVec->setName( "reflectVec" );
- reflectVec->setStructName( "IN" );
- reflectVec->setType( "vec3" );
- // create cubemap var
- Var *cubeMap = new Var;
- cubeMap->setType( "samplerCube" );
- cubeMap->setName( "cubeMap" );
- cubeMap->uniform = true;
- cubeMap->sampler = true;
- cubeMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
- Var *cubeMips = new Var;
- cubeMips->setType("float");
- cubeMips->setName("cubeMips");
- cubeMips->uniform = true;
- cubeMips->constSortPos = cspPotentialPrimitive;
- // TODO: Restore the lighting attenuation here!
- Var *attn = NULL;
- //if ( fd.materialFeatures[MFT_DynamicLight] )
- //attn = (Var*)LangElement::find("attn");
- //else
- if ( fd.materialFeatures[MFT_RTLighting] )
- attn =(Var*)LangElement::find("d_NL_Att");
- LangElement *texCube = NULL;
- Var* matinfo = (Var*) LangElement::find( getOutputTargetVarName(ShaderFeature::RenderTarget2) );
- Var *roughness = (Var*)LangElement::find("roughness");
- if (roughness) //try to grab roughness directly
- texCube = new GenOp("textureLod( @, @, min((1.0 - @)*@ + 1.0, @))", cubeMap, reflectVec, roughness, cubeMips, cubeMips);
- else if (glossColor) //failing that, try and find color data
- texCube = new GenOp("textureLod( @, @, min((1.0 - @.b)*@ + 1.0, @))", cubeMap, reflectVec, glossColor, cubeMips, cubeMips);
- else //failing *that*, just draw the cubemap
- texCube = new GenOp("texture( @, @)", cubeMap, reflectVec);
-
- LangElement *lerpVal = NULL;
- Material::BlendOp blendOp = Material::LerpAlpha;
- // Note that the lerpVal needs to be a float4 so that
- // it will work with the LerpAlpha blend.
-
- if (matinfo)
- {
- if (attn)
- lerpVal = new GenOp("@ * saturate( @ )", matinfo, attn);
- else
- lerpVal = new GenOp("@", matinfo);
- }
- else if ( glossColor )
- {
- if ( attn )
- lerpVal = new GenOp( "@ * saturate( @ )", glossColor, attn );
- else
- lerpVal = glossColor;
- }
- else
- {
- if ( attn )
- lerpVal = new GenOp( "vec4( saturate( @ ) ).xxxx", attn );
- else
- blendOp = Material::Mul;
- }
-
- Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
- if (fd.features[MFT_isDeferred])
- {
- //metalness: black(0) = color, white(1) = reflection
- if (fd.features[MFT_ToneMap])
- meta->addStatement(new GenOp(" @ *= @;\r\n", targ, texCube));
- else
- meta->addStatement(new GenOp(" @ = @;\r\n", targ, texCube));
- }
- else
- {
- meta->addStatement(new GenOp(" //forward lit cubemapping\r\n"));
- targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- Var *metalness = (Var*)LangElement::find("metalness");
- if (metalness)
- {
- Var *dColor = new Var("dColor", "vec3");
- Var *envColor = new Var("envColor", "vec3");
- meta->addStatement(new GenOp(" @ = @.rgb - (@.rgb * @);\r\n", new DecOp(dColor), targ, targ, metalness));
- meta->addStatement(new GenOp(" @ = @.rgb*(@).rgb;\r\n", new DecOp(envColor), targ, texCube));
- meta->addStatement(new GenOp(" @.rgb = @+@;\r\n", targ, dColor, envColor));
- }
- else if (lerpVal)
- meta->addStatement(new GenOp(" @ *= vec4(@.rgb*@.a, @.a);\r\n", targ, texCube, lerpVal, targ));
- else
- meta->addStatement(new GenOp(" @.rgb *= @.rgb;\r\n", targ, texCube));
- }
- output = meta;
- }
- ShaderFeature::Resources ReflectCubeFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- if( fd.features[MFT_DiffuseMap] ||
- fd.features[MFT_NormalMap] )
- {
- res.numTex = 1;
- res.numTexReg = 1;
- }
- else
- {
- res.numTex = 2;
- res.numTexReg = 2;
- }
- return res;
- }
- void ReflectCubeFeatGLSL::setTexData( Material::StageData &stageDat,
- const MaterialFeatureData &stageFeatures,
- RenderPassData &passData,
- U32 &texIndex )
- {
- // set up a gloss map if one is not present in the current pass
- // but is present in the current material stage
- if( !passData.mFeatureData.features[MFT_DiffuseMap] &&
- !passData.mFeatureData.features[MFT_NormalMap] )
- {
- GFXTextureObject *tex = stageDat.getTex( MFT_DetailMap );
- if ( tex && stageFeatures.features[MFT_DiffuseMap] )
- {
- passData.mSamplerNames[ texIndex ] = "diffuseMap";
- passData.mTexSlot[ texIndex++ ].texObject = tex;
- }
- else
- {
- tex = stageDat.getTex( MFT_NormalMap );
- if ( tex && stageFeatures.features[ MFT_NormalMap ] )
- {
- passData.mSamplerNames[ texIndex ] = "bumpMap";
- passData.mTexSlot[ texIndex++ ].texObject = tex;
- }
- }
- }
-
- if( stageDat.getCubemap() )
- {
- passData.mCubeMap = stageDat.getCubemap();
- passData.mSamplerNames[texIndex] = "cubeMap";
- passData.mTexType[texIndex++] = Material::Cube;
- }
- else
- {
- if( stageFeatures.features[MFT_CubeMap] )
- {
- // assuming here that it is a scenegraph cubemap
- passData.mSamplerNames[texIndex] = "cubeMap";
- passData.mTexType[texIndex++] = Material::SGCube;
- }
- }
- }
- //****************************************************************************
- // RTLighting
- //****************************************************************************
- RTLightingFeatGLSL::RTLightingFeatGLSL()
- : mDep(ShaderGen::smCommonShaderPath + String("/gl/lighting.glsl" ))
- {
- addDependency( &mDep );
- }
- void RTLightingFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- MultiLine *meta = new MultiLine;
-
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
-
- // Special case for lighting imposters. We dont have a vert normal and may not
- // have a normal map. Generate and pass the normal data the pixel shader needs.
- if ( fd.features[MFT_ImposterVert] )
- {
- if ( !fd.features[MFT_NormalMap] )
- {
- Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
- if ( !eyePos )
- {
- eyePos = new Var( "eyePosWorld", "vec3" );
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
- //Temporarily disabled while we figure out how to better handle normals without a normal map
- /*Var *inPosition = (Var*)LangElement::find( "position" );
-
- Var *outNormal = connectComp->getElement( RT_TEXCOORD );
- outNormal->setName( "wsNormal" );
- outNormal->setStructName( "OUT" );
- outNormal->setType( "vec3" );
-
- // Transform the normal to world space.
- meta->addStatement( new GenOp( " @ = normalize( @ - @.xyz );\r\n", outNormal, eyePos, inPosition ) );*/
- }
-
- addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
-
- output = meta;
-
- return;
- }
-
- addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
- getOutWorldToTangent(componentList, meta, fd);
-
- output = meta;
-
- // Find the incoming vertex normal.
- Var *inNormal = (Var*)LangElement::find( "normal" );
- if ( !inNormal ||
- fd.features[MFT_LightMap] ||
- fd.features[MFT_ToneMap] ||
- fd.features[MFT_VertLit] )
- return;
-
- // If there isn't a normal map then we need to pass
- // the world space normal to the pixel shader ourselves.
- if ( !fd.features[MFT_NormalMap] )
- {
- Var *outNormal = connectComp->getElement( RT_TEXCOORD );
- outNormal->setName( "wsNormal" );
- outNormal->setStructName( "OUT" );
- outNormal->setType( "vec3" );
- // Get the transform to world space.
- Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
-
- // Transform the normal to world space.
- meta->addStatement( new GenOp( " @ = tMul( @, vec4( normalize( @ ), 0.0 ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
- }
- }
- void RTLightingFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Skip out on realtime lighting if we don't have a normal
- // or we're doing some sort of baked lighting.
- //
- // TODO: We can totally detect for this in the material
- // feature setup... we should move it out of here!
- //
- if ( fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit] )
- return;
-
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- MultiLine *meta = new MultiLine;
- // Now the wsPosition and wsView.
- Var *wsPosition = getInWsPosition( componentList );
- Var* worldToTangent = getInWorldToTangent(componentList);
- Var* wsNormal = getInWorldNormal(componentList);
- Var *wsView = getWsView( wsPosition, meta );
- // Look for a light mask generated from a previous
- // feature (this is done for BL terrain lightmaps).
- LangElement *lightMask = LangElement::find( "lightMask" );
- if ( !lightMask )
- lightMask = new GenOp( "vec4( 1, 1, 1, 1 )" );
- // Get all the light constants.
- Var *inLightPos = new Var( "inLightPos", "vec4" );
- inLightPos->uniform = true;
- inLightPos->arraySize = 4;
- inLightPos->constSortPos = cspPotentialPrimitive;
- Var * inLightConfigData = new Var( "inLightConfigData", "vec4" );
- inLightConfigData->uniform = true;
- inLightConfigData->arraySize = 4;
- inLightConfigData->constSortPos = cspPotentialPrimitive;
- Var *inLightColor = new Var( "inLightColor", "vec4" );
- inLightColor->uniform = true;
- inLightColor->arraySize = 4;
- inLightColor->constSortPos = cspPotentialPrimitive;
- Var *inLightSpotDir = new Var( "inLightSpotDir", "vec4" );
- inLightSpotDir->uniform = true;
- inLightSpotDir->arraySize = 4;
- inLightSpotDir->constSortPos = cspPotentialPrimitive;
- Var * lightSpotParams = new Var( "inlightSpotParams", "vec2" );
- lightSpotParams->uniform = true;
- lightSpotParams->arraySize = 4;
- lightSpotParams->constSortPos = cspPotentialPrimitive;
- Var* hasVectorLight = new Var("hasVectorLight", "int");
- hasVectorLight->uniform = true;
- hasVectorLight->constSortPos = cspPotentialPrimitive;
- Var* vectorLightDirection = new Var("vectorLightDirection", "vec4");
- vectorLightDirection->uniform = true;
- vectorLightDirection->constSortPos = cspPotentialPrimitive;
- Var* vectorLightColor = new Var("vectorLightColor", "vec4");
- vectorLightColor->uniform = true;
- vectorLightColor->constSortPos = cspPotentialPrimitive;
- Var* vectorLightBrightness = new Var("vectorLightBrightness", "float");
- vectorLightBrightness->uniform = true;
- vectorLightBrightness->constSortPos = cspPotentialPrimitive;
- Var* surface = getSurface(componentList, meta, fd);
- if (!surface)
- {
- Con::errorf("ShaderGen::RTLightingFeatGLSL() - failed to generate surface!");
- return;
- }
- Var *roughness = (Var*)LangElement::find("roughness");
- Var *metalness = (Var*)LangElement::find("metalness");
- Var *curColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- Var *ambient = new Var( "ambient", "vec4" );
- ambient->uniform = true;
- ambient->constSortPos = cspPass;
-
- Var* lighting = new Var("lighting", "vec4");
- meta->addStatement(new GenOp(" @ = compute4Lights( @, @, @, @,\r\n"
- " @, @, @, @, @, @, @);\r\n",
- new DecOp(lighting), surface, lightMask, inLightPos, inLightConfigData, inLightColor, inLightSpotDir, lightSpotParams,
- hasVectorLight, vectorLightDirection, vectorLightColor, vectorLightBrightness));
- meta->addStatement(new GenOp(" @.rgb += @.rgb;\r\n", curColor, lighting));
- output = meta;
- }
- ShaderFeature::Resources RTLightingFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- // These features disable realtime lighting.
- if ( !fd.features[MFT_LightMap] &&
- !fd.features[MFT_ToneMap] &&
- !fd.features[MFT_VertLit] )
- {
- // If enabled we pass the position.
- res.numTexReg = 1;
- // If there isn't a bump map then we pass the
- // world space normal as well.
- if ( !fd.features[MFT_NormalMap] )
- res.numTexReg++;
- }
- return res;
- }
- //****************************************************************************
- // Fog
- //****************************************************************************
- FogFeatGLSL::FogFeatGLSL()
- : mFogDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
- {
- addDependency( &mFogDep );
- }
- void FogFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- MultiLine *meta = new MultiLine;
-
- const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
- if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
- {
- // Grab the eye position.
- Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
- if ( !eyePos )
- {
- eyePos = new Var( "eyePosWorld", "vec3" );
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
-
- Var *fogData = new Var( "fogData", "vec3" );
- fogData->uniform = true;
- fogData->constSortPos = cspPass;
-
- Var *wsPosition = new Var( "fogPos", "vec3" );
- getWsPosition( componentList,
- fd.features[MFT_UseInstancing],
- meta,
- new DecOp( wsPosition ) );
-
- // We pass the fog amount to the pixel shader.
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *fogAmount = connectComp->getElement( RT_TEXCOORD );
- fogAmount->setName( "fogAmount" );
- fogAmount->setStructName( "OUT" );
- fogAmount->setType( "float" );
-
- meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
- fogAmount, eyePos, wsPosition, fogData, fogData, fogData ) );
- }
- else
- {
- // We fog in world space... make sure the world space
- // position is passed to the pixel shader. This is
- // often already passed for lighting, so it takes up
- // no extra output registers.
- addOutWsPosition( componentList, fd.features[MFT_UseInstancing], meta );
- }
-
- output = meta;
- }
- void FogFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- MultiLine *meta = new MultiLine;
-
- Var *fogColor = new Var;
- fogColor->setType( "vec4" );
- fogColor->setName( "fogColor" );
- fogColor->uniform = true;
- fogColor->constSortPos = cspPass;
-
- // Get the out color.
- Var *color = (Var*) LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- if (fd.features[MFT_isDeferred])
- color = (Var *)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
- if ( !color )
- {
- color = new Var;
- color->setType( "vec4" );
- color->setName( "col" );
- color->setStructName("OUT");
- }
-
- Var *fogAmount;
-
- const bool vertexFog = Con::getBoolVariable( "$useVertexFog", false );
- if ( vertexFog || GFX->getPixelShaderVersion() < 3.0 )
- {
- // Per-vertex.... just get the fog amount.
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- fogAmount = connectComp->getElement( RT_TEXCOORD );
- fogAmount->setName( "fogAmount" );
- fogAmount->setStructName( "IN" );
- fogAmount->setType( "float" );
- }
- else
- {
- Var *wsPosition = getInWsPosition( componentList );
-
- // grab the eye position
- Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
- if ( !eyePos )
- {
- eyePos = new Var( "eyePosWorld", "vec3" );
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
-
- Var *fogData = new Var( "fogData", "vec3" );
- fogData->uniform = true;
- fogData->constSortPos = cspPass;
-
- /// Get the fog amount.
- fogAmount = new Var( "fogAmount", "float" );
- meta->addStatement( new GenOp( " @ = saturate( computeSceneFog( @, @, @.r, @.g, @.b ) );\r\n",
- new DecOp( fogAmount ), eyePos, wsPosition, fogData, fogData, fogData ) );
- }
-
- // Lerp between the fog color and diffuse color.
- LangElement *fogLerp = new GenOp( "lerp( @.rgb, @.rgb, @ )", fogColor, color, fogAmount );
- meta->addStatement( new GenOp( " @.rgb = @;\r\n", color, fogLerp ) );
-
- output = meta;
- }
- ShaderFeature::Resources FogFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- res.numTexReg = 1;
- return res;
- }
- //****************************************************************************
- // Visibility
- //****************************************************************************
- VisibilityFeatGLSL::VisibilityFeatGLSL()
- : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
- {
- addDependency( &mTorqueDep );
- }
- void VisibilityFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- MultiLine *meta = new MultiLine;
- output = meta;
- if ( fd.features[ MFT_UseInstancing ] )
- {
- // We pass the visibility to the pixel shader via
- // another output register.
- //
- // TODO: We should see if we can share this register
- // with some other common instanced data.
- //
- ShaderConnector *conn = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *outVisibility = conn->getElement( RT_TEXCOORD );
- outVisibility->setStructName( "OUT" );
- outVisibility->setName( "visibility" );
- outVisibility->setType( "float" );
- ShaderConnector *vertStruct = dynamic_cast<ShaderConnector *>( componentList[C_VERT_STRUCT] );
- Var *instVisibility = vertStruct->getElement( RT_TEXCOORD, 1 );
- instVisibility->setStructName( "IN" );
- instVisibility->setName( "inst_visibility" );
- instVisibility->setType( "float" );
- mInstancingFormat->addElement( "visibility", GFXDeclType_Float, instVisibility->constNum );
-
- meta->addStatement( new GenOp( " @ = @; // Instancing!\r\n", outVisibility, instVisibility ) );
- }
- if ( fd.features[ MFT_IsTranslucent ] )
- return;
- addOutVpos( meta, componentList );
- }
- void VisibilityFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Get the visibility constant.
- Var *visibility = NULL;
- if ( fd.features[ MFT_UseInstancing ] )
- visibility = getInTexCoord( "visibility", "float", componentList );
- else
- {
- visibility = (Var*)LangElement::find( "visibility" );
-
- if ( !visibility )
- {
- visibility = new Var();
- visibility->setType( "float" );
- visibility->setName( "visibility" );
- visibility->uniform = true;
- visibility->constSortPos = cspPotentialPrimitive;
- }
- }
- MultiLine* meta = new MultiLine;
- output = meta;
-
- // Translucent objects do a simple alpha fade.
- if ( fd.features[ MFT_IsTranslucent ] )
- {
- Var *color = (Var*) LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- meta->addStatement( new GenOp( " @.a *= @;\r\n", color, visibility ) );
- return;
- }
- // Everything else does a fizzle.
- Var *vPos = getInVpos( meta, componentList );
- meta->addStatement( new GenOp( " fizzle( @.xy, @ );\r\n", vPos, visibility ) );
- }
- ShaderFeature::Resources VisibilityFeatGLSL::getResources( const MaterialFeatureData &fd )
- {
- Resources res;
- // TODO: Fix for instancing.
-
- if ( !fd.features[ MFT_IsTranslucent ] )
- res.numTexReg = 1;
- return res;
- }
- //****************************************************************************
- // AlphaTest
- //****************************************************************************
- void AlphaTestGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // If we're below SM3 and don't have a depth output
- // feature then don't waste an instruction here.
- if (( GFX->getPixelShaderVersion() < 3.0 &&
- !fd.features[ MFT_EyeSpaceDepthOut ] &&
- !fd.features[ MFT_DepthOut ] ) ||
- fd.features[MFT_IsTranslucent])
- {
- output = NULL;
- return;
- }
- // If we don't have a color var then we cannot do an alpha test.
- Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1) );
- if ( !color )
- color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- if ( !color )
- {
- output = NULL;
- return;
- }
- // Now grab the alpha test value.
- Var *alphaTestVal = new Var;
- alphaTestVal->setType( "float" );
- alphaTestVal->setName( "alphaTestValue" );
- alphaTestVal->uniform = true;
- alphaTestVal->constSortPos = cspPotentialPrimitive;
- // Do the clip.
- output = new GenOp( " clip( @.a - @ );\r\n", color, alphaTestVal );
- }
- //****************************************************************************
- // GlowMask
- //****************************************************************************
- void GlowMaskGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- output = NULL;
- // Get the output color... and make it black to mask out
- // glow passes rendered before us.
- //
- // The shader compiler will optimize out all the other
- // code above that doesn't contribute to the alpha mask.
- Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- if ( color )
- output = new GenOp( " @.rgb = vec3(0);\r\n", color );
- }
- //****************************************************************************
- // RenderTargetZero
- //****************************************************************************
- void RenderTargetZeroGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
- {
- // Do not actually assign zero, but instead a number so close to zero it may as well be zero.
- // This will prevent a divide by zero causing an FP special on float render targets
- output = new GenOp( " @;\r\n", assignColor( new GenOp( "vec4(0.00001)" ), Material::None, NULL, mOutputTargetMask ) );
- }
- //****************************************************************************
- // HDR Output
- //****************************************************************************
- HDROutGLSL::HDROutGLSL()
- : mTorqueDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
- {
- addDependency( &mTorqueDep );
- }
- void HDROutGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Let the helper function do the work.
- Var *color = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- if ( color )
- output = new GenOp( " @ = hdrEncode( @ );\r\n", color, color );
- }
- //****************************************************************************
- // FoliageFeatureGLSL
- //****************************************************************************
- #include "T3D/fx/groundCover.h"
- FoliageFeatureGLSL::FoliageFeatureGLSL()
- : mDep(ShaderGen::smCommonShaderPath + String("/gl/foliage.glsl" ))
- {
- addDependency( &mDep );
- }
- void FoliageFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Get the input variables we need.
-
- Var *inPosition = (Var*)LangElement::find( "inPosition" );
- if ( !inPosition )
- inPosition = (Var*)LangElement::find( "position" );
-
- Var *inColor = (Var*)LangElement::find( "diffuse" );
-
- Var *inParams = (Var*)LangElement::find( "texCoord" );
-
- MultiLine *meta = new MultiLine;
-
- // Declare the normal and tangent variables since they do not exist
- // in this vert type, but we do need to set them up for others.
-
- Var *normal = (Var*)LangElement::find( "normal" );
- AssertFatal( normal, "FoliageFeatureGLSL requires vert normal!" );
-
- Var *tangent = new Var;
- tangent->setType( "vec3" );
- tangent->setName( "T" );
- LangElement *tangentDec = new DecOp( tangent );
- meta->addStatement( new GenOp( " @ = vec3(1.0,0,0);\n", tangentDec ) );
-
- // We add a float foliageFade to the OUT structure.
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *fade = connectComp->getElement( RT_TEXCOORD );
- fade->setName( "foliageFade" );
- fade->setStructName( "OUT" );
- fade->setType( "float" );
-
- // grab the eye position
- Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
- if ( !eyePos )
- {
- eyePos = new Var( "eyePosWorld", "vec3" );
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
-
- // All actual work is offloaded to this method.
- meta->addStatement( new GenOp( " foliageProcessVert( @, @, @, @, @, @ );\r\n", inPosition, inColor, inParams, normal, tangent, eyePos ) );
-
- // Assign to foliageFade. InColor.a was set to the correct value inside foliageProcessVert.
- meta->addStatement( new GenOp( " @ = @.a;\r\n", fade, inColor ) );
-
- output = meta;
- }
- void FoliageFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Find / create IN.foliageFade
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *fade = connectComp->getElement( RT_TEXCOORD );
- fade->setName( "foliageFade" );
- fade->setStructName( "IN" );
- fade->setType( "float" );
-
- // Find / create visibility
- Var *visibility = (Var*) LangElement::find( "visibility" );
- if ( !visibility )
- {
- visibility = new Var();
- visibility->setType( "float" );
- visibility->setName( "visibility" );
- visibility->uniform = true;
- visibility->constSortPos = cspPotentialPrimitive;
- }
- MultiLine *meta = new MultiLine;
- // Multiply foliageFade into visibility.
- meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
- output = meta;
- }
- void FoliageFeatureGLSL::determineFeature( Material *material, const GFXVertexFormat *vertexFormat, U32 stageNum, const FeatureType &type, const FeatureSet &features, MaterialFeatureData *outFeatureData )
- {
- // This isn't really necessary since the outFeatureData will be filtered after
- // this call.
- if ( features.hasFeature( MFT_Foliage ) )
- outFeatureData->features.addFeature( type );
- }
- ShaderFeatureConstHandles* FoliageFeatureGLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
- {
- GroundCover *gcover = dynamic_cast< GroundCover* >( userObject );
- AssertFatal( gcover != NULL, "FoliageFeatureGLSL::createConstHandles - userObject was not valid!" );
-
- GroundCoverShaderConstHandles *handles = new GroundCoverShaderConstHandles();
- handles->mGroundCover = gcover;
-
- handles->init( shader );
-
- return handles;
- }
- void ParticleNormalFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd)
- {
- MultiLine *meta = new MultiLine;
- output = meta;
-
- // Calculate normal and tangent values since we want to keep particle verts
- // as light-weight as possible
-
- Var *normal = (Var*) LangElement::find("normal");
- if(normal == NULL)
- {
- normal = new Var;
- normal->setType( "vec3" );
- normal->setName( "normal" );
-
- // These values are not accidental. It is slightly adjusted from facing straight into the
- // screen because there is a discontinuity at (0, 1, 0) for gbuffer encoding. Do not
- // cause this value to be (0, -1, 0) or interlaced normals will be discontinuous.
- // [11/23/2009 Pat]
- meta->addStatement(new GenOp(" @ = float3(0.0, -0.97, 0.14);\r\n", new DecOp(normal)));
- }
-
- Var *T = (Var*) LangElement::find( "T" );
- if(T == NULL)
- {
- T = new Var;
- T->setType( "vec3" );
- T->setName( "T" );
- meta->addStatement(new GenOp(" @ = float3(0.0, 0.0, -1.0);\r\n", new DecOp(T)));
- }
- }
- //****************************************************************************
- // ImposterVertFeatureGLSL
- //****************************************************************************
- ImposterVertFeatureGLSL::ImposterVertFeatureGLSL()
- : mDep(ShaderGen::smCommonShaderPath + String("/gl/imposter.glsl" ))
- {
- addDependency( &mDep );
- }
- void ImposterVertFeatureGLSL::processVert( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- MultiLine *meta = new MultiLine;
- output = meta;
-
- // Get the input vertex variables.
- Var *inPosition = (Var*)LangElement::find( "position" );
- Var *inMiscParams = (Var*)LangElement::find( "tcImposterParams" );
- Var *inUpVec = (Var*)LangElement::find( "tcImposterUpVec" );
- Var *inRightVec = (Var*)LangElement::find( "tcImposterRightVec" );
-
- // Get the input shader constants.
- Var *imposterLimits = new Var;
- imposterLimits->setType( "vec4" );
- imposterLimits->setName( "imposterLimits" );
- imposterLimits->uniform = true;
- imposterLimits->constSortPos = cspPotentialPrimitive;
-
- Var *imposterUVs = new Var;
- imposterUVs->setType( "vec4" );
- imposterUVs->setName( "imposterUVs" );
- imposterUVs->arraySize = 64; // See imposter.glsl
- imposterUVs->uniform = true;
- imposterUVs->constSortPos = cspPotentialPrimitive;
-
- Var *eyePos = (Var*)LangElement::find( "eyePosWorld" );
- if ( !eyePos )
- {
- eyePos = new Var( "eyePosWorld", "vec3" );
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
-
- // Declare the outputs from this feature.
- Var *outInPosition = new Var;
- outInPosition->setType( "vec3" );
- outInPosition->setName( "inPosition" );
- meta->addStatement( new GenOp( " @;\r\n", new DecOp( outInPosition ) ) );
-
- Var *outTexCoord = new Var;
- outTexCoord->setType( "vec2" );
- outTexCoord->setName( "texCoord" );
- meta->addStatement( new GenOp( " @;\r\n", new DecOp( outTexCoord ) ) );
-
- Var *outWorldToTangent = new Var;
- outWorldToTangent->setType( "float3x3" );
- outWorldToTangent->setName( "worldToTangent" );
- meta->addStatement( new GenOp( " @;\r\n", new DecOp( outWorldToTangent ) ) );
-
- // Add imposterFade to the OUT structure.
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *outFade = connectComp->getElement( RT_TEXCOORD );
- outFade->setName( "imposterFade" );
- outFade->setStructName( "OUT" );
- outFade->setType( "float" );
-
- // Assign OUT.imposterFade
- meta->addStatement( new GenOp( " @ = @.y;\r\n", outFade, inMiscParams ) );
-
- // All actual work is done in this method.
- meta->addStatement( new GenOp( " imposter_v( @.xyz, int(@.w), @.x * length(@), normalize(@), normalize(@), int(@.y), int(@.x), @.z, bool(@.w), @, @, @, @, @ );\r\n",
-
- inPosition,
- inPosition,
-
- inMiscParams,
- inRightVec,
-
- inUpVec,
- inRightVec,
-
- imposterLimits,
- imposterLimits,
- imposterLimits,
- imposterLimits,
-
- eyePos,
- imposterUVs,
-
- outInPosition,
- outTexCoord,
- outWorldToTangent ) );
-
- // Copy the position to wsPosition for use in shaders
- // down stream instead of looking for objTrans.
- Var *wsPosition = new Var;
- wsPosition->setType( "vec3" );
- wsPosition->setName( "wsPosition" );
- meta->addStatement( new GenOp( " @ = @.xyz;\r\n", new DecOp( wsPosition ), outInPosition ) );
-
- // If we new viewToTangent... its the same as the
- // world to tangent for an imposter.
- Var *viewToTangent = new Var;
- viewToTangent->setType( "float3x3" );
- viewToTangent->setName( "viewToTangent" );
- meta->addStatement( new GenOp( " @ = @;\r\n", new DecOp( viewToTangent ), outWorldToTangent ) );
- }
- void ImposterVertFeatureGLSL::processPix( Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd )
- {
- // Find / create IN.imposterFade
- ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
- Var *fade = connectComp->getElement( RT_TEXCOORD );
- fade->setName( "imposterFade" );
- fade->setStructName( "IN" );
- fade->setType( "float" );
-
- // Find / create visibility
- Var *visibility = (Var*) LangElement::find( "visibility" );
- if ( !visibility )
- {
- visibility = new Var();
- visibility->setType( "float" );
- visibility->setName( "visibility" );
- visibility->uniform = true;
- visibility->constSortPos = cspPotentialPrimitive;
- }
-
- MultiLine *meta = new MultiLine;
-
- // Multiply foliageFade into visibility.
- meta->addStatement( new GenOp( " @ *= @;\r\n", visibility, fade ) );
-
- output = meta;
- }
- void ImposterVertFeatureGLSL::determineFeature( Material *material,
- const GFXVertexFormat *vertexFormat,
- U32 stageNum,
- const FeatureType &type,
- const FeatureSet &features,
- MaterialFeatureData *outFeatureData )
- {
- if ( features.hasFeature( MFT_ImposterVert ) )
- outFeatureData->features.addFeature( MFT_ImposterVert );
- }
- //****************************************************************************
- // HardwareSkinningFeatureGLSL
- //****************************************************************************
- void HardwareSkinningFeatureGLSL::processVert(Vector<ShaderComponent*> &componentList,
- const MaterialFeatureData &fd)
- {
- MultiLine *meta = new MultiLine;
- Var *inPosition = (Var*)LangElement::find("inPosition");
- Var *inNormal = (Var*)LangElement::find("inNormal");
- if (!inPosition)
- inPosition = (Var*)LangElement::find("position");
- if (!inNormal)
- inNormal = (Var*)LangElement::find("normal");
- Var* posePos = new Var("posePos", "vec3");
- Var* poseNormal = new Var("poseNormal", "vec3");
- Var* poseMat = new Var("poseMat", "mat4x3");
- Var* poseRotMat = new Var("poseRotMat", "mat3x3");
- Var* nodeTransforms = (Var*)LangElement::find("nodeTransforms");
- if (!nodeTransforms)
- {
- nodeTransforms = new Var("nodeTransforms", "mat4x3");
- nodeTransforms->uniform = true;
- nodeTransforms->arraySize = TSShape::smMaxSkinBones;
- nodeTransforms->constSortPos = cspPrimitive;
- }
- U32 numIndices = mVertexFormat->getNumBlendIndices();
- meta->addStatement(new GenOp(" @ = vec3(0.0);\r\n", new DecOp(posePos)));
- meta->addStatement(new GenOp(" @ = vec3(0.0);\r\n", new DecOp(poseNormal)));
- meta->addStatement(new GenOp(" @;\r\n", new DecOp(poseMat)));
- meta->addStatement(new GenOp(" @;\r\n int i;\r\n", new DecOp(poseRotMat)));
- for (U32 i = 0; i<numIndices; i++)
- {
- // NOTE: To keep things simple, we assume all 4 bone indices are used in each element chunk.
- LangElement* inIndices = (Var*)LangElement::find(String::ToString("vBlendIndex%d", i));
- LangElement* inWeights = (Var*)LangElement::find(String::ToString("vBlendWeight%d", i));
- AssertFatal(inIndices && inWeights, "Something went wrong here");
- AssertFatal(poseMat && nodeTransforms && posePos && inPosition && inWeights && poseNormal && inNormal && poseRotMat, "Something went REALLY wrong here");
- meta->addStatement(new GenOp(" for (i=0; i<4; i++) {\r\n"));
- meta->addStatement(new GenOp(" int poseIdx = int(@[i]);\r\n", inIndices));
- meta->addStatement(new GenOp(" float poseWeight = @[i];\r\n", inWeights));
- meta->addStatement(new GenOp(" @ = @[poseIdx];\r\n", poseMat, nodeTransforms));
- meta->addStatement(new GenOp(" @ = mat3x3(@);\r\n", poseRotMat, poseMat));
- meta->addStatement(new GenOp(" @ += (@ * vec4(@, 1)).xyz * poseWeight;\r\n", posePos, poseMat, inPosition));
- meta->addStatement(new GenOp(" @ += ((@ * @) * poseWeight);\r\n", poseNormal, poseRotMat, inNormal));
- meta->addStatement(new GenOp(" }\r\n"));
- }
- // Assign new position and normal
- meta->addStatement(new GenOp(" @ = @;\r\n", inPosition, posePos));
- meta->addStatement(new GenOp(" @ = normalize(@);\r\n", inNormal, poseNormal));
- output = meta;
- }
- //****************************************************************************
- // ReflectionProbeFeatGLSL
- //****************************************************************************
- ReflectionProbeFeatGLSL::ReflectionProbeFeatGLSL()
- : mDep(ShaderGen::smCommonShaderPath + String("/gl/lighting.glsl"))
- {
- addDependency(&mDep);
- }
- void ReflectionProbeFeatGLSL::processVert(Vector<ShaderComponent*>& componentList,
- const MaterialFeatureData& fd)
- {
- //MultiLine* meta = new MultiLine;
- //output = meta;
- // Also output the worldToTanget transform which
- // we use to create the world space normal.
- //getOutWorldToTangent(componentList, meta, fd);
- }
- void ReflectionProbeFeatGLSL::processPix(Vector<ShaderComponent*>& componentList,
- const MaterialFeatureData& fd)
- {
- // Skip out on realtime lighting if we don't have a normal
- // or we're doing some sort of baked lighting.
- //
- // TODO: We can totally detect for this in the material
- // feature setup... we should move it out of here!
- //
- if (fd.features[MFT_LightMap] || fd.features[MFT_ToneMap] || fd.features[MFT_VertLit])
- return;
- ShaderConnector * connectComp = dynamic_cast<ShaderConnector*>(componentList[C_CONNECTOR]);
- MultiLine * meta = new MultiLine;
- // Now the wsPosition and wsView.
- Var *wsPosition = getInWsPosition(componentList);
- Var *worldToTangent = getInWorldToTangent(componentList);
- Var *wsNormal = getInWorldNormal(componentList);
- Var *wsView = getWsView(wsPosition, meta);
-
- //Reflection Probe WIP
- U32 MAX_FORWARD_PROBES = 4;
- Var * numProbes = new Var("inNumProbes", "int");
- numProbes->uniform = true;
- numProbes->constSortPos = cspPotentialPrimitive;
- Var * cubeMips = new Var("cubeMips", "float");
- cubeMips->uniform = true;
- cubeMips->constSortPos = cspPotentialPrimitive;
- Var * skylightCubemapIdx = new Var("inSkylightCubemapIdx", "float");
- skylightCubemapIdx->uniform = true;
- skylightCubemapIdx->constSortPos = cspPotentialPrimitive;
- Var* SkylightDamp = new Var("SkylightDamp", "int");
- SkylightDamp->uniform = true;
- SkylightDamp->constSortPos = cspPotentialPrimitive;
- Var * inProbePosArray = new Var("inProbePosArray", "vec4");
- inProbePosArray->arraySize = MAX_FORWARD_PROBES;
- inProbePosArray->uniform = true;
- inProbePosArray->constSortPos = cspPotentialPrimitive;
- Var * inRefPosArray = new Var("inRefPosArray", "vec4");
- inRefPosArray->arraySize = MAX_FORWARD_PROBES;
- inRefPosArray->uniform = true;
- inRefPosArray->constSortPos = cspPotentialPrimitive;
- Var * refScaleArray = new Var("inRefScaleArray", "vec4");
- refScaleArray->arraySize = MAX_FORWARD_PROBES;
- refScaleArray->uniform = true;
- refScaleArray->constSortPos = cspPotentialPrimitive;
- Var * probeConfigData = new Var("inProbeConfigDataArray", "vec4");
- probeConfigData->arraySize = MAX_FORWARD_PROBES;
- probeConfigData->uniform = true;
- probeConfigData->constSortPos = cspPotentialPrimitive;
- Var * worldToObjArray = new Var("inWorldToObjArray", "mat4");
- worldToObjArray->arraySize = MAX_FORWARD_PROBES;
- worldToObjArray->uniform = true;
- worldToObjArray->constSortPos = cspPotentialPrimitive;
- // create texture var
- Var* BRDFTexture = new Var;
- BRDFTexture->setType("sampler2D");
- BRDFTexture->setName("BRDFTexture");
- BRDFTexture->uniform = true;
- BRDFTexture->sampler = true;
- BRDFTexture->constNum = Var::getTexUnitNum(); // used as texture unit num here
-
- Var * specularCubemapAR = new Var("SpecularCubemapAR", "samplerCubeArray");
- specularCubemapAR->uniform = true;
- specularCubemapAR->sampler = true;
- specularCubemapAR->constNum = Var::getTexUnitNum();
- Var * irradianceCubemapAR = new Var("IrradianceCubemapAR", "samplerCubeArray");
- irradianceCubemapAR->uniform = true;
- irradianceCubemapAR->sampler = true;
- irradianceCubemapAR->constNum = Var::getTexUnitNum();
- // create texture var
- Var* WetnessTexture = new Var;
- WetnessTexture->setType("sampler2D");
- WetnessTexture->setName("WetnessTexture");
- WetnessTexture->uniform = true;
- WetnessTexture->sampler = true;
- WetnessTexture->constNum = Var::getTexUnitNum(); // used as texture unit num here
- Var* surface = getSurface(componentList, meta, fd);
- if (!surface)
- {
- Con::errorf("ShaderGen::ReflectionProbeFeatGLSL() - failed to generate surface!");
- return;
- }
- Var *curColor = (Var *)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
- //Reflection vec
- Var *ibl = (Var *)LangElement::find("ibl");
- if (!ibl)
- {
- ibl = new Var("ibl", "float3");
- }
- Var* eyePos = (Var*)LangElement::find("eyePosWorld");
- if (!eyePos)
- {
- eyePos = new Var;
- eyePos->setType("vec3");
- eyePos->setName("eyePosWorld");
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
- Var* accumTime = (Var*)LangElement::find("accumTime");
- if (!accumTime)
- {
- accumTime = new Var("accumTime", "float");
- accumTime->uniform = true;
- accumTime->constSortPos = cspPass;
- }
- Var* dampness = (Var*)LangElement::find("dampness");
- if (!dampness)
- {
- dampness = new Var("dampness", "float");
- dampness->uniform = true;
- dampness->constSortPos = cspPass;
- }
-
- //Reflection vec
- String computeForwardProbes = String(" @ = computeForwardProbes(@,@,@,@,@,@,@,@,@,\r\n\t\t");
- computeForwardProbes += String("@,@,@,@,@,@,\r\n\t\t");
- computeForwardProbes += String("@,@).rgb; \r\n");
- meta->addStatement(new GenOp(computeForwardProbes.c_str(), new DecOp(ibl), surface, cubeMips, numProbes, worldToObjArray, probeConfigData, inProbePosArray, refScaleArray, inRefPosArray, eyePos,
- skylightCubemapIdx, SkylightDamp, BRDFTexture, WetnessTexture, accumTime, dampness,
- irradianceCubemapAR, specularCubemapAR));
- Var *ambient = (Var *)LangElement::find("ambient");
- if (!ambient)
- {
- ambient = new Var("ambient", "vec3");
- eyePos->uniform = true;
- eyePos->constSortPos = cspPass;
- }
- meta->addStatement(new GenOp(" @.rgb *= @.rgb;\r\n", ibl, ambient));
- meta->addStatement(new GenOp(" @.rgb = @.rgb;\r\n", curColor, ibl));
- output = meta;
- }
- ShaderFeature::Resources ReflectionProbeFeatGLSL::getResources(const MaterialFeatureData& fd)
- {
- Resources res;
- res.numTex = 4;
- res.numTexReg = 4;
- return res;
- }
- void ReflectionProbeFeatGLSL::setTexData(Material::StageData& stageDat,
- const MaterialFeatureData& stageFeatures,
- RenderPassData& passData,
- U32& texIndex)
- {
- if (stageFeatures.features[MFT_ReflectionProbes])
- {
- passData.mSamplerNames[texIndex] = "BRDFTexture";
- passData.mTexType[texIndex++] = Material::Standard;
- // assuming here that it is a scenegraph cubemap
- passData.mSamplerNames[texIndex] = "SpecularCubemapAR";
- passData.mTexType[texIndex++] = Material::SGCube;
- passData.mSamplerNames[texIndex] = "IrradianceCubemapAR";
- passData.mTexType[texIndex++] = Material::SGCube;
- passData.mSamplerNames[texIndex] = "WetnessTexture";
- passData.mTexType[texIndex++] = Material::Standard;
- }
- }
|