nodes.cpp 189 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "render/colorspace.h"
  17. #include "render/film.h"
  18. #include "render/image.h"
  19. #include "render/integrator.h"
  20. #include "render/light.h"
  21. #include "render/nodes.h"
  22. #include "render/scene.h"
  23. #include "render/svm.h"
  24. #include "kernel/svm/svm_color_util.h"
  25. #include "kernel/svm/svm_ramp_util.h"
  26. #include "kernel/svm/svm_math_util.h"
  27. #include "render/osl.h"
  28. #include "render/constant_fold.h"
  29. #include "util/util_sky_model.h"
  30. #include "util/util_foreach.h"
  31. #include "util/util_logging.h"
  32. #include "util/util_transform.h"
  33. CCL_NAMESPACE_BEGIN
  34. /* Texture Mapping */
  35. #define TEXTURE_MAPPING_DEFINE(TextureNode) \
  36. SOCKET_POINT(tex_mapping.translation, "Translation", make_float3(0.0f, 0.0f, 0.0f)); \
  37. SOCKET_VECTOR(tex_mapping.rotation, "Rotation", make_float3(0.0f, 0.0f, 0.0f)); \
  38. SOCKET_VECTOR(tex_mapping.scale, "Scale", make_float3(1.0f, 1.0f, 1.0f)); \
  39. \
  40. SOCKET_VECTOR(tex_mapping.min, "Min", make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX)); \
  41. SOCKET_VECTOR(tex_mapping.max, "Max", make_float3(FLT_MAX, FLT_MAX, FLT_MAX)); \
  42. SOCKET_BOOLEAN(tex_mapping.use_minmax, "Use Min Max", false); \
  43. \
  44. static NodeEnum mapping_axis_enum; \
  45. mapping_axis_enum.insert("none", TextureMapping::NONE); \
  46. mapping_axis_enum.insert("x", TextureMapping::X); \
  47. mapping_axis_enum.insert("y", TextureMapping::Y); \
  48. mapping_axis_enum.insert("z", TextureMapping::Z); \
  49. SOCKET_ENUM(tex_mapping.x_mapping, "x_mapping", mapping_axis_enum, TextureMapping::X); \
  50. SOCKET_ENUM(tex_mapping.y_mapping, "y_mapping", mapping_axis_enum, TextureMapping::Y); \
  51. SOCKET_ENUM(tex_mapping.z_mapping, "z_mapping", mapping_axis_enum, TextureMapping::Z); \
  52. \
  53. static NodeEnum mapping_type_enum; \
  54. mapping_type_enum.insert("point", TextureMapping::POINT); \
  55. mapping_type_enum.insert("texture", TextureMapping::TEXTURE); \
  56. mapping_type_enum.insert("vector", TextureMapping::VECTOR); \
  57. mapping_type_enum.insert("normal", TextureMapping::NORMAL); \
  58. SOCKET_ENUM(tex_mapping.type, "Type", mapping_type_enum, TextureMapping::TEXTURE); \
  59. \
  60. static NodeEnum mapping_projection_enum; \
  61. mapping_projection_enum.insert("flat", TextureMapping::FLAT); \
  62. mapping_projection_enum.insert("cube", TextureMapping::CUBE); \
  63. mapping_projection_enum.insert("tube", TextureMapping::TUBE); \
  64. mapping_projection_enum.insert("sphere", TextureMapping::SPHERE); \
  65. SOCKET_ENUM(tex_mapping.projection, "Projection", mapping_projection_enum, TextureMapping::FLAT);
  66. TextureMapping::TextureMapping()
  67. {
  68. }
  69. Transform TextureMapping::compute_transform()
  70. {
  71. Transform mmat = transform_scale(make_float3(0.0f, 0.0f, 0.0f));
  72. if (x_mapping != NONE)
  73. mmat[0][x_mapping - 1] = 1.0f;
  74. if (y_mapping != NONE)
  75. mmat[1][y_mapping - 1] = 1.0f;
  76. if (z_mapping != NONE)
  77. mmat[2][z_mapping - 1] = 1.0f;
  78. float3 scale_clamped = scale;
  79. if (type == TEXTURE || type == NORMAL) {
  80. /* keep matrix invertible */
  81. if (fabsf(scale.x) < 1e-5f)
  82. scale_clamped.x = signf(scale.x) * 1e-5f;
  83. if (fabsf(scale.y) < 1e-5f)
  84. scale_clamped.y = signf(scale.y) * 1e-5f;
  85. if (fabsf(scale.z) < 1e-5f)
  86. scale_clamped.z = signf(scale.z) * 1e-5f;
  87. }
  88. Transform smat = transform_scale(scale_clamped);
  89. Transform rmat = transform_euler(rotation);
  90. Transform tmat = transform_translate(translation);
  91. Transform mat;
  92. switch (type) {
  93. case TEXTURE:
  94. /* inverse transform on texture coordinate gives
  95. * forward transform on texture */
  96. mat = tmat * rmat * smat;
  97. mat = transform_inverse(mat);
  98. break;
  99. case POINT:
  100. /* full transform */
  101. mat = tmat * rmat * smat;
  102. break;
  103. case VECTOR:
  104. /* no translation for vectors */
  105. mat = rmat * smat;
  106. break;
  107. case NORMAL:
  108. /* no translation for normals, and inverse transpose */
  109. mat = rmat * smat;
  110. mat = transform_transposed_inverse(mat);
  111. break;
  112. }
  113. /* projection last */
  114. mat = mat * mmat;
  115. return mat;
  116. }
  117. bool TextureMapping::skip()
  118. {
  119. if (translation != make_float3(0.0f, 0.0f, 0.0f))
  120. return false;
  121. if (rotation != make_float3(0.0f, 0.0f, 0.0f))
  122. return false;
  123. if (scale != make_float3(1.0f, 1.0f, 1.0f))
  124. return false;
  125. if (x_mapping != X || y_mapping != Y || z_mapping != Z)
  126. return false;
  127. if (use_minmax)
  128. return false;
  129. return true;
  130. }
  131. void TextureMapping::compile(SVMCompiler &compiler, int offset_in, int offset_out)
  132. {
  133. compiler.add_node(NODE_MAPPING, offset_in, offset_out);
  134. Transform tfm = compute_transform();
  135. compiler.add_node(tfm.x);
  136. compiler.add_node(tfm.y);
  137. compiler.add_node(tfm.z);
  138. if (use_minmax) {
  139. compiler.add_node(NODE_MIN_MAX, offset_out, offset_out);
  140. compiler.add_node(float3_to_float4(min));
  141. compiler.add_node(float3_to_float4(max));
  142. }
  143. if (type == NORMAL) {
  144. compiler.add_node(NODE_VECTOR_MATH, NODE_VECTOR_MATH_NORMALIZE, offset_out, offset_out);
  145. compiler.add_node(NODE_VECTOR_MATH, SVM_STACK_INVALID, offset_out);
  146. }
  147. }
  148. /* Convenience function for texture nodes, allocating stack space to output
  149. * a modified vector and returning its offset */
  150. int TextureMapping::compile_begin(SVMCompiler &compiler, ShaderInput *vector_in)
  151. {
  152. if (!skip()) {
  153. int offset_in = compiler.stack_assign(vector_in);
  154. int offset_out = compiler.stack_find_offset(SocketType::VECTOR);
  155. compile(compiler, offset_in, offset_out);
  156. return offset_out;
  157. }
  158. return compiler.stack_assign(vector_in);
  159. }
  160. void TextureMapping::compile_end(SVMCompiler &compiler, ShaderInput *vector_in, int vector_offset)
  161. {
  162. if (!skip()) {
  163. compiler.stack_clear_offset(vector_in->type(), vector_offset);
  164. }
  165. }
  166. void TextureMapping::compile(OSLCompiler &compiler)
  167. {
  168. if (!skip()) {
  169. compiler.parameter("mapping", compute_transform());
  170. compiler.parameter("use_mapping", 1);
  171. }
  172. }
  173. /* Image Texture */
  174. NODE_DEFINE(ImageTextureNode)
  175. {
  176. NodeType *type = NodeType::add("image_texture", create, NodeType::SHADER);
  177. TEXTURE_MAPPING_DEFINE(ImageTextureNode);
  178. SOCKET_STRING(filename, "Filename", ustring());
  179. SOCKET_STRING(colorspace, "Colorspace", u_colorspace_auto);
  180. static NodeEnum alpha_type_enum;
  181. alpha_type_enum.insert("auto", IMAGE_ALPHA_AUTO);
  182. alpha_type_enum.insert("unassociated", IMAGE_ALPHA_UNASSOCIATED);
  183. alpha_type_enum.insert("associated", IMAGE_ALPHA_ASSOCIATED);
  184. alpha_type_enum.insert("channel_packed", IMAGE_ALPHA_CHANNEL_PACKED);
  185. alpha_type_enum.insert("ignore", IMAGE_ALPHA_IGNORE);
  186. SOCKET_ENUM(alpha_type, "Alpha Type", alpha_type_enum, IMAGE_ALPHA_AUTO);
  187. static NodeEnum interpolation_enum;
  188. interpolation_enum.insert("closest", INTERPOLATION_CLOSEST);
  189. interpolation_enum.insert("linear", INTERPOLATION_LINEAR);
  190. interpolation_enum.insert("cubic", INTERPOLATION_CUBIC);
  191. interpolation_enum.insert("smart", INTERPOLATION_SMART);
  192. SOCKET_ENUM(interpolation, "Interpolation", interpolation_enum, INTERPOLATION_LINEAR);
  193. static NodeEnum extension_enum;
  194. extension_enum.insert("periodic", EXTENSION_REPEAT);
  195. extension_enum.insert("clamp", EXTENSION_EXTEND);
  196. extension_enum.insert("black", EXTENSION_CLIP);
  197. SOCKET_ENUM(extension, "Extension", extension_enum, EXTENSION_REPEAT);
  198. static NodeEnum projection_enum;
  199. projection_enum.insert("flat", NODE_IMAGE_PROJ_FLAT);
  200. projection_enum.insert("box", NODE_IMAGE_PROJ_BOX);
  201. projection_enum.insert("sphere", NODE_IMAGE_PROJ_SPHERE);
  202. projection_enum.insert("tube", NODE_IMAGE_PROJ_TUBE);
  203. SOCKET_ENUM(projection, "Projection", projection_enum, NODE_IMAGE_PROJ_FLAT);
  204. SOCKET_FLOAT(projection_blend, "Projection Blend", 0.0f);
  205. SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_UV);
  206. SOCKET_OUT_COLOR(color, "Color");
  207. SOCKET_OUT_FLOAT(alpha, "Alpha");
  208. return type;
  209. }
  210. ImageTextureNode::ImageTextureNode() : ImageSlotTextureNode(node_type)
  211. {
  212. image_manager = NULL;
  213. slot = -1;
  214. is_float = -1;
  215. compress_as_srgb = false;
  216. colorspace = u_colorspace_raw;
  217. builtin_data = NULL;
  218. animated = false;
  219. }
  220. ImageTextureNode::~ImageTextureNode()
  221. {
  222. if (image_manager) {
  223. image_manager->remove_image(
  224. filename.string(), builtin_data, interpolation, extension, alpha_type, colorspace);
  225. }
  226. }
  227. ShaderNode *ImageTextureNode::clone() const
  228. {
  229. /* Increase image user count for new node. */
  230. if (slot != -1) {
  231. image_manager->add_image_user(slot);
  232. }
  233. return new ImageTextureNode(*this);
  234. }
  235. void ImageTextureNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  236. {
  237. #ifdef WITH_PTEX
  238. /* todo: avoid loading other texture coordinates when using ptex,
  239. * and hide texture coordinate socket in the UI */
  240. if (shader->has_surface && string_endswith(filename, ".ptx")) {
  241. /* ptex */
  242. attributes->add(ATTR_STD_PTEX_FACE_ID);
  243. attributes->add(ATTR_STD_PTEX_UV);
  244. }
  245. #endif
  246. ShaderNode::attributes(shader, attributes);
  247. }
  248. void ImageTextureNode::compile(SVMCompiler &compiler)
  249. {
  250. ShaderInput *vector_in = input("Vector");
  251. ShaderOutput *color_out = output("Color");
  252. ShaderOutput *alpha_out = output("Alpha");
  253. image_manager = compiler.image_manager;
  254. if (is_float == -1) {
  255. ImageMetaData metadata;
  256. slot = image_manager->add_image(filename.string(),
  257. builtin_data,
  258. animated,
  259. 0,
  260. interpolation,
  261. extension,
  262. alpha_type,
  263. colorspace,
  264. metadata);
  265. is_float = metadata.is_float;
  266. compress_as_srgb = metadata.compress_as_srgb;
  267. known_colorspace = metadata.colorspace;
  268. }
  269. if (slot != -1) {
  270. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  271. uint flags = 0;
  272. if (compress_as_srgb) {
  273. flags |= NODE_IMAGE_COMPRESS_AS_SRGB;
  274. }
  275. if (!alpha_out->links.empty()) {
  276. const bool unassociate_alpha = !(ColorSpaceManager::colorspace_is_data(colorspace) ||
  277. alpha_type == IMAGE_ALPHA_CHANNEL_PACKED ||
  278. alpha_type == IMAGE_ALPHA_IGNORE);
  279. if (unassociate_alpha) {
  280. flags |= NODE_IMAGE_ALPHA_UNASSOCIATE;
  281. }
  282. }
  283. if (projection != NODE_IMAGE_PROJ_BOX) {
  284. compiler.add_node(NODE_TEX_IMAGE,
  285. slot,
  286. compiler.encode_uchar4(vector_offset,
  287. compiler.stack_assign_if_linked(color_out),
  288. compiler.stack_assign_if_linked(alpha_out),
  289. flags),
  290. projection);
  291. }
  292. else {
  293. compiler.add_node(NODE_TEX_IMAGE_BOX,
  294. slot,
  295. compiler.encode_uchar4(vector_offset,
  296. compiler.stack_assign_if_linked(color_out),
  297. compiler.stack_assign_if_linked(alpha_out),
  298. flags),
  299. __float_as_int(projection_blend));
  300. }
  301. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  302. }
  303. else {
  304. /* image not found */
  305. if (!color_out->links.empty()) {
  306. compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
  307. compiler.add_node(
  308. NODE_VALUE_V,
  309. make_float3(TEX_IMAGE_MISSING_R, TEX_IMAGE_MISSING_G, TEX_IMAGE_MISSING_B));
  310. }
  311. if (!alpha_out->links.empty())
  312. compiler.add_node(
  313. NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), compiler.stack_assign(alpha_out));
  314. }
  315. }
  316. void ImageTextureNode::compile(OSLCompiler &compiler)
  317. {
  318. ShaderOutput *alpha_out = output("Alpha");
  319. tex_mapping.compile(compiler);
  320. image_manager = compiler.image_manager;
  321. if (is_float == -1) {
  322. ImageMetaData metadata;
  323. if (builtin_data == NULL) {
  324. image_manager->get_image_metadata(filename.string(), NULL, colorspace, metadata);
  325. }
  326. else {
  327. slot = image_manager->add_image(filename.string(),
  328. builtin_data,
  329. animated,
  330. 0,
  331. interpolation,
  332. extension,
  333. alpha_type,
  334. colorspace,
  335. metadata);
  336. }
  337. is_float = metadata.is_float;
  338. compress_as_srgb = metadata.compress_as_srgb;
  339. known_colorspace = metadata.colorspace;
  340. }
  341. if (slot == -1) {
  342. compiler.parameter_texture(
  343. "filename", filename, compress_as_srgb ? u_colorspace_raw : known_colorspace);
  344. }
  345. else {
  346. compiler.parameter_texture("filename", slot);
  347. }
  348. const bool unassociate_alpha = !(ColorSpaceManager::colorspace_is_data(colorspace) ||
  349. alpha_type == IMAGE_ALPHA_CHANNEL_PACKED ||
  350. alpha_type == IMAGE_ALPHA_IGNORE);
  351. compiler.parameter(this, "projection");
  352. compiler.parameter(this, "projection_blend");
  353. compiler.parameter("compress_as_srgb", compress_as_srgb);
  354. compiler.parameter("ignore_alpha", alpha_type == IMAGE_ALPHA_IGNORE);
  355. compiler.parameter("unassociate_alpha", !alpha_out->links.empty() && unassociate_alpha);
  356. compiler.parameter("is_float", is_float);
  357. compiler.parameter(this, "interpolation");
  358. compiler.parameter(this, "extension");
  359. compiler.add(this, "node_image_texture");
  360. }
  361. /* Environment Texture */
  362. NODE_DEFINE(EnvironmentTextureNode)
  363. {
  364. NodeType *type = NodeType::add("environment_texture", create, NodeType::SHADER);
  365. TEXTURE_MAPPING_DEFINE(EnvironmentTextureNode);
  366. SOCKET_STRING(filename, "Filename", ustring());
  367. SOCKET_STRING(colorspace, "Colorspace", u_colorspace_auto);
  368. static NodeEnum alpha_type_enum;
  369. alpha_type_enum.insert("auto", IMAGE_ALPHA_AUTO);
  370. alpha_type_enum.insert("unassociated", IMAGE_ALPHA_UNASSOCIATED);
  371. alpha_type_enum.insert("associated", IMAGE_ALPHA_ASSOCIATED);
  372. alpha_type_enum.insert("channel_packed", IMAGE_ALPHA_CHANNEL_PACKED);
  373. alpha_type_enum.insert("ignore", IMAGE_ALPHA_IGNORE);
  374. SOCKET_ENUM(alpha_type, "Alpha Type", alpha_type_enum, IMAGE_ALPHA_AUTO);
  375. static NodeEnum interpolation_enum;
  376. interpolation_enum.insert("closest", INTERPOLATION_CLOSEST);
  377. interpolation_enum.insert("linear", INTERPOLATION_LINEAR);
  378. interpolation_enum.insert("cubic", INTERPOLATION_CUBIC);
  379. interpolation_enum.insert("smart", INTERPOLATION_SMART);
  380. SOCKET_ENUM(interpolation, "Interpolation", interpolation_enum, INTERPOLATION_LINEAR);
  381. static NodeEnum projection_enum;
  382. projection_enum.insert("equirectangular", NODE_ENVIRONMENT_EQUIRECTANGULAR);
  383. projection_enum.insert("mirror_ball", NODE_ENVIRONMENT_MIRROR_BALL);
  384. SOCKET_ENUM(projection, "Projection", projection_enum, NODE_ENVIRONMENT_EQUIRECTANGULAR);
  385. SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_POSITION);
  386. SOCKET_OUT_COLOR(color, "Color");
  387. SOCKET_OUT_FLOAT(alpha, "Alpha");
  388. return type;
  389. }
  390. EnvironmentTextureNode::EnvironmentTextureNode() : ImageSlotTextureNode(node_type)
  391. {
  392. image_manager = NULL;
  393. slot = -1;
  394. is_float = -1;
  395. compress_as_srgb = false;
  396. colorspace = u_colorspace_raw;
  397. builtin_data = NULL;
  398. animated = false;
  399. }
  400. EnvironmentTextureNode::~EnvironmentTextureNode()
  401. {
  402. if (image_manager) {
  403. image_manager->remove_image(
  404. filename.string(), builtin_data, interpolation, EXTENSION_REPEAT, alpha_type, colorspace);
  405. }
  406. }
  407. ShaderNode *EnvironmentTextureNode::clone() const
  408. {
  409. /* Increase image user count for new node. */
  410. if (slot != -1) {
  411. image_manager->add_image_user(slot);
  412. }
  413. return new EnvironmentTextureNode(*this);
  414. }
  415. void EnvironmentTextureNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  416. {
  417. #ifdef WITH_PTEX
  418. if (shader->has_surface && string_endswith(filename, ".ptx")) {
  419. /* ptex */
  420. attributes->add(ATTR_STD_PTEX_FACE_ID);
  421. attributes->add(ATTR_STD_PTEX_UV);
  422. }
  423. #endif
  424. ShaderNode::attributes(shader, attributes);
  425. }
  426. void EnvironmentTextureNode::compile(SVMCompiler &compiler)
  427. {
  428. ShaderInput *vector_in = input("Vector");
  429. ShaderOutput *color_out = output("Color");
  430. ShaderOutput *alpha_out = output("Alpha");
  431. image_manager = compiler.image_manager;
  432. if (slot == -1) {
  433. ImageMetaData metadata;
  434. slot = image_manager->add_image(filename.string(),
  435. builtin_data,
  436. animated,
  437. 0,
  438. interpolation,
  439. EXTENSION_REPEAT,
  440. alpha_type,
  441. colorspace,
  442. metadata);
  443. is_float = metadata.is_float;
  444. compress_as_srgb = metadata.compress_as_srgb;
  445. known_colorspace = metadata.colorspace;
  446. }
  447. if (slot != -1) {
  448. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  449. uint flags = 0;
  450. if (compress_as_srgb) {
  451. flags |= NODE_IMAGE_COMPRESS_AS_SRGB;
  452. }
  453. compiler.add_node(NODE_TEX_ENVIRONMENT,
  454. slot,
  455. compiler.encode_uchar4(vector_offset,
  456. compiler.stack_assign_if_linked(color_out),
  457. compiler.stack_assign_if_linked(alpha_out),
  458. flags),
  459. projection);
  460. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  461. }
  462. else {
  463. /* image not found */
  464. if (!color_out->links.empty()) {
  465. compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
  466. compiler.add_node(
  467. NODE_VALUE_V,
  468. make_float3(TEX_IMAGE_MISSING_R, TEX_IMAGE_MISSING_G, TEX_IMAGE_MISSING_B));
  469. }
  470. if (!alpha_out->links.empty())
  471. compiler.add_node(
  472. NODE_VALUE_F, __float_as_int(TEX_IMAGE_MISSING_A), compiler.stack_assign(alpha_out));
  473. }
  474. }
  475. void EnvironmentTextureNode::compile(OSLCompiler &compiler)
  476. {
  477. tex_mapping.compile(compiler);
  478. /* See comments in ImageTextureNode::compile about support
  479. * of builtin images.
  480. */
  481. image_manager = compiler.image_manager;
  482. if (is_float == -1) {
  483. ImageMetaData metadata;
  484. if (builtin_data == NULL) {
  485. image_manager->get_image_metadata(filename.string(), NULL, colorspace, metadata);
  486. }
  487. else {
  488. slot = image_manager->add_image(filename.string(),
  489. builtin_data,
  490. animated,
  491. 0,
  492. interpolation,
  493. EXTENSION_REPEAT,
  494. alpha_type,
  495. colorspace,
  496. metadata);
  497. }
  498. is_float = metadata.is_float;
  499. compress_as_srgb = metadata.compress_as_srgb;
  500. known_colorspace = metadata.colorspace;
  501. }
  502. if (slot == -1) {
  503. compiler.parameter_texture(
  504. "filename", filename, compress_as_srgb ? u_colorspace_raw : known_colorspace);
  505. }
  506. else {
  507. compiler.parameter_texture("filename", slot);
  508. }
  509. compiler.parameter(this, "projection");
  510. compiler.parameter(this, "interpolation");
  511. compiler.parameter("compress_as_srgb", compress_as_srgb);
  512. compiler.parameter("ignore_alpha", alpha_type == IMAGE_ALPHA_IGNORE);
  513. compiler.parameter("is_float", is_float);
  514. compiler.add(this, "node_environment_texture");
  515. }
  516. /* Sky Texture */
  517. static float2 sky_spherical_coordinates(float3 dir)
  518. {
  519. return make_float2(acosf(dir.z), atan2f(dir.x, dir.y));
  520. }
  521. typedef struct SunSky {
  522. /* sun direction in spherical and cartesian */
  523. float theta, phi;
  524. /* Parameter */
  525. float radiance_x, radiance_y, radiance_z;
  526. float config_x[9], config_y[9], config_z[9];
  527. } SunSky;
  528. /* Preetham model */
  529. static float sky_perez_function(float lam[6], float theta, float gamma)
  530. {
  531. return (1.0f + lam[0] * expf(lam[1] / cosf(theta))) *
  532. (1.0f + lam[2] * expf(lam[3] * gamma) + lam[4] * cosf(gamma) * cosf(gamma));
  533. }
  534. static void sky_texture_precompute_old(SunSky *sunsky, float3 dir, float turbidity)
  535. {
  536. /*
  537. * We re-use the SunSky struct of the new model, to avoid extra variables
  538. * zenith_Y/x/y is now radiance_x/y/z
  539. * perez_Y/x/y is now config_x/y/z
  540. */
  541. float2 spherical = sky_spherical_coordinates(dir);
  542. float theta = spherical.x;
  543. float phi = spherical.y;
  544. sunsky->theta = theta;
  545. sunsky->phi = phi;
  546. float theta2 = theta * theta;
  547. float theta3 = theta2 * theta;
  548. float T = turbidity;
  549. float T2 = T * T;
  550. float chi = (4.0f / 9.0f - T / 120.0f) * (M_PI_F - 2.0f * theta);
  551. sunsky->radiance_x = (4.0453f * T - 4.9710f) * tanf(chi) - 0.2155f * T + 2.4192f;
  552. sunsky->radiance_x *= 0.06f;
  553. sunsky->radiance_y = (0.00166f * theta3 - 0.00375f * theta2 + 0.00209f * theta) * T2 +
  554. (-0.02903f * theta3 + 0.06377f * theta2 - 0.03202f * theta + 0.00394f) * T +
  555. (0.11693f * theta3 - 0.21196f * theta2 + 0.06052f * theta + 0.25886f);
  556. sunsky->radiance_z = (0.00275f * theta3 - 0.00610f * theta2 + 0.00317f * theta) * T2 +
  557. (-0.04214f * theta3 + 0.08970f * theta2 - 0.04153f * theta + 0.00516f) * T +
  558. (0.15346f * theta3 - 0.26756f * theta2 + 0.06670f * theta + 0.26688f);
  559. sunsky->config_x[0] = (0.1787f * T - 1.4630f);
  560. sunsky->config_x[1] = (-0.3554f * T + 0.4275f);
  561. sunsky->config_x[2] = (-0.0227f * T + 5.3251f);
  562. sunsky->config_x[3] = (0.1206f * T - 2.5771f);
  563. sunsky->config_x[4] = (-0.0670f * T + 0.3703f);
  564. sunsky->config_y[0] = (-0.0193f * T - 0.2592f);
  565. sunsky->config_y[1] = (-0.0665f * T + 0.0008f);
  566. sunsky->config_y[2] = (-0.0004f * T + 0.2125f);
  567. sunsky->config_y[3] = (-0.0641f * T - 0.8989f);
  568. sunsky->config_y[4] = (-0.0033f * T + 0.0452f);
  569. sunsky->config_z[0] = (-0.0167f * T - 0.2608f);
  570. sunsky->config_z[1] = (-0.0950f * T + 0.0092f);
  571. sunsky->config_z[2] = (-0.0079f * T + 0.2102f);
  572. sunsky->config_z[3] = (-0.0441f * T - 1.6537f);
  573. sunsky->config_z[4] = (-0.0109f * T + 0.0529f);
  574. /* unused for old sky model */
  575. for (int i = 5; i < 9; i++) {
  576. sunsky->config_x[i] = 0.0f;
  577. sunsky->config_y[i] = 0.0f;
  578. sunsky->config_z[i] = 0.0f;
  579. }
  580. sunsky->radiance_x /= sky_perez_function(sunsky->config_x, 0, theta);
  581. sunsky->radiance_y /= sky_perez_function(sunsky->config_y, 0, theta);
  582. sunsky->radiance_z /= sky_perez_function(sunsky->config_z, 0, theta);
  583. }
  584. /* Hosek / Wilkie */
  585. static void sky_texture_precompute_new(SunSky *sunsky,
  586. float3 dir,
  587. float turbidity,
  588. float ground_albedo)
  589. {
  590. /* Calculate Sun Direction and save coordinates */
  591. float2 spherical = sky_spherical_coordinates(dir);
  592. float theta = spherical.x;
  593. float phi = spherical.y;
  594. /* Clamp Turbidity */
  595. turbidity = clamp(turbidity, 0.0f, 10.0f);
  596. /* Clamp to Horizon */
  597. theta = clamp(theta, 0.0f, M_PI_2_F);
  598. sunsky->theta = theta;
  599. sunsky->phi = phi;
  600. float solarElevation = M_PI_2_F - theta;
  601. /* Initialize Sky Model */
  602. ArHosekSkyModelState *sky_state;
  603. sky_state = arhosek_xyz_skymodelstate_alloc_init(
  604. (double)turbidity, (double)ground_albedo, (double)solarElevation);
  605. /* Copy values from sky_state to SunSky */
  606. for (int i = 0; i < 9; ++i) {
  607. sunsky->config_x[i] = (float)sky_state->configs[0][i];
  608. sunsky->config_y[i] = (float)sky_state->configs[1][i];
  609. sunsky->config_z[i] = (float)sky_state->configs[2][i];
  610. }
  611. sunsky->radiance_x = (float)sky_state->radiances[0];
  612. sunsky->radiance_y = (float)sky_state->radiances[1];
  613. sunsky->radiance_z = (float)sky_state->radiances[2];
  614. /* Free sky_state */
  615. arhosekskymodelstate_free(sky_state);
  616. }
  617. NODE_DEFINE(SkyTextureNode)
  618. {
  619. NodeType *type = NodeType::add("sky_texture", create, NodeType::SHADER);
  620. TEXTURE_MAPPING_DEFINE(SkyTextureNode);
  621. static NodeEnum type_enum;
  622. type_enum.insert("preetham", NODE_SKY_OLD);
  623. type_enum.insert("hosek_wilkie", NODE_SKY_NEW);
  624. SOCKET_ENUM(type, "Type", type_enum, NODE_SKY_NEW);
  625. SOCKET_VECTOR(sun_direction, "Sun Direction", make_float3(0.0f, 0.0f, 1.0f));
  626. SOCKET_FLOAT(turbidity, "Turbidity", 2.2f);
  627. SOCKET_FLOAT(ground_albedo, "Ground Albedo", 0.3f);
  628. SOCKET_IN_POINT(
  629. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  630. SOCKET_OUT_COLOR(color, "Color");
  631. return type;
  632. }
  633. SkyTextureNode::SkyTextureNode() : TextureNode(node_type)
  634. {
  635. }
  636. void SkyTextureNode::compile(SVMCompiler &compiler)
  637. {
  638. ShaderInput *vector_in = input("Vector");
  639. ShaderOutput *color_out = output("Color");
  640. SunSky sunsky;
  641. if (type == NODE_SKY_OLD)
  642. sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
  643. else if (type == NODE_SKY_NEW)
  644. sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
  645. else
  646. assert(false);
  647. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  648. compiler.stack_assign(color_out);
  649. compiler.add_node(NODE_TEX_SKY, vector_offset, compiler.stack_assign(color_out), type);
  650. compiler.add_node(__float_as_uint(sunsky.phi),
  651. __float_as_uint(sunsky.theta),
  652. __float_as_uint(sunsky.radiance_x),
  653. __float_as_uint(sunsky.radiance_y));
  654. compiler.add_node(__float_as_uint(sunsky.radiance_z),
  655. __float_as_uint(sunsky.config_x[0]),
  656. __float_as_uint(sunsky.config_x[1]),
  657. __float_as_uint(sunsky.config_x[2]));
  658. compiler.add_node(__float_as_uint(sunsky.config_x[3]),
  659. __float_as_uint(sunsky.config_x[4]),
  660. __float_as_uint(sunsky.config_x[5]),
  661. __float_as_uint(sunsky.config_x[6]));
  662. compiler.add_node(__float_as_uint(sunsky.config_x[7]),
  663. __float_as_uint(sunsky.config_x[8]),
  664. __float_as_uint(sunsky.config_y[0]),
  665. __float_as_uint(sunsky.config_y[1]));
  666. compiler.add_node(__float_as_uint(sunsky.config_y[2]),
  667. __float_as_uint(sunsky.config_y[3]),
  668. __float_as_uint(sunsky.config_y[4]),
  669. __float_as_uint(sunsky.config_y[5]));
  670. compiler.add_node(__float_as_uint(sunsky.config_y[6]),
  671. __float_as_uint(sunsky.config_y[7]),
  672. __float_as_uint(sunsky.config_y[8]),
  673. __float_as_uint(sunsky.config_z[0]));
  674. compiler.add_node(__float_as_uint(sunsky.config_z[1]),
  675. __float_as_uint(sunsky.config_z[2]),
  676. __float_as_uint(sunsky.config_z[3]),
  677. __float_as_uint(sunsky.config_z[4]));
  678. compiler.add_node(__float_as_uint(sunsky.config_z[5]),
  679. __float_as_uint(sunsky.config_z[6]),
  680. __float_as_uint(sunsky.config_z[7]),
  681. __float_as_uint(sunsky.config_z[8]));
  682. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  683. }
  684. void SkyTextureNode::compile(OSLCompiler &compiler)
  685. {
  686. tex_mapping.compile(compiler);
  687. SunSky sunsky;
  688. if (type == NODE_SKY_OLD)
  689. sky_texture_precompute_old(&sunsky, sun_direction, turbidity);
  690. else if (type == NODE_SKY_NEW)
  691. sky_texture_precompute_new(&sunsky, sun_direction, turbidity, ground_albedo);
  692. else
  693. assert(false);
  694. compiler.parameter(this, "type");
  695. compiler.parameter("theta", sunsky.theta);
  696. compiler.parameter("phi", sunsky.phi);
  697. compiler.parameter_color("radiance",
  698. make_float3(sunsky.radiance_x, sunsky.radiance_y, sunsky.radiance_z));
  699. compiler.parameter_array("config_x", sunsky.config_x, 9);
  700. compiler.parameter_array("config_y", sunsky.config_y, 9);
  701. compiler.parameter_array("config_z", sunsky.config_z, 9);
  702. compiler.add(this, "node_sky_texture");
  703. }
  704. /* Gradient Texture */
  705. NODE_DEFINE(GradientTextureNode)
  706. {
  707. NodeType *type = NodeType::add("gradient_texture", create, NodeType::SHADER);
  708. TEXTURE_MAPPING_DEFINE(GradientTextureNode);
  709. static NodeEnum type_enum;
  710. type_enum.insert("linear", NODE_BLEND_LINEAR);
  711. type_enum.insert("quadratic", NODE_BLEND_QUADRATIC);
  712. type_enum.insert("easing", NODE_BLEND_EASING);
  713. type_enum.insert("diagonal", NODE_BLEND_DIAGONAL);
  714. type_enum.insert("radial", NODE_BLEND_RADIAL);
  715. type_enum.insert("quadratic_sphere", NODE_BLEND_QUADRATIC_SPHERE);
  716. type_enum.insert("spherical", NODE_BLEND_SPHERICAL);
  717. SOCKET_ENUM(type, "Type", type_enum, NODE_BLEND_LINEAR);
  718. SOCKET_IN_POINT(
  719. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  720. SOCKET_OUT_COLOR(color, "Color");
  721. SOCKET_OUT_FLOAT(fac, "Fac");
  722. return type;
  723. }
  724. GradientTextureNode::GradientTextureNode() : TextureNode(node_type)
  725. {
  726. }
  727. void GradientTextureNode::compile(SVMCompiler &compiler)
  728. {
  729. ShaderInput *vector_in = input("Vector");
  730. ShaderOutput *color_out = output("Color");
  731. ShaderOutput *fac_out = output("Fac");
  732. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  733. compiler.add_node(NODE_TEX_GRADIENT,
  734. compiler.encode_uchar4(type,
  735. vector_offset,
  736. compiler.stack_assign_if_linked(fac_out),
  737. compiler.stack_assign_if_linked(color_out)));
  738. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  739. }
  740. void GradientTextureNode::compile(OSLCompiler &compiler)
  741. {
  742. tex_mapping.compile(compiler);
  743. compiler.parameter(this, "type");
  744. compiler.add(this, "node_gradient_texture");
  745. }
  746. /* Noise Texture */
  747. NODE_DEFINE(NoiseTextureNode)
  748. {
  749. NodeType *type = NodeType::add("noise_texture", create, NodeType::SHADER);
  750. TEXTURE_MAPPING_DEFINE(NoiseTextureNode);
  751. SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
  752. SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
  753. SOCKET_IN_FLOAT(distortion, "Distortion", 0.0f);
  754. SOCKET_IN_POINT(
  755. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  756. SOCKET_OUT_COLOR(color, "Color");
  757. SOCKET_OUT_FLOAT(fac, "Fac");
  758. return type;
  759. }
  760. NoiseTextureNode::NoiseTextureNode() : TextureNode(node_type)
  761. {
  762. }
  763. void NoiseTextureNode::compile(SVMCompiler &compiler)
  764. {
  765. ShaderInput *distortion_in = input("Distortion");
  766. ShaderInput *detail_in = input("Detail");
  767. ShaderInput *scale_in = input("Scale");
  768. ShaderInput *vector_in = input("Vector");
  769. ShaderOutput *color_out = output("Color");
  770. ShaderOutput *fac_out = output("Fac");
  771. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  772. compiler.add_node(NODE_TEX_NOISE,
  773. compiler.encode_uchar4(vector_offset,
  774. compiler.stack_assign_if_linked(scale_in),
  775. compiler.stack_assign_if_linked(detail_in),
  776. compiler.stack_assign_if_linked(distortion_in)),
  777. compiler.encode_uchar4(compiler.stack_assign_if_linked(color_out),
  778. compiler.stack_assign_if_linked(fac_out)));
  779. compiler.add_node(__float_as_int(scale), __float_as_int(detail), __float_as_int(distortion));
  780. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  781. }
  782. void NoiseTextureNode::compile(OSLCompiler &compiler)
  783. {
  784. tex_mapping.compile(compiler);
  785. compiler.add(this, "node_noise_texture");
  786. }
  787. /* Voronoi Texture */
  788. NODE_DEFINE(VoronoiTextureNode)
  789. {
  790. NodeType *type = NodeType::add("voronoi_texture", create, NodeType::SHADER);
  791. TEXTURE_MAPPING_DEFINE(VoronoiTextureNode);
  792. static NodeEnum coloring_enum;
  793. coloring_enum.insert("intensity", NODE_VORONOI_INTENSITY);
  794. coloring_enum.insert("cells", NODE_VORONOI_CELLS);
  795. SOCKET_ENUM(coloring, "Coloring", coloring_enum, NODE_VORONOI_INTENSITY);
  796. static NodeEnum metric;
  797. metric.insert("distance", NODE_VORONOI_DISTANCE);
  798. metric.insert("manhattan", NODE_VORONOI_MANHATTAN);
  799. metric.insert("chebychev", NODE_VORONOI_CHEBYCHEV);
  800. metric.insert("minkowski", NODE_VORONOI_MINKOWSKI);
  801. SOCKET_ENUM(metric, "Distance Metric", metric, NODE_VORONOI_INTENSITY);
  802. static NodeEnum feature_enum;
  803. feature_enum.insert("F1", NODE_VORONOI_F1);
  804. feature_enum.insert("F2", NODE_VORONOI_F2);
  805. feature_enum.insert("F3", NODE_VORONOI_F3);
  806. feature_enum.insert("F4", NODE_VORONOI_F4);
  807. feature_enum.insert("F2F1", NODE_VORONOI_F2F1);
  808. SOCKET_ENUM(feature, "Feature", feature_enum, NODE_VORONOI_INTENSITY);
  809. SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
  810. SOCKET_IN_FLOAT(exponent, "Exponent", 0.5f);
  811. SOCKET_IN_POINT(
  812. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  813. SOCKET_OUT_COLOR(color, "Color");
  814. SOCKET_OUT_FLOAT(fac, "Fac");
  815. return type;
  816. }
  817. VoronoiTextureNode::VoronoiTextureNode() : TextureNode(node_type)
  818. {
  819. }
  820. void VoronoiTextureNode::compile(SVMCompiler &compiler)
  821. {
  822. ShaderInput *scale_in = input("Scale");
  823. ShaderInput *vector_in = input("Vector");
  824. ShaderInput *exponent_in = input("Exponent");
  825. ShaderOutput *color_out = output("Color");
  826. ShaderOutput *fac_out = output("Fac");
  827. if (vector_in->link)
  828. compiler.stack_assign(vector_in);
  829. if (scale_in->link)
  830. compiler.stack_assign(scale_in);
  831. if (exponent_in->link)
  832. compiler.stack_assign(exponent_in);
  833. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  834. compiler.add_node(NODE_TEX_VORONOI,
  835. compiler.encode_uchar4(vector_offset, coloring, metric, feature),
  836. compiler.encode_uchar4(compiler.stack_assign_if_linked(scale_in),
  837. compiler.stack_assign_if_linked(exponent_in),
  838. compiler.stack_assign(fac_out),
  839. compiler.stack_assign(color_out)));
  840. compiler.add_node(__float_as_int(scale), __float_as_int(exponent));
  841. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  842. }
  843. void VoronoiTextureNode::compile(OSLCompiler &compiler)
  844. {
  845. tex_mapping.compile(compiler);
  846. compiler.parameter(this, "coloring");
  847. compiler.parameter(this, "metric");
  848. compiler.parameter(this, "feature");
  849. compiler.add(this, "node_voronoi_texture");
  850. }
  851. /* IES Light */
  852. NODE_DEFINE(IESLightNode)
  853. {
  854. NodeType *type = NodeType::add("ies_light", create, NodeType::SHADER);
  855. TEXTURE_MAPPING_DEFINE(IESLightNode);
  856. SOCKET_STRING(ies, "IES", ustring());
  857. SOCKET_STRING(filename, "File Name", ustring());
  858. SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
  859. SOCKET_IN_POINT(
  860. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_NORMAL);
  861. SOCKET_OUT_FLOAT(fac, "Fac");
  862. return type;
  863. }
  864. IESLightNode::IESLightNode() : TextureNode(node_type)
  865. {
  866. light_manager = NULL;
  867. slot = -1;
  868. }
  869. ShaderNode *IESLightNode::clone() const
  870. {
  871. IESLightNode *node = new IESLightNode(*this);
  872. node->light_manager = NULL;
  873. node->slot = -1;
  874. return node;
  875. }
  876. IESLightNode::~IESLightNode()
  877. {
  878. if (light_manager) {
  879. light_manager->remove_ies(slot);
  880. }
  881. }
  882. void IESLightNode::get_slot()
  883. {
  884. assert(light_manager);
  885. if (slot == -1) {
  886. if (ies.empty()) {
  887. slot = light_manager->add_ies_from_file(filename);
  888. }
  889. else {
  890. slot = light_manager->add_ies(ies);
  891. }
  892. }
  893. }
  894. void IESLightNode::compile(SVMCompiler &compiler)
  895. {
  896. light_manager = compiler.light_manager;
  897. get_slot();
  898. ShaderInput *strength_in = input("Strength");
  899. ShaderInput *vector_in = input("Vector");
  900. ShaderOutput *fac_out = output("Fac");
  901. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  902. compiler.add_node(NODE_IES,
  903. compiler.encode_uchar4(compiler.stack_assign_if_linked(strength_in),
  904. vector_offset,
  905. compiler.stack_assign(fac_out),
  906. 0),
  907. slot,
  908. __float_as_int(strength));
  909. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  910. }
  911. void IESLightNode::compile(OSLCompiler &compiler)
  912. {
  913. light_manager = compiler.light_manager;
  914. get_slot();
  915. tex_mapping.compile(compiler);
  916. compiler.parameter_texture_ies("filename", slot);
  917. compiler.add(this, "node_ies_light");
  918. }
  919. /* Musgrave Texture */
  920. NODE_DEFINE(MusgraveTextureNode)
  921. {
  922. NodeType *type = NodeType::add("musgrave_texture", create, NodeType::SHADER);
  923. TEXTURE_MAPPING_DEFINE(MusgraveTextureNode);
  924. static NodeEnum type_enum;
  925. type_enum.insert("multifractal", NODE_MUSGRAVE_MULTIFRACTAL);
  926. type_enum.insert("fBM", NODE_MUSGRAVE_FBM);
  927. type_enum.insert("hybrid_multifractal", NODE_MUSGRAVE_HYBRID_MULTIFRACTAL);
  928. type_enum.insert("ridged_multifractal", NODE_MUSGRAVE_RIDGED_MULTIFRACTAL);
  929. type_enum.insert("hetero_terrain", NODE_MUSGRAVE_HETERO_TERRAIN);
  930. SOCKET_ENUM(type, "Type", type_enum, NODE_MUSGRAVE_FBM);
  931. SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
  932. SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
  933. SOCKET_IN_FLOAT(dimension, "Dimension", 2.0f);
  934. SOCKET_IN_FLOAT(lacunarity, "Lacunarity", 1.0f);
  935. SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
  936. SOCKET_IN_FLOAT(gain, "Gain", 1.0f);
  937. SOCKET_IN_POINT(
  938. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  939. SOCKET_OUT_COLOR(color, "Color");
  940. SOCKET_OUT_FLOAT(fac, "Fac");
  941. return type;
  942. }
  943. MusgraveTextureNode::MusgraveTextureNode() : TextureNode(node_type)
  944. {
  945. }
  946. void MusgraveTextureNode::compile(SVMCompiler &compiler)
  947. {
  948. ShaderInput *vector_in = input("Vector");
  949. ShaderInput *scale_in = input("Scale");
  950. ShaderInput *dimension_in = input("Dimension");
  951. ShaderInput *lacunarity_in = input("Lacunarity");
  952. ShaderInput *detail_in = input("Detail");
  953. ShaderInput *offset_in = input("Offset");
  954. ShaderInput *gain_in = input("Gain");
  955. ShaderOutput *fac_out = output("Fac");
  956. ShaderOutput *color_out = output("Color");
  957. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  958. compiler.add_node(NODE_TEX_MUSGRAVE,
  959. compiler.encode_uchar4(type,
  960. vector_offset,
  961. compiler.stack_assign_if_linked(color_out),
  962. compiler.stack_assign_if_linked(fac_out)),
  963. compiler.encode_uchar4(compiler.stack_assign_if_linked(dimension_in),
  964. compiler.stack_assign_if_linked(lacunarity_in),
  965. compiler.stack_assign_if_linked(detail_in),
  966. compiler.stack_assign_if_linked(offset_in)),
  967. compiler.encode_uchar4(compiler.stack_assign_if_linked(gain_in),
  968. compiler.stack_assign_if_linked(scale_in)));
  969. compiler.add_node(__float_as_int(dimension),
  970. __float_as_int(lacunarity),
  971. __float_as_int(detail),
  972. __float_as_int(offset));
  973. compiler.add_node(__float_as_int(gain), __float_as_int(scale));
  974. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  975. }
  976. void MusgraveTextureNode::compile(OSLCompiler &compiler)
  977. {
  978. tex_mapping.compile(compiler);
  979. compiler.parameter(this, "type");
  980. compiler.add(this, "node_musgrave_texture");
  981. }
  982. /* Wave Texture */
  983. NODE_DEFINE(WaveTextureNode)
  984. {
  985. NodeType *type = NodeType::add("wave_texture", create, NodeType::SHADER);
  986. TEXTURE_MAPPING_DEFINE(WaveTextureNode);
  987. static NodeEnum type_enum;
  988. type_enum.insert("bands", NODE_WAVE_BANDS);
  989. type_enum.insert("rings", NODE_WAVE_RINGS);
  990. SOCKET_ENUM(type, "Type", type_enum, NODE_WAVE_BANDS);
  991. static NodeEnum profile_enum;
  992. profile_enum.insert("sine", NODE_WAVE_PROFILE_SIN);
  993. profile_enum.insert("saw", NODE_WAVE_PROFILE_SAW);
  994. SOCKET_ENUM(profile, "Profile", profile_enum, NODE_WAVE_PROFILE_SIN);
  995. SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
  996. SOCKET_IN_FLOAT(distortion, "Distortion", 0.0f);
  997. SOCKET_IN_FLOAT(detail, "Detail", 2.0f);
  998. SOCKET_IN_FLOAT(detail_scale, "Detail Scale", 0.0f);
  999. SOCKET_IN_POINT(
  1000. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  1001. SOCKET_OUT_COLOR(color, "Color");
  1002. SOCKET_OUT_FLOAT(fac, "Fac");
  1003. return type;
  1004. }
  1005. WaveTextureNode::WaveTextureNode() : TextureNode(node_type)
  1006. {
  1007. }
  1008. void WaveTextureNode::compile(SVMCompiler &compiler)
  1009. {
  1010. ShaderInput *scale_in = input("Scale");
  1011. ShaderInput *distortion_in = input("Distortion");
  1012. ShaderInput *dscale_in = input("Detail Scale");
  1013. ShaderInput *detail_in = input("Detail");
  1014. ShaderInput *vector_in = input("Vector");
  1015. ShaderOutput *fac_out = output("Fac");
  1016. ShaderOutput *color_out = output("Color");
  1017. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  1018. compiler.add_node(NODE_TEX_WAVE,
  1019. compiler.encode_uchar4(type,
  1020. compiler.stack_assign_if_linked(color_out),
  1021. compiler.stack_assign_if_linked(fac_out),
  1022. compiler.stack_assign_if_linked(dscale_in)),
  1023. compiler.encode_uchar4(vector_offset,
  1024. compiler.stack_assign_if_linked(scale_in),
  1025. compiler.stack_assign_if_linked(detail_in),
  1026. compiler.stack_assign_if_linked(distortion_in)),
  1027. profile);
  1028. compiler.add_node(__float_as_int(scale),
  1029. __float_as_int(detail),
  1030. __float_as_int(distortion),
  1031. __float_as_int(detail_scale));
  1032. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  1033. }
  1034. void WaveTextureNode::compile(OSLCompiler &compiler)
  1035. {
  1036. tex_mapping.compile(compiler);
  1037. compiler.parameter(this, "type");
  1038. compiler.parameter(this, "profile");
  1039. compiler.add(this, "node_wave_texture");
  1040. }
  1041. /* Magic Texture */
  1042. NODE_DEFINE(MagicTextureNode)
  1043. {
  1044. NodeType *type = NodeType::add("magic_texture", create, NodeType::SHADER);
  1045. TEXTURE_MAPPING_DEFINE(MagicTextureNode);
  1046. SOCKET_INT(depth, "Depth", 2);
  1047. SOCKET_IN_POINT(
  1048. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  1049. SOCKET_IN_FLOAT(scale, "Scale", 5.0f);
  1050. SOCKET_IN_FLOAT(distortion, "Distortion", 1.0f);
  1051. SOCKET_OUT_COLOR(color, "Color");
  1052. SOCKET_OUT_FLOAT(fac, "Fac");
  1053. return type;
  1054. }
  1055. MagicTextureNode::MagicTextureNode() : TextureNode(node_type)
  1056. {
  1057. }
  1058. void MagicTextureNode::compile(SVMCompiler &compiler)
  1059. {
  1060. ShaderInput *vector_in = input("Vector");
  1061. ShaderInput *scale_in = input("Scale");
  1062. ShaderInput *distortion_in = input("Distortion");
  1063. ShaderOutput *color_out = output("Color");
  1064. ShaderOutput *fac_out = output("Fac");
  1065. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  1066. compiler.add_node(NODE_TEX_MAGIC,
  1067. compiler.encode_uchar4(depth,
  1068. compiler.stack_assign_if_linked(color_out),
  1069. compiler.stack_assign_if_linked(fac_out)),
  1070. compiler.encode_uchar4(vector_offset,
  1071. compiler.stack_assign_if_linked(scale_in),
  1072. compiler.stack_assign_if_linked(distortion_in)));
  1073. compiler.add_node(__float_as_int(scale), __float_as_int(distortion));
  1074. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  1075. }
  1076. void MagicTextureNode::compile(OSLCompiler &compiler)
  1077. {
  1078. tex_mapping.compile(compiler);
  1079. compiler.parameter(this, "depth");
  1080. compiler.add(this, "node_magic_texture");
  1081. }
  1082. /* Checker Texture */
  1083. NODE_DEFINE(CheckerTextureNode)
  1084. {
  1085. NodeType *type = NodeType::add("checker_texture", create, NodeType::SHADER);
  1086. TEXTURE_MAPPING_DEFINE(CheckerTextureNode);
  1087. SOCKET_IN_POINT(
  1088. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  1089. SOCKET_IN_COLOR(color1, "Color1", make_float3(0.0f, 0.0f, 0.0f));
  1090. SOCKET_IN_COLOR(color2, "Color2", make_float3(0.0f, 0.0f, 0.0f));
  1091. SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
  1092. SOCKET_OUT_COLOR(color, "Color");
  1093. SOCKET_OUT_FLOAT(fac, "Fac");
  1094. return type;
  1095. }
  1096. CheckerTextureNode::CheckerTextureNode() : TextureNode(node_type)
  1097. {
  1098. }
  1099. void CheckerTextureNode::compile(SVMCompiler &compiler)
  1100. {
  1101. ShaderInput *vector_in = input("Vector");
  1102. ShaderInput *color1_in = input("Color1");
  1103. ShaderInput *color2_in = input("Color2");
  1104. ShaderInput *scale_in = input("Scale");
  1105. ShaderOutput *color_out = output("Color");
  1106. ShaderOutput *fac_out = output("Fac");
  1107. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  1108. compiler.add_node(NODE_TEX_CHECKER,
  1109. compiler.encode_uchar4(vector_offset,
  1110. compiler.stack_assign(color1_in),
  1111. compiler.stack_assign(color2_in),
  1112. compiler.stack_assign_if_linked(scale_in)),
  1113. compiler.encode_uchar4(compiler.stack_assign_if_linked(color_out),
  1114. compiler.stack_assign_if_linked(fac_out)),
  1115. __float_as_int(scale));
  1116. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  1117. }
  1118. void CheckerTextureNode::compile(OSLCompiler &compiler)
  1119. {
  1120. tex_mapping.compile(compiler);
  1121. compiler.add(this, "node_checker_texture");
  1122. }
  1123. /* Brick Texture */
  1124. NODE_DEFINE(BrickTextureNode)
  1125. {
  1126. NodeType *type = NodeType::add("brick_texture", create, NodeType::SHADER);
  1127. TEXTURE_MAPPING_DEFINE(BrickTextureNode);
  1128. SOCKET_FLOAT(offset, "Offset", 0.5f);
  1129. SOCKET_INT(offset_frequency, "Offset Frequency", 2);
  1130. SOCKET_FLOAT(squash, "Squash", 1.0f);
  1131. SOCKET_INT(squash_frequency, "Squash Frequency", 2);
  1132. SOCKET_IN_POINT(
  1133. vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TEXTURE_GENERATED);
  1134. SOCKET_IN_COLOR(color1, "Color1", make_float3(0.0f, 0.0f, 0.0f));
  1135. SOCKET_IN_COLOR(color2, "Color2", make_float3(0.0f, 0.0f, 0.0f));
  1136. SOCKET_IN_COLOR(mortar, "Mortar", make_float3(0.0f, 0.0f, 0.0f));
  1137. SOCKET_IN_FLOAT(scale, "Scale", 5.0f);
  1138. SOCKET_IN_FLOAT(mortar_size, "Mortar Size", 0.02f);
  1139. SOCKET_IN_FLOAT(mortar_smooth, "Mortar Smooth", 0.0f);
  1140. SOCKET_IN_FLOAT(bias, "Bias", 0.0f);
  1141. SOCKET_IN_FLOAT(brick_width, "Brick Width", 0.5f);
  1142. SOCKET_IN_FLOAT(row_height, "Row Height", 0.25f);
  1143. SOCKET_OUT_COLOR(color, "Color");
  1144. SOCKET_OUT_FLOAT(fac, "Fac");
  1145. return type;
  1146. }
  1147. BrickTextureNode::BrickTextureNode() : TextureNode(node_type)
  1148. {
  1149. }
  1150. void BrickTextureNode::compile(SVMCompiler &compiler)
  1151. {
  1152. ShaderInput *vector_in = input("Vector");
  1153. ShaderInput *color1_in = input("Color1");
  1154. ShaderInput *color2_in = input("Color2");
  1155. ShaderInput *mortar_in = input("Mortar");
  1156. ShaderInput *scale_in = input("Scale");
  1157. ShaderInput *mortar_size_in = input("Mortar Size");
  1158. ShaderInput *mortar_smooth_in = input("Mortar Smooth");
  1159. ShaderInput *bias_in = input("Bias");
  1160. ShaderInput *brick_width_in = input("Brick Width");
  1161. ShaderInput *row_height_in = input("Row Height");
  1162. ShaderOutput *color_out = output("Color");
  1163. ShaderOutput *fac_out = output("Fac");
  1164. int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
  1165. compiler.add_node(NODE_TEX_BRICK,
  1166. compiler.encode_uchar4(vector_offset,
  1167. compiler.stack_assign(color1_in),
  1168. compiler.stack_assign(color2_in),
  1169. compiler.stack_assign(mortar_in)),
  1170. compiler.encode_uchar4(compiler.stack_assign_if_linked(scale_in),
  1171. compiler.stack_assign_if_linked(mortar_size_in),
  1172. compiler.stack_assign_if_linked(bias_in),
  1173. compiler.stack_assign_if_linked(brick_width_in)),
  1174. compiler.encode_uchar4(compiler.stack_assign_if_linked(row_height_in),
  1175. compiler.stack_assign_if_linked(color_out),
  1176. compiler.stack_assign_if_linked(fac_out),
  1177. compiler.stack_assign_if_linked(mortar_smooth_in)));
  1178. compiler.add_node(compiler.encode_uchar4(offset_frequency, squash_frequency),
  1179. __float_as_int(scale),
  1180. __float_as_int(mortar_size),
  1181. __float_as_int(bias));
  1182. compiler.add_node(__float_as_int(brick_width),
  1183. __float_as_int(row_height),
  1184. __float_as_int(offset),
  1185. __float_as_int(squash));
  1186. compiler.add_node(
  1187. __float_as_int(mortar_smooth), SVM_STACK_INVALID, SVM_STACK_INVALID, SVM_STACK_INVALID);
  1188. tex_mapping.compile_end(compiler, vector_in, vector_offset);
  1189. }
  1190. void BrickTextureNode::compile(OSLCompiler &compiler)
  1191. {
  1192. tex_mapping.compile(compiler);
  1193. compiler.parameter(this, "offset");
  1194. compiler.parameter(this, "offset_frequency");
  1195. compiler.parameter(this, "squash");
  1196. compiler.parameter(this, "squash_frequency");
  1197. compiler.add(this, "node_brick_texture");
  1198. }
  1199. /* Point Density Texture */
  1200. NODE_DEFINE(PointDensityTextureNode)
  1201. {
  1202. NodeType *type = NodeType::add("point_density_texture", create, NodeType::SHADER);
  1203. SOCKET_STRING(filename, "Filename", ustring());
  1204. static NodeEnum space_enum;
  1205. space_enum.insert("object", NODE_TEX_VOXEL_SPACE_OBJECT);
  1206. space_enum.insert("world", NODE_TEX_VOXEL_SPACE_WORLD);
  1207. SOCKET_ENUM(space, "Space", space_enum, NODE_TEX_VOXEL_SPACE_OBJECT);
  1208. static NodeEnum interpolation_enum;
  1209. interpolation_enum.insert("closest", INTERPOLATION_CLOSEST);
  1210. interpolation_enum.insert("linear", INTERPOLATION_LINEAR);
  1211. interpolation_enum.insert("cubic", INTERPOLATION_CUBIC);
  1212. interpolation_enum.insert("smart", INTERPOLATION_SMART);
  1213. SOCKET_ENUM(interpolation, "Interpolation", interpolation_enum, INTERPOLATION_LINEAR);
  1214. SOCKET_TRANSFORM(tfm, "Transform", transform_identity());
  1215. SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_POSITION);
  1216. SOCKET_OUT_FLOAT(density, "Density");
  1217. SOCKET_OUT_COLOR(color, "Color");
  1218. return type;
  1219. }
  1220. PointDensityTextureNode::PointDensityTextureNode() : ShaderNode(node_type)
  1221. {
  1222. image_manager = NULL;
  1223. slot = -1;
  1224. builtin_data = NULL;
  1225. }
  1226. PointDensityTextureNode::~PointDensityTextureNode()
  1227. {
  1228. if (image_manager) {
  1229. image_manager->remove_image(filename.string(),
  1230. builtin_data,
  1231. interpolation,
  1232. EXTENSION_CLIP,
  1233. IMAGE_ALPHA_AUTO,
  1234. ustring());
  1235. }
  1236. }
  1237. ShaderNode *PointDensityTextureNode::clone() const
  1238. {
  1239. /* Increase image user count for new node. We need to ensure to not call
  1240. * add_image again, to work around access of freed data on the Blender
  1241. * side. A better solution should be found to avoid this. */
  1242. if (slot != -1) {
  1243. image_manager->add_image_user(slot);
  1244. }
  1245. return new PointDensityTextureNode(*this);
  1246. }
  1247. void PointDensityTextureNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  1248. {
  1249. if (shader->has_volume)
  1250. attributes->add(ATTR_STD_GENERATED_TRANSFORM);
  1251. ShaderNode::attributes(shader, attributes);
  1252. }
  1253. void PointDensityTextureNode::add_image()
  1254. {
  1255. if (slot == -1) {
  1256. ImageMetaData metadata;
  1257. slot = image_manager->add_image(filename.string(),
  1258. builtin_data,
  1259. false,
  1260. 0,
  1261. interpolation,
  1262. EXTENSION_CLIP,
  1263. IMAGE_ALPHA_AUTO,
  1264. u_colorspace_raw,
  1265. metadata);
  1266. }
  1267. }
  1268. void PointDensityTextureNode::compile(SVMCompiler &compiler)
  1269. {
  1270. ShaderInput *vector_in = input("Vector");
  1271. ShaderOutput *density_out = output("Density");
  1272. ShaderOutput *color_out = output("Color");
  1273. const bool use_density = !density_out->links.empty();
  1274. const bool use_color = !color_out->links.empty();
  1275. image_manager = compiler.image_manager;
  1276. if (use_density || use_color) {
  1277. add_image();
  1278. if (slot != -1) {
  1279. compiler.stack_assign(vector_in);
  1280. compiler.add_node(NODE_TEX_VOXEL,
  1281. slot,
  1282. compiler.encode_uchar4(compiler.stack_assign(vector_in),
  1283. compiler.stack_assign_if_linked(density_out),
  1284. compiler.stack_assign_if_linked(color_out),
  1285. space));
  1286. if (space == NODE_TEX_VOXEL_SPACE_WORLD) {
  1287. compiler.add_node(tfm.x);
  1288. compiler.add_node(tfm.y);
  1289. compiler.add_node(tfm.z);
  1290. }
  1291. }
  1292. else {
  1293. if (use_density) {
  1294. compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), compiler.stack_assign(density_out));
  1295. }
  1296. if (use_color) {
  1297. compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
  1298. compiler.add_node(
  1299. NODE_VALUE_V,
  1300. make_float3(TEX_IMAGE_MISSING_R, TEX_IMAGE_MISSING_G, TEX_IMAGE_MISSING_B));
  1301. }
  1302. }
  1303. }
  1304. }
  1305. void PointDensityTextureNode::compile(OSLCompiler &compiler)
  1306. {
  1307. ShaderOutput *density_out = output("Density");
  1308. ShaderOutput *color_out = output("Color");
  1309. const bool use_density = !density_out->links.empty();
  1310. const bool use_color = !color_out->links.empty();
  1311. image_manager = compiler.image_manager;
  1312. if (use_density || use_color) {
  1313. add_image();
  1314. compiler.parameter_texture("filename", slot);
  1315. if (space == NODE_TEX_VOXEL_SPACE_WORLD) {
  1316. compiler.parameter("mapping", tfm);
  1317. compiler.parameter("use_mapping", 1);
  1318. }
  1319. compiler.parameter(this, "interpolation");
  1320. compiler.add(this, "node_voxel_texture");
  1321. }
  1322. }
  1323. /* Normal */
  1324. NODE_DEFINE(NormalNode)
  1325. {
  1326. NodeType *type = NodeType::add("normal", create, NodeType::SHADER);
  1327. SOCKET_VECTOR(direction, "direction", make_float3(0.0f, 0.0f, 0.0f));
  1328. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f));
  1329. SOCKET_OUT_NORMAL(normal, "Normal");
  1330. SOCKET_OUT_FLOAT(dot, "Dot");
  1331. return type;
  1332. }
  1333. NormalNode::NormalNode() : ShaderNode(node_type)
  1334. {
  1335. }
  1336. void NormalNode::compile(SVMCompiler &compiler)
  1337. {
  1338. ShaderInput *normal_in = input("Normal");
  1339. ShaderOutput *normal_out = output("Normal");
  1340. ShaderOutput *dot_out = output("Dot");
  1341. compiler.add_node(NODE_NORMAL,
  1342. compiler.stack_assign(normal_in),
  1343. compiler.stack_assign(normal_out),
  1344. compiler.stack_assign(dot_out));
  1345. compiler.add_node(
  1346. __float_as_int(direction.x), __float_as_int(direction.y), __float_as_int(direction.z));
  1347. }
  1348. void NormalNode::compile(OSLCompiler &compiler)
  1349. {
  1350. compiler.parameter(this, "direction");
  1351. compiler.add(this, "node_normal");
  1352. }
  1353. /* Mapping */
  1354. NODE_DEFINE(MappingNode)
  1355. {
  1356. NodeType *type = NodeType::add("mapping", create, NodeType::SHADER);
  1357. TEXTURE_MAPPING_DEFINE(MappingNode);
  1358. SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
  1359. SOCKET_OUT_POINT(vector, "Vector");
  1360. return type;
  1361. }
  1362. MappingNode::MappingNode() : ShaderNode(node_type)
  1363. {
  1364. }
  1365. void MappingNode::compile(SVMCompiler &compiler)
  1366. {
  1367. ShaderInput *vector_in = input("Vector");
  1368. ShaderOutput *vector_out = output("Vector");
  1369. tex_mapping.compile(
  1370. compiler, compiler.stack_assign(vector_in), compiler.stack_assign(vector_out));
  1371. }
  1372. void MappingNode::compile(OSLCompiler &compiler)
  1373. {
  1374. compiler.parameter("Matrix", tex_mapping.compute_transform());
  1375. compiler.parameter_point("mapping_min", tex_mapping.min);
  1376. compiler.parameter_point("mapping_max", tex_mapping.max);
  1377. compiler.parameter("use_minmax", tex_mapping.use_minmax);
  1378. compiler.add(this, "node_mapping");
  1379. }
  1380. /* RGBToBW */
  1381. NODE_DEFINE(RGBToBWNode)
  1382. {
  1383. NodeType *type = NodeType::add("rgb_to_bw", create, NodeType::SHADER);
  1384. SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
  1385. SOCKET_OUT_FLOAT(val, "Val");
  1386. return type;
  1387. }
  1388. RGBToBWNode::RGBToBWNode() : ShaderNode(node_type)
  1389. {
  1390. }
  1391. void RGBToBWNode::constant_fold(const ConstantFolder &folder)
  1392. {
  1393. if (folder.all_inputs_constant()) {
  1394. float val = folder.scene->shader_manager->linear_rgb_to_gray(color);
  1395. folder.make_constant(val);
  1396. }
  1397. }
  1398. void RGBToBWNode::compile(SVMCompiler &compiler)
  1399. {
  1400. compiler.add_node(NODE_CONVERT,
  1401. NODE_CONVERT_CF,
  1402. compiler.stack_assign(inputs[0]),
  1403. compiler.stack_assign(outputs[0]));
  1404. }
  1405. void RGBToBWNode::compile(OSLCompiler &compiler)
  1406. {
  1407. compiler.add(this, "node_rgb_to_bw");
  1408. }
  1409. /* Convert */
  1410. const NodeType *ConvertNode::node_types[ConvertNode::MAX_TYPE][ConvertNode::MAX_TYPE];
  1411. bool ConvertNode::initialized = ConvertNode::register_types();
  1412. Node *ConvertNode::create(const NodeType *type)
  1413. {
  1414. return new ConvertNode(type->inputs[0].type, type->outputs[0].type);
  1415. }
  1416. bool ConvertNode::register_types()
  1417. {
  1418. const int num_types = 8;
  1419. SocketType::Type types[num_types] = {SocketType::FLOAT,
  1420. SocketType::INT,
  1421. SocketType::COLOR,
  1422. SocketType::VECTOR,
  1423. SocketType::POINT,
  1424. SocketType::NORMAL,
  1425. SocketType::STRING,
  1426. SocketType::CLOSURE};
  1427. for (size_t i = 0; i < num_types; i++) {
  1428. SocketType::Type from = types[i];
  1429. ustring from_name(SocketType::type_name(from));
  1430. ustring from_value_name("value_" + from_name.string());
  1431. for (size_t j = 0; j < num_types; j++) {
  1432. SocketType::Type to = types[j];
  1433. ustring to_name(SocketType::type_name(to));
  1434. ustring to_value_name("value_" + to_name.string());
  1435. string node_name = "convert_" + from_name.string() + "_to_" + to_name.string();
  1436. NodeType *type = NodeType::add(node_name.c_str(), create, NodeType::SHADER);
  1437. type->register_input(from_value_name,
  1438. from_value_name,
  1439. from,
  1440. SOCKET_OFFSETOF(ConvertNode, value_float),
  1441. SocketType::zero_default_value(),
  1442. NULL,
  1443. NULL,
  1444. SocketType::LINKABLE);
  1445. type->register_output(to_value_name, to_value_name, to);
  1446. assert(from < MAX_TYPE);
  1447. assert(to < MAX_TYPE);
  1448. node_types[from][to] = type;
  1449. }
  1450. }
  1451. return true;
  1452. }
  1453. ConvertNode::ConvertNode(SocketType::Type from_, SocketType::Type to_, bool autoconvert)
  1454. : ShaderNode(node_types[from_][to_])
  1455. {
  1456. from = from_;
  1457. to = to_;
  1458. if (from == to)
  1459. special_type = SHADER_SPECIAL_TYPE_PROXY;
  1460. else if (autoconvert)
  1461. special_type = SHADER_SPECIAL_TYPE_AUTOCONVERT;
  1462. }
  1463. void ConvertNode::constant_fold(const ConstantFolder &folder)
  1464. {
  1465. /* proxy nodes should have been removed at this point */
  1466. assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
  1467. /* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
  1468. if (folder.all_inputs_constant()) {
  1469. if (from == SocketType::FLOAT) {
  1470. if (SocketType::is_float3(to)) {
  1471. folder.make_constant(make_float3(value_float, value_float, value_float));
  1472. }
  1473. }
  1474. else if (SocketType::is_float3(from)) {
  1475. if (to == SocketType::FLOAT) {
  1476. if (from == SocketType::COLOR) {
  1477. /* color to float */
  1478. float val = folder.scene->shader_manager->linear_rgb_to_gray(value_color);
  1479. folder.make_constant(val);
  1480. }
  1481. else {
  1482. /* vector/point/normal to float */
  1483. folder.make_constant(average(value_vector));
  1484. }
  1485. }
  1486. else if (SocketType::is_float3(to)) {
  1487. folder.make_constant(value_color);
  1488. }
  1489. }
  1490. }
  1491. else {
  1492. ShaderInput *in = inputs[0];
  1493. ShaderNode *prev = in->link->parent;
  1494. /* no-op conversion of A to B to A */
  1495. if (prev->type == node_types[to][from]) {
  1496. ShaderInput *prev_in = prev->inputs[0];
  1497. if (SocketType::is_float3(from) && (to == SocketType::FLOAT || SocketType::is_float3(to)) &&
  1498. prev_in->link) {
  1499. folder.bypass(prev_in->link);
  1500. }
  1501. }
  1502. }
  1503. }
  1504. void ConvertNode::compile(SVMCompiler &compiler)
  1505. {
  1506. /* proxy nodes should have been removed at this point */
  1507. assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
  1508. ShaderInput *in = inputs[0];
  1509. ShaderOutput *out = outputs[0];
  1510. if (from == SocketType::FLOAT) {
  1511. if (to == SocketType::INT)
  1512. /* float to int */
  1513. compiler.add_node(
  1514. NODE_CONVERT, NODE_CONVERT_FI, compiler.stack_assign(in), compiler.stack_assign(out));
  1515. else
  1516. /* float to float3 */
  1517. compiler.add_node(
  1518. NODE_CONVERT, NODE_CONVERT_FV, compiler.stack_assign(in), compiler.stack_assign(out));
  1519. }
  1520. else if (from == SocketType::INT) {
  1521. if (to == SocketType::FLOAT)
  1522. /* int to float */
  1523. compiler.add_node(
  1524. NODE_CONVERT, NODE_CONVERT_IF, compiler.stack_assign(in), compiler.stack_assign(out));
  1525. else
  1526. /* int to vector/point/normal */
  1527. compiler.add_node(
  1528. NODE_CONVERT, NODE_CONVERT_IV, compiler.stack_assign(in), compiler.stack_assign(out));
  1529. }
  1530. else if (to == SocketType::FLOAT) {
  1531. if (from == SocketType::COLOR)
  1532. /* color to float */
  1533. compiler.add_node(
  1534. NODE_CONVERT, NODE_CONVERT_CF, compiler.stack_assign(in), compiler.stack_assign(out));
  1535. else
  1536. /* vector/point/normal to float */
  1537. compiler.add_node(
  1538. NODE_CONVERT, NODE_CONVERT_VF, compiler.stack_assign(in), compiler.stack_assign(out));
  1539. }
  1540. else if (to == SocketType::INT) {
  1541. if (from == SocketType::COLOR)
  1542. /* color to int */
  1543. compiler.add_node(
  1544. NODE_CONVERT, NODE_CONVERT_CI, compiler.stack_assign(in), compiler.stack_assign(out));
  1545. else
  1546. /* vector/point/normal to int */
  1547. compiler.add_node(
  1548. NODE_CONVERT, NODE_CONVERT_VI, compiler.stack_assign(in), compiler.stack_assign(out));
  1549. }
  1550. else {
  1551. /* float3 to float3 */
  1552. if (in->link) {
  1553. /* no op in SVM */
  1554. compiler.stack_link(in, out);
  1555. }
  1556. else {
  1557. /* set 0,0,0 value */
  1558. compiler.add_node(NODE_VALUE_V, compiler.stack_assign(out));
  1559. compiler.add_node(NODE_VALUE_V, value_color);
  1560. }
  1561. }
  1562. }
  1563. void ConvertNode::compile(OSLCompiler &compiler)
  1564. {
  1565. /* proxy nodes should have been removed at this point */
  1566. assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
  1567. if (from == SocketType::FLOAT)
  1568. compiler.add(this, "node_convert_from_float");
  1569. else if (from == SocketType::INT)
  1570. compiler.add(this, "node_convert_from_int");
  1571. else if (from == SocketType::COLOR)
  1572. compiler.add(this, "node_convert_from_color");
  1573. else if (from == SocketType::VECTOR)
  1574. compiler.add(this, "node_convert_from_vector");
  1575. else if (from == SocketType::POINT)
  1576. compiler.add(this, "node_convert_from_point");
  1577. else if (from == SocketType::NORMAL)
  1578. compiler.add(this, "node_convert_from_normal");
  1579. else
  1580. assert(0);
  1581. }
  1582. /* Base type for all closure-type nodes */
  1583. BsdfBaseNode::BsdfBaseNode(const NodeType *node_type) : ShaderNode(node_type)
  1584. {
  1585. special_type = SHADER_SPECIAL_TYPE_CLOSURE;
  1586. }
  1587. bool BsdfBaseNode::has_bump()
  1588. {
  1589. /* detect if anything is plugged into the normal input besides the default */
  1590. ShaderInput *normal_in = input("Normal");
  1591. return (normal_in && normal_in->link &&
  1592. normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
  1593. }
  1594. /* BSDF Closure */
  1595. BsdfNode::BsdfNode(const NodeType *node_type) : BsdfBaseNode(node_type)
  1596. {
  1597. }
  1598. void BsdfNode::compile(SVMCompiler &compiler,
  1599. ShaderInput *param1,
  1600. ShaderInput *param2,
  1601. ShaderInput *param3,
  1602. ShaderInput *param4)
  1603. {
  1604. ShaderInput *color_in = input("Color");
  1605. ShaderInput *normal_in = input("Normal");
  1606. ShaderInput *tangent_in = input("Tangent");
  1607. if (color_in->link)
  1608. compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
  1609. else
  1610. compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
  1611. int normal_offset = (normal_in) ? compiler.stack_assign_if_linked(normal_in) : SVM_STACK_INVALID;
  1612. int tangent_offset = (tangent_in) ? compiler.stack_assign_if_linked(tangent_in) :
  1613. SVM_STACK_INVALID;
  1614. int param3_offset = (param3) ? compiler.stack_assign(param3) : SVM_STACK_INVALID;
  1615. int param4_offset = (param4) ? compiler.stack_assign(param4) : SVM_STACK_INVALID;
  1616. compiler.add_node(
  1617. NODE_CLOSURE_BSDF,
  1618. compiler.encode_uchar4(closure,
  1619. (param1) ? compiler.stack_assign(param1) : SVM_STACK_INVALID,
  1620. (param2) ? compiler.stack_assign(param2) : SVM_STACK_INVALID,
  1621. compiler.closure_mix_weight_offset()),
  1622. __float_as_int((param1) ? get_float(param1->socket_type) : 0.0f),
  1623. __float_as_int((param2) ? get_float(param2->socket_type) : 0.0f));
  1624. compiler.add_node(normal_offset, tangent_offset, param3_offset, param4_offset);
  1625. }
  1626. void BsdfNode::compile(SVMCompiler &compiler)
  1627. {
  1628. compile(compiler, NULL, NULL);
  1629. }
  1630. void BsdfNode::compile(OSLCompiler & /*compiler*/)
  1631. {
  1632. assert(0);
  1633. }
  1634. /* Anisotropic BSDF Closure */
  1635. NODE_DEFINE(AnisotropicBsdfNode)
  1636. {
  1637. NodeType *type = NodeType::add("anisotropic_bsdf", create, NodeType::SHADER);
  1638. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  1639. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  1640. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  1641. static NodeEnum distribution_enum;
  1642. distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ANISO_ID);
  1643. distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
  1644. distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_ANISO_ID);
  1645. distribution_enum.insert("ashikhmin_shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID);
  1646. SOCKET_ENUM(
  1647. distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID);
  1648. SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
  1649. SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
  1650. SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.5f);
  1651. SOCKET_IN_FLOAT(rotation, "Rotation", 0.0f);
  1652. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  1653. return type;
  1654. }
  1655. AnisotropicBsdfNode::AnisotropicBsdfNode() : BsdfNode(node_type)
  1656. {
  1657. closure = CLOSURE_BSDF_MICROFACET_GGX_ANISO_ID;
  1658. }
  1659. void AnisotropicBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  1660. {
  1661. if (shader->has_surface) {
  1662. ShaderInput *tangent_in = input("Tangent");
  1663. if (!tangent_in->link)
  1664. attributes->add(ATTR_STD_GENERATED);
  1665. }
  1666. ShaderNode::attributes(shader, attributes);
  1667. }
  1668. void AnisotropicBsdfNode::compile(SVMCompiler &compiler)
  1669. {
  1670. closure = distribution;
  1671. if (closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ANISO_ID)
  1672. BsdfNode::compile(
  1673. compiler, input("Roughness"), input("Anisotropy"), input("Rotation"), input("Color"));
  1674. else
  1675. BsdfNode::compile(compiler, input("Roughness"), input("Anisotropy"), input("Rotation"));
  1676. }
  1677. void AnisotropicBsdfNode::compile(OSLCompiler &compiler)
  1678. {
  1679. compiler.parameter(this, "distribution");
  1680. compiler.add(this, "node_anisotropic_bsdf");
  1681. }
  1682. /* Glossy BSDF Closure */
  1683. NODE_DEFINE(GlossyBsdfNode)
  1684. {
  1685. NodeType *type = NodeType::add("glossy_bsdf", create, NodeType::SHADER);
  1686. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  1687. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  1688. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  1689. static NodeEnum distribution_enum;
  1690. distribution_enum.insert("sharp", CLOSURE_BSDF_REFLECTION_ID);
  1691. distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_ID);
  1692. distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_ID);
  1693. distribution_enum.insert("ashikhmin_shirley", CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID);
  1694. distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID);
  1695. SOCKET_ENUM(distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_ID);
  1696. SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
  1697. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  1698. return type;
  1699. }
  1700. GlossyBsdfNode::GlossyBsdfNode() : BsdfNode(node_type)
  1701. {
  1702. closure = CLOSURE_BSDF_MICROFACET_GGX_ID;
  1703. distribution_orig = NBUILTIN_CLOSURES;
  1704. }
  1705. void GlossyBsdfNode::simplify_settings(Scene *scene)
  1706. {
  1707. if (distribution_orig == NBUILTIN_CLOSURES) {
  1708. roughness_orig = roughness;
  1709. distribution_orig = distribution;
  1710. }
  1711. else {
  1712. /* By default we use original values, so we don't worry about restoring
  1713. * defaults later one and can only do override when needed.
  1714. */
  1715. roughness = roughness_orig;
  1716. distribution = distribution_orig;
  1717. }
  1718. Integrator *integrator = scene->integrator;
  1719. ShaderInput *roughness_input = input("Roughness");
  1720. if (integrator->filter_glossy == 0.0f) {
  1721. /* Fallback to Sharp closure for Roughness close to 0.
  1722. * Note: Keep the epsilon in sync with kernel!
  1723. */
  1724. if (!roughness_input->link && roughness <= 1e-4f) {
  1725. VLOG(1) << "Using sharp glossy BSDF.";
  1726. distribution = CLOSURE_BSDF_REFLECTION_ID;
  1727. }
  1728. }
  1729. else {
  1730. /* If filter glossy is used we replace Sharp glossy with GGX so we can
  1731. * benefit from closure blur to remove unwanted noise.
  1732. */
  1733. if (roughness_input->link == NULL && distribution == CLOSURE_BSDF_REFLECTION_ID) {
  1734. VLOG(1) << "Using GGX glossy with filter glossy.";
  1735. distribution = CLOSURE_BSDF_MICROFACET_GGX_ID;
  1736. roughness = 0.0f;
  1737. }
  1738. }
  1739. closure = distribution;
  1740. }
  1741. bool GlossyBsdfNode::has_integrator_dependency()
  1742. {
  1743. ShaderInput *roughness_input = input("Roughness");
  1744. return !roughness_input->link &&
  1745. (distribution == CLOSURE_BSDF_REFLECTION_ID || roughness <= 1e-4f);
  1746. }
  1747. void GlossyBsdfNode::compile(SVMCompiler &compiler)
  1748. {
  1749. closure = distribution;
  1750. if (closure == CLOSURE_BSDF_REFLECTION_ID)
  1751. BsdfNode::compile(compiler, NULL, NULL);
  1752. else if (closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_ID)
  1753. BsdfNode::compile(compiler, input("Roughness"), NULL, input("Color"));
  1754. else
  1755. BsdfNode::compile(compiler, input("Roughness"), NULL);
  1756. }
  1757. void GlossyBsdfNode::compile(OSLCompiler &compiler)
  1758. {
  1759. compiler.parameter(this, "distribution");
  1760. compiler.add(this, "node_glossy_bsdf");
  1761. }
  1762. /* Glass BSDF Closure */
  1763. NODE_DEFINE(GlassBsdfNode)
  1764. {
  1765. NodeType *type = NodeType::add("glass_bsdf", create, NodeType::SHADER);
  1766. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  1767. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  1768. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  1769. static NodeEnum distribution_enum;
  1770. distribution_enum.insert("sharp", CLOSURE_BSDF_SHARP_GLASS_ID);
  1771. distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID);
  1772. distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
  1773. distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
  1774. SOCKET_ENUM(
  1775. distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
  1776. SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
  1777. SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
  1778. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  1779. return type;
  1780. }
  1781. GlassBsdfNode::GlassBsdfNode() : BsdfNode(node_type)
  1782. {
  1783. closure = CLOSURE_BSDF_SHARP_GLASS_ID;
  1784. distribution_orig = NBUILTIN_CLOSURES;
  1785. }
  1786. void GlassBsdfNode::simplify_settings(Scene *scene)
  1787. {
  1788. if (distribution_orig == NBUILTIN_CLOSURES) {
  1789. roughness_orig = roughness;
  1790. distribution_orig = distribution;
  1791. }
  1792. else {
  1793. /* By default we use original values, so we don't worry about restoring
  1794. * defaults later one and can only do override when needed.
  1795. */
  1796. roughness = roughness_orig;
  1797. distribution = distribution_orig;
  1798. }
  1799. Integrator *integrator = scene->integrator;
  1800. ShaderInput *roughness_input = input("Roughness");
  1801. if (integrator->filter_glossy == 0.0f) {
  1802. /* Fallback to Sharp closure for Roughness close to 0.
  1803. * Note: Keep the epsilon in sync with kernel!
  1804. */
  1805. if (!roughness_input->link && roughness <= 1e-4f) {
  1806. VLOG(1) << "Using sharp glass BSDF.";
  1807. distribution = CLOSURE_BSDF_SHARP_GLASS_ID;
  1808. }
  1809. }
  1810. else {
  1811. /* If filter glossy is used we replace Sharp glossy with GGX so we can
  1812. * benefit from closure blur to remove unwanted noise.
  1813. */
  1814. if (roughness_input->link == NULL && distribution == CLOSURE_BSDF_SHARP_GLASS_ID) {
  1815. VLOG(1) << "Using GGX glass with filter glossy.";
  1816. distribution = CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID;
  1817. roughness = 0.0f;
  1818. }
  1819. }
  1820. closure = distribution;
  1821. }
  1822. bool GlassBsdfNode::has_integrator_dependency()
  1823. {
  1824. ShaderInput *roughness_input = input("Roughness");
  1825. return !roughness_input->link &&
  1826. (distribution == CLOSURE_BSDF_SHARP_GLASS_ID || roughness <= 1e-4f);
  1827. }
  1828. void GlassBsdfNode::compile(SVMCompiler &compiler)
  1829. {
  1830. closure = distribution;
  1831. if (closure == CLOSURE_BSDF_SHARP_GLASS_ID)
  1832. BsdfNode::compile(compiler, NULL, input("IOR"));
  1833. else if (closure == CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID)
  1834. BsdfNode::compile(compiler, input("Roughness"), input("IOR"), input("Color"));
  1835. else
  1836. BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
  1837. }
  1838. void GlassBsdfNode::compile(OSLCompiler &compiler)
  1839. {
  1840. compiler.parameter(this, "distribution");
  1841. compiler.add(this, "node_glass_bsdf");
  1842. }
  1843. /* Refraction BSDF Closure */
  1844. NODE_DEFINE(RefractionBsdfNode)
  1845. {
  1846. NodeType *type = NodeType::add("refraction_bsdf", create, NodeType::SHADER);
  1847. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  1848. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  1849. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  1850. static NodeEnum distribution_enum;
  1851. distribution_enum.insert("sharp", CLOSURE_BSDF_REFRACTION_ID);
  1852. distribution_enum.insert("beckmann", CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID);
  1853. distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
  1854. SOCKET_ENUM(
  1855. distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID);
  1856. SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
  1857. SOCKET_IN_FLOAT(IOR, "IOR", 0.3f);
  1858. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  1859. return type;
  1860. }
  1861. RefractionBsdfNode::RefractionBsdfNode() : BsdfNode(node_type)
  1862. {
  1863. closure = CLOSURE_BSDF_REFRACTION_ID;
  1864. distribution_orig = NBUILTIN_CLOSURES;
  1865. }
  1866. void RefractionBsdfNode::simplify_settings(Scene *scene)
  1867. {
  1868. if (distribution_orig == NBUILTIN_CLOSURES) {
  1869. roughness_orig = roughness;
  1870. distribution_orig = distribution;
  1871. }
  1872. else {
  1873. /* By default we use original values, so we don't worry about restoring
  1874. * defaults later one and can only do override when needed.
  1875. */
  1876. roughness = roughness_orig;
  1877. distribution = distribution_orig;
  1878. }
  1879. Integrator *integrator = scene->integrator;
  1880. ShaderInput *roughness_input = input("Roughness");
  1881. if (integrator->filter_glossy == 0.0f) {
  1882. /* Fallback to Sharp closure for Roughness close to 0.
  1883. * Note: Keep the epsilon in sync with kernel!
  1884. */
  1885. if (!roughness_input->link && roughness <= 1e-4f) {
  1886. VLOG(1) << "Using sharp refraction BSDF.";
  1887. distribution = CLOSURE_BSDF_REFRACTION_ID;
  1888. }
  1889. }
  1890. else {
  1891. /* If filter glossy is used we replace Sharp glossy with GGX so we can
  1892. * benefit from closure blur to remove unwanted noise.
  1893. */
  1894. if (roughness_input->link == NULL && distribution == CLOSURE_BSDF_REFRACTION_ID) {
  1895. VLOG(1) << "Using GGX refraction with filter glossy.";
  1896. distribution = CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID;
  1897. roughness = 0.0f;
  1898. }
  1899. }
  1900. closure = distribution;
  1901. }
  1902. bool RefractionBsdfNode::has_integrator_dependency()
  1903. {
  1904. ShaderInput *roughness_input = input("Roughness");
  1905. return !roughness_input->link &&
  1906. (distribution == CLOSURE_BSDF_REFRACTION_ID || roughness <= 1e-4f);
  1907. }
  1908. void RefractionBsdfNode::compile(SVMCompiler &compiler)
  1909. {
  1910. closure = distribution;
  1911. if (closure == CLOSURE_BSDF_REFRACTION_ID)
  1912. BsdfNode::compile(compiler, NULL, input("IOR"));
  1913. else
  1914. BsdfNode::compile(compiler, input("Roughness"), input("IOR"));
  1915. }
  1916. void RefractionBsdfNode::compile(OSLCompiler &compiler)
  1917. {
  1918. compiler.parameter(this, "distribution");
  1919. compiler.add(this, "node_refraction_bsdf");
  1920. }
  1921. /* Toon BSDF Closure */
  1922. NODE_DEFINE(ToonBsdfNode)
  1923. {
  1924. NodeType *type = NodeType::add("toon_bsdf", create, NodeType::SHADER);
  1925. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  1926. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  1927. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  1928. static NodeEnum component_enum;
  1929. component_enum.insert("diffuse", CLOSURE_BSDF_DIFFUSE_TOON_ID);
  1930. component_enum.insert("glossy", CLOSURE_BSDF_GLOSSY_TOON_ID);
  1931. SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_DIFFUSE_TOON_ID);
  1932. SOCKET_IN_FLOAT(size, "Size", 0.5f);
  1933. SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
  1934. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  1935. return type;
  1936. }
  1937. ToonBsdfNode::ToonBsdfNode() : BsdfNode(node_type)
  1938. {
  1939. closure = CLOSURE_BSDF_DIFFUSE_TOON_ID;
  1940. }
  1941. void ToonBsdfNode::compile(SVMCompiler &compiler)
  1942. {
  1943. closure = component;
  1944. BsdfNode::compile(compiler, input("Size"), input("Smooth"));
  1945. }
  1946. void ToonBsdfNode::compile(OSLCompiler &compiler)
  1947. {
  1948. compiler.parameter(this, "component");
  1949. compiler.add(this, "node_toon_bsdf");
  1950. }
  1951. /* Velvet BSDF Closure */
  1952. NODE_DEFINE(VelvetBsdfNode)
  1953. {
  1954. NodeType *type = NodeType::add("velvet_bsdf", create, NodeType::SHADER);
  1955. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  1956. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  1957. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  1958. SOCKET_IN_FLOAT(sigma, "Sigma", 1.0f);
  1959. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  1960. return type;
  1961. }
  1962. VelvetBsdfNode::VelvetBsdfNode() : BsdfNode(node_type)
  1963. {
  1964. closure = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
  1965. }
  1966. void VelvetBsdfNode::compile(SVMCompiler &compiler)
  1967. {
  1968. BsdfNode::compile(compiler, input("Sigma"), NULL);
  1969. }
  1970. void VelvetBsdfNode::compile(OSLCompiler &compiler)
  1971. {
  1972. compiler.add(this, "node_velvet_bsdf");
  1973. }
  1974. /* Diffuse BSDF Closure */
  1975. NODE_DEFINE(DiffuseBsdfNode)
  1976. {
  1977. NodeType *type = NodeType::add("diffuse_bsdf", create, NodeType::SHADER);
  1978. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  1979. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  1980. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  1981. SOCKET_IN_FLOAT(roughness, "Roughness", 0.0f);
  1982. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  1983. return type;
  1984. }
  1985. DiffuseBsdfNode::DiffuseBsdfNode() : BsdfNode(node_type)
  1986. {
  1987. closure = CLOSURE_BSDF_DIFFUSE_ID;
  1988. }
  1989. void DiffuseBsdfNode::compile(SVMCompiler &compiler)
  1990. {
  1991. BsdfNode::compile(compiler, input("Roughness"), NULL);
  1992. }
  1993. void DiffuseBsdfNode::compile(OSLCompiler &compiler)
  1994. {
  1995. compiler.add(this, "node_diffuse_bsdf");
  1996. }
  1997. /* Disney principled BSDF Closure */
  1998. NODE_DEFINE(PrincipledBsdfNode)
  1999. {
  2000. NodeType *type = NodeType::add("principled_bsdf", create, NodeType::SHADER);
  2001. static NodeEnum distribution_enum;
  2002. distribution_enum.insert("GGX", CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID);
  2003. distribution_enum.insert("Multiscatter GGX", CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
  2004. SOCKET_ENUM(
  2005. distribution, "Distribution", distribution_enum, CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID);
  2006. static NodeEnum subsurface_method_enum;
  2007. subsurface_method_enum.insert("burley", CLOSURE_BSSRDF_PRINCIPLED_ID);
  2008. subsurface_method_enum.insert("random_walk", CLOSURE_BSSRDF_PRINCIPLED_RANDOM_WALK_ID);
  2009. SOCKET_ENUM(subsurface_method,
  2010. "Subsurface Method",
  2011. subsurface_method_enum,
  2012. CLOSURE_BSSRDF_PRINCIPLED_ID);
  2013. SOCKET_IN_COLOR(base_color, "Base Color", make_float3(0.8f, 0.8f, 0.8f));
  2014. SOCKET_IN_COLOR(subsurface_color, "Subsurface Color", make_float3(0.8f, 0.8f, 0.8f));
  2015. SOCKET_IN_FLOAT(metallic, "Metallic", 0.0f);
  2016. SOCKET_IN_FLOAT(subsurface, "Subsurface", 0.0f);
  2017. SOCKET_IN_VECTOR(subsurface_radius, "Subsurface Radius", make_float3(0.1f, 0.1f, 0.1f));
  2018. SOCKET_IN_FLOAT(specular, "Specular", 0.0f);
  2019. SOCKET_IN_FLOAT(roughness, "Roughness", 0.5f);
  2020. SOCKET_IN_FLOAT(specular_tint, "Specular Tint", 0.0f);
  2021. SOCKET_IN_FLOAT(anisotropic, "Anisotropic", 0.0f);
  2022. SOCKET_IN_FLOAT(sheen, "Sheen", 0.0f);
  2023. SOCKET_IN_FLOAT(sheen_tint, "Sheen Tint", 0.0f);
  2024. SOCKET_IN_FLOAT(clearcoat, "Clearcoat", 0.0f);
  2025. SOCKET_IN_FLOAT(clearcoat_roughness, "Clearcoat Roughness", 0.03f);
  2026. SOCKET_IN_FLOAT(ior, "IOR", 0.0f);
  2027. SOCKET_IN_FLOAT(transmission, "Transmission", 0.0f);
  2028. SOCKET_IN_FLOAT(transmission_roughness, "Transmission Roughness", 0.0f);
  2029. SOCKET_IN_FLOAT(anisotropic_rotation, "Anisotropic Rotation", 0.0f);
  2030. SOCKET_IN_COLOR(emission, "Emission", make_float3(0.0f, 0.0f, 0.0f));
  2031. SOCKET_IN_FLOAT(alpha, "Alpha", 1.0f);
  2032. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  2033. SOCKET_IN_NORMAL(clearcoat_normal,
  2034. "Clearcoat Normal",
  2035. make_float3(0.0f, 0.0f, 0.0f),
  2036. SocketType::LINK_NORMAL);
  2037. SOCKET_IN_NORMAL(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_TANGENT);
  2038. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2039. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  2040. return type;
  2041. }
  2042. PrincipledBsdfNode::PrincipledBsdfNode() : BsdfBaseNode(node_type)
  2043. {
  2044. closure = CLOSURE_BSDF_PRINCIPLED_ID;
  2045. distribution = CLOSURE_BSDF_MICROFACET_MULTI_GGX_GLASS_ID;
  2046. distribution_orig = NBUILTIN_CLOSURES;
  2047. }
  2048. void PrincipledBsdfNode::expand(ShaderGraph *graph)
  2049. {
  2050. ShaderOutput *principled_out = output("BSDF");
  2051. ShaderInput *emission_in = input("Emission");
  2052. if (emission_in->link || emission != make_float3(0.0f, 0.0f, 0.0f)) {
  2053. /* Create add closure and emission. */
  2054. AddClosureNode *add = new AddClosureNode();
  2055. EmissionNode *emission_node = new EmissionNode();
  2056. ShaderOutput *new_out = add->output("Closure");
  2057. graph->add(add);
  2058. graph->add(emission_node);
  2059. emission_node->strength = 1.0f;
  2060. graph->relink(emission_in, emission_node->input("Color"));
  2061. graph->relink(principled_out, new_out);
  2062. graph->connect(emission_node->output("Emission"), add->input("Closure1"));
  2063. graph->connect(principled_out, add->input("Closure2"));
  2064. principled_out = new_out;
  2065. }
  2066. ShaderInput *alpha_in = input("Alpha");
  2067. if (alpha_in->link || alpha != 1.0f) {
  2068. /* Create mix and transparent BSDF for alpha transparency. */
  2069. MixClosureNode *mix = new MixClosureNode();
  2070. TransparentBsdfNode *transparent = new TransparentBsdfNode();
  2071. graph->add(mix);
  2072. graph->add(transparent);
  2073. graph->relink(alpha_in, mix->input("Fac"));
  2074. graph->relink(principled_out, mix->output("Closure"));
  2075. graph->connect(transparent->output("BSDF"), mix->input("Closure1"));
  2076. graph->connect(principled_out, mix->input("Closure2"));
  2077. }
  2078. remove_input(emission_in);
  2079. remove_input(alpha_in);
  2080. }
  2081. bool PrincipledBsdfNode::has_surface_bssrdf()
  2082. {
  2083. ShaderInput *subsurface_in = input("Subsurface");
  2084. return (subsurface_in->link != NULL || subsurface > CLOSURE_WEIGHT_CUTOFF);
  2085. }
  2086. void PrincipledBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  2087. {
  2088. if (shader->has_surface) {
  2089. ShaderInput *tangent_in = input("Tangent");
  2090. if (!tangent_in->link)
  2091. attributes->add(ATTR_STD_GENERATED);
  2092. }
  2093. ShaderNode::attributes(shader, attributes);
  2094. }
  2095. void PrincipledBsdfNode::compile(SVMCompiler &compiler,
  2096. ShaderInput *p_metallic,
  2097. ShaderInput *p_subsurface,
  2098. ShaderInput *p_subsurface_radius,
  2099. ShaderInput *p_specular,
  2100. ShaderInput *p_roughness,
  2101. ShaderInput *p_specular_tint,
  2102. ShaderInput *p_anisotropic,
  2103. ShaderInput *p_sheen,
  2104. ShaderInput *p_sheen_tint,
  2105. ShaderInput *p_clearcoat,
  2106. ShaderInput *p_clearcoat_roughness,
  2107. ShaderInput *p_ior,
  2108. ShaderInput *p_transmission,
  2109. ShaderInput *p_anisotropic_rotation,
  2110. ShaderInput *p_transmission_roughness)
  2111. {
  2112. ShaderInput *base_color_in = input("Base Color");
  2113. ShaderInput *subsurface_color_in = input("Subsurface Color");
  2114. ShaderInput *normal_in = input("Normal");
  2115. ShaderInput *clearcoat_normal_in = input("Clearcoat Normal");
  2116. ShaderInput *tangent_in = input("Tangent");
  2117. float3 weight = make_float3(1.0f, 1.0f, 1.0f);
  2118. compiler.add_node(NODE_CLOSURE_SET_WEIGHT, weight);
  2119. int normal_offset = compiler.stack_assign_if_linked(normal_in);
  2120. int clearcoat_normal_offset = compiler.stack_assign_if_linked(clearcoat_normal_in);
  2121. int tangent_offset = compiler.stack_assign_if_linked(tangent_in);
  2122. int specular_offset = compiler.stack_assign(p_specular);
  2123. int roughness_offset = compiler.stack_assign(p_roughness);
  2124. int specular_tint_offset = compiler.stack_assign(p_specular_tint);
  2125. int anisotropic_offset = compiler.stack_assign(p_anisotropic);
  2126. int sheen_offset = compiler.stack_assign(p_sheen);
  2127. int sheen_tint_offset = compiler.stack_assign(p_sheen_tint);
  2128. int clearcoat_offset = compiler.stack_assign(p_clearcoat);
  2129. int clearcoat_roughness_offset = compiler.stack_assign(p_clearcoat_roughness);
  2130. int ior_offset = compiler.stack_assign(p_ior);
  2131. int transmission_offset = compiler.stack_assign(p_transmission);
  2132. int transmission_roughness_offset = compiler.stack_assign(p_transmission_roughness);
  2133. int anisotropic_rotation_offset = compiler.stack_assign(p_anisotropic_rotation);
  2134. int subsurface_radius_offset = compiler.stack_assign(p_subsurface_radius);
  2135. compiler.add_node(NODE_CLOSURE_BSDF,
  2136. compiler.encode_uchar4(closure,
  2137. compiler.stack_assign(p_metallic),
  2138. compiler.stack_assign(p_subsurface),
  2139. compiler.closure_mix_weight_offset()),
  2140. __float_as_int((p_metallic) ? get_float(p_metallic->socket_type) : 0.0f),
  2141. __float_as_int((p_subsurface) ? get_float(p_subsurface->socket_type) : 0.0f));
  2142. compiler.add_node(
  2143. normal_offset,
  2144. tangent_offset,
  2145. compiler.encode_uchar4(
  2146. specular_offset, roughness_offset, specular_tint_offset, anisotropic_offset),
  2147. compiler.encode_uchar4(
  2148. sheen_offset, sheen_tint_offset, clearcoat_offset, clearcoat_roughness_offset));
  2149. compiler.add_node(compiler.encode_uchar4(ior_offset,
  2150. transmission_offset,
  2151. anisotropic_rotation_offset,
  2152. transmission_roughness_offset),
  2153. distribution,
  2154. subsurface_method,
  2155. SVM_STACK_INVALID);
  2156. float3 bc_default = get_float3(base_color_in->socket_type);
  2157. compiler.add_node(
  2158. ((base_color_in->link) ? compiler.stack_assign(base_color_in) : SVM_STACK_INVALID),
  2159. __float_as_int(bc_default.x),
  2160. __float_as_int(bc_default.y),
  2161. __float_as_int(bc_default.z));
  2162. compiler.add_node(
  2163. clearcoat_normal_offset, subsurface_radius_offset, SVM_STACK_INVALID, SVM_STACK_INVALID);
  2164. float3 ss_default = get_float3(subsurface_color_in->socket_type);
  2165. compiler.add_node(((subsurface_color_in->link) ? compiler.stack_assign(subsurface_color_in) :
  2166. SVM_STACK_INVALID),
  2167. __float_as_int(ss_default.x),
  2168. __float_as_int(ss_default.y),
  2169. __float_as_int(ss_default.z));
  2170. }
  2171. bool PrincipledBsdfNode::has_integrator_dependency()
  2172. {
  2173. ShaderInput *roughness_input = input("Roughness");
  2174. return !roughness_input->link && roughness <= 1e-4f;
  2175. }
  2176. void PrincipledBsdfNode::compile(SVMCompiler &compiler)
  2177. {
  2178. compile(compiler,
  2179. input("Metallic"),
  2180. input("Subsurface"),
  2181. input("Subsurface Radius"),
  2182. input("Specular"),
  2183. input("Roughness"),
  2184. input("Specular Tint"),
  2185. input("Anisotropic"),
  2186. input("Sheen"),
  2187. input("Sheen Tint"),
  2188. input("Clearcoat"),
  2189. input("Clearcoat Roughness"),
  2190. input("IOR"),
  2191. input("Transmission"),
  2192. input("Anisotropic Rotation"),
  2193. input("Transmission Roughness"));
  2194. }
  2195. void PrincipledBsdfNode::compile(OSLCompiler &compiler)
  2196. {
  2197. compiler.parameter(this, "distribution");
  2198. compiler.parameter(this, "subsurface_method");
  2199. compiler.add(this, "node_principled_bsdf");
  2200. }
  2201. bool PrincipledBsdfNode::has_bssrdf_bump()
  2202. {
  2203. return has_surface_bssrdf() && has_bump();
  2204. }
  2205. /* Translucent BSDF Closure */
  2206. NODE_DEFINE(TranslucentBsdfNode)
  2207. {
  2208. NodeType *type = NodeType::add("translucent_bsdf", create, NodeType::SHADER);
  2209. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  2210. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  2211. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2212. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  2213. return type;
  2214. }
  2215. TranslucentBsdfNode::TranslucentBsdfNode() : BsdfNode(node_type)
  2216. {
  2217. closure = CLOSURE_BSDF_TRANSLUCENT_ID;
  2218. }
  2219. void TranslucentBsdfNode::compile(SVMCompiler &compiler)
  2220. {
  2221. BsdfNode::compile(compiler, NULL, NULL);
  2222. }
  2223. void TranslucentBsdfNode::compile(OSLCompiler &compiler)
  2224. {
  2225. compiler.add(this, "node_translucent_bsdf");
  2226. }
  2227. /* Transparent BSDF Closure */
  2228. NODE_DEFINE(TransparentBsdfNode)
  2229. {
  2230. NodeType *type = NodeType::add("transparent_bsdf", create, NodeType::SHADER);
  2231. SOCKET_IN_COLOR(color, "Color", make_float3(1.0f, 1.0f, 1.0f));
  2232. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2233. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  2234. return type;
  2235. }
  2236. TransparentBsdfNode::TransparentBsdfNode() : BsdfNode(node_type)
  2237. {
  2238. closure = CLOSURE_BSDF_TRANSPARENT_ID;
  2239. }
  2240. void TransparentBsdfNode::compile(SVMCompiler &compiler)
  2241. {
  2242. BsdfNode::compile(compiler, NULL, NULL);
  2243. }
  2244. void TransparentBsdfNode::compile(OSLCompiler &compiler)
  2245. {
  2246. compiler.add(this, "node_transparent_bsdf");
  2247. }
  2248. /* Subsurface Scattering Closure */
  2249. NODE_DEFINE(SubsurfaceScatteringNode)
  2250. {
  2251. NodeType *type = NodeType::add("subsurface_scattering", create, NodeType::SHADER);
  2252. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  2253. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  2254. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2255. static NodeEnum falloff_enum;
  2256. falloff_enum.insert("cubic", CLOSURE_BSSRDF_CUBIC_ID);
  2257. falloff_enum.insert("gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
  2258. falloff_enum.insert("burley", CLOSURE_BSSRDF_BURLEY_ID);
  2259. falloff_enum.insert("random_walk", CLOSURE_BSSRDF_RANDOM_WALK_ID);
  2260. SOCKET_ENUM(falloff, "Falloff", falloff_enum, CLOSURE_BSSRDF_BURLEY_ID);
  2261. SOCKET_IN_FLOAT(scale, "Scale", 0.01f);
  2262. SOCKET_IN_VECTOR(radius, "Radius", make_float3(0.1f, 0.1f, 0.1f));
  2263. SOCKET_IN_FLOAT(sharpness, "Sharpness", 0.0f);
  2264. SOCKET_IN_FLOAT(texture_blur, "Texture Blur", 1.0f);
  2265. SOCKET_OUT_CLOSURE(BSSRDF, "BSSRDF");
  2266. return type;
  2267. }
  2268. SubsurfaceScatteringNode::SubsurfaceScatteringNode() : BsdfNode(node_type)
  2269. {
  2270. closure = falloff;
  2271. }
  2272. void SubsurfaceScatteringNode::compile(SVMCompiler &compiler)
  2273. {
  2274. closure = falloff;
  2275. BsdfNode::compile(
  2276. compiler, input("Scale"), input("Texture Blur"), input("Radius"), input("Sharpness"));
  2277. }
  2278. void SubsurfaceScatteringNode::compile(OSLCompiler &compiler)
  2279. {
  2280. closure = falloff;
  2281. compiler.parameter(this, "falloff");
  2282. compiler.add(this, "node_subsurface_scattering");
  2283. }
  2284. bool SubsurfaceScatteringNode::has_bssrdf_bump()
  2285. {
  2286. /* detect if anything is plugged into the normal input besides the default */
  2287. ShaderInput *normal_in = input("Normal");
  2288. return (normal_in->link &&
  2289. normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
  2290. }
  2291. /* Emissive Closure */
  2292. NODE_DEFINE(EmissionNode)
  2293. {
  2294. NodeType *type = NodeType::add("emission", create, NodeType::SHADER);
  2295. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  2296. SOCKET_IN_FLOAT(strength, "Strength", 10.0f);
  2297. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2298. SOCKET_OUT_CLOSURE(emission, "Emission");
  2299. return type;
  2300. }
  2301. EmissionNode::EmissionNode() : ShaderNode(node_type)
  2302. {
  2303. }
  2304. void EmissionNode::compile(SVMCompiler &compiler)
  2305. {
  2306. ShaderInput *color_in = input("Color");
  2307. ShaderInput *strength_in = input("Strength");
  2308. if (color_in->link || strength_in->link) {
  2309. compiler.add_node(
  2310. NODE_EMISSION_WEIGHT, compiler.stack_assign(color_in), compiler.stack_assign(strength_in));
  2311. }
  2312. else
  2313. compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color * strength);
  2314. compiler.add_node(NODE_CLOSURE_EMISSION, compiler.closure_mix_weight_offset());
  2315. }
  2316. void EmissionNode::compile(OSLCompiler &compiler)
  2317. {
  2318. compiler.add(this, "node_emission");
  2319. }
  2320. void EmissionNode::constant_fold(const ConstantFolder &folder)
  2321. {
  2322. ShaderInput *color_in = input("Color");
  2323. ShaderInput *strength_in = input("Strength");
  2324. if ((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
  2325. (!strength_in->link && strength == 0.0f)) {
  2326. folder.discard();
  2327. }
  2328. }
  2329. /* Background Closure */
  2330. NODE_DEFINE(BackgroundNode)
  2331. {
  2332. NodeType *type = NodeType::add("background_shader", create, NodeType::SHADER);
  2333. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  2334. SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
  2335. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2336. SOCKET_OUT_CLOSURE(background, "Background");
  2337. return type;
  2338. }
  2339. BackgroundNode::BackgroundNode() : ShaderNode(node_type)
  2340. {
  2341. }
  2342. void BackgroundNode::compile(SVMCompiler &compiler)
  2343. {
  2344. ShaderInput *color_in = input("Color");
  2345. ShaderInput *strength_in = input("Strength");
  2346. if (color_in->link || strength_in->link) {
  2347. compiler.add_node(
  2348. NODE_EMISSION_WEIGHT, compiler.stack_assign(color_in), compiler.stack_assign(strength_in));
  2349. }
  2350. else
  2351. compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color * strength);
  2352. compiler.add_node(NODE_CLOSURE_BACKGROUND, compiler.closure_mix_weight_offset());
  2353. }
  2354. void BackgroundNode::compile(OSLCompiler &compiler)
  2355. {
  2356. compiler.add(this, "node_background");
  2357. }
  2358. void BackgroundNode::constant_fold(const ConstantFolder &folder)
  2359. {
  2360. ShaderInput *color_in = input("Color");
  2361. ShaderInput *strength_in = input("Strength");
  2362. if ((!color_in->link && color == make_float3(0.0f, 0.0f, 0.0f)) ||
  2363. (!strength_in->link && strength == 0.0f)) {
  2364. folder.discard();
  2365. }
  2366. }
  2367. /* Holdout Closure */
  2368. NODE_DEFINE(HoldoutNode)
  2369. {
  2370. NodeType *type = NodeType::add("holdout", create, NodeType::SHADER);
  2371. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2372. SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2373. SOCKET_OUT_CLOSURE(holdout, "Holdout");
  2374. return type;
  2375. }
  2376. HoldoutNode::HoldoutNode() : ShaderNode(node_type)
  2377. {
  2378. }
  2379. void HoldoutNode::compile(SVMCompiler &compiler)
  2380. {
  2381. float3 value = make_float3(1.0f, 1.0f, 1.0f);
  2382. compiler.add_node(NODE_CLOSURE_SET_WEIGHT, value);
  2383. compiler.add_node(NODE_CLOSURE_HOLDOUT, compiler.closure_mix_weight_offset());
  2384. }
  2385. void HoldoutNode::compile(OSLCompiler &compiler)
  2386. {
  2387. compiler.add(this, "node_holdout");
  2388. }
  2389. /* Ambient Occlusion */
  2390. NODE_DEFINE(AmbientOcclusionNode)
  2391. {
  2392. NodeType *type = NodeType::add("ambient_occlusion", create, NodeType::SHADER);
  2393. SOCKET_INT(samples, "Samples", 16);
  2394. SOCKET_IN_COLOR(color, "Color", make_float3(1.0f, 1.0f, 1.0f));
  2395. SOCKET_IN_FLOAT(distance, "Distance", 1.0f);
  2396. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  2397. SOCKET_BOOLEAN(inside, "Inside", false);
  2398. SOCKET_BOOLEAN(only_local, "Only Local", false);
  2399. SOCKET_OUT_COLOR(color, "Color");
  2400. SOCKET_OUT_FLOAT(ao, "AO");
  2401. return type;
  2402. }
  2403. AmbientOcclusionNode::AmbientOcclusionNode() : ShaderNode(node_type)
  2404. {
  2405. }
  2406. void AmbientOcclusionNode::compile(SVMCompiler &compiler)
  2407. {
  2408. ShaderInput *color_in = input("Color");
  2409. ShaderInput *distance_in = input("Distance");
  2410. ShaderInput *normal_in = input("Normal");
  2411. ShaderOutput *color_out = output("Color");
  2412. ShaderOutput *ao_out = output("AO");
  2413. int flags = (inside ? NODE_AO_INSIDE : 0) | (only_local ? NODE_AO_ONLY_LOCAL : 0);
  2414. if (!distance_in->link && distance == 0.0f) {
  2415. flags |= NODE_AO_GLOBAL_RADIUS;
  2416. }
  2417. compiler.add_node(NODE_AMBIENT_OCCLUSION,
  2418. compiler.encode_uchar4(flags,
  2419. compiler.stack_assign_if_linked(distance_in),
  2420. compiler.stack_assign_if_linked(normal_in),
  2421. compiler.stack_assign(ao_out)),
  2422. compiler.encode_uchar4(compiler.stack_assign(color_in),
  2423. compiler.stack_assign(color_out),
  2424. samples),
  2425. __float_as_uint(distance));
  2426. }
  2427. void AmbientOcclusionNode::compile(OSLCompiler &compiler)
  2428. {
  2429. compiler.parameter(this, "samples");
  2430. compiler.parameter(this, "inside");
  2431. compiler.parameter(this, "only_local");
  2432. compiler.add(this, "node_ambient_occlusion");
  2433. }
  2434. /* Volume Closure */
  2435. VolumeNode::VolumeNode(const NodeType *node_type) : ShaderNode(node_type)
  2436. {
  2437. closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
  2438. }
  2439. void VolumeNode::compile(SVMCompiler &compiler, ShaderInput *param1, ShaderInput *param2)
  2440. {
  2441. ShaderInput *color_in = input("Color");
  2442. if (color_in->link)
  2443. compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
  2444. else
  2445. compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
  2446. compiler.add_node(
  2447. NODE_CLOSURE_VOLUME,
  2448. compiler.encode_uchar4(closure,
  2449. (param1) ? compiler.stack_assign(param1) : SVM_STACK_INVALID,
  2450. (param2) ? compiler.stack_assign(param2) : SVM_STACK_INVALID,
  2451. compiler.closure_mix_weight_offset()),
  2452. __float_as_int((param1) ? get_float(param1->socket_type) : 0.0f),
  2453. __float_as_int((param2) ? get_float(param2->socket_type) : 0.0f));
  2454. }
  2455. void VolumeNode::compile(SVMCompiler &compiler)
  2456. {
  2457. compile(compiler, NULL, NULL);
  2458. }
  2459. void VolumeNode::compile(OSLCompiler & /*compiler*/)
  2460. {
  2461. assert(0);
  2462. }
  2463. /* Absorption Volume Closure */
  2464. NODE_DEFINE(AbsorptionVolumeNode)
  2465. {
  2466. NodeType *type = NodeType::add("absorption_volume", create, NodeType::SHADER);
  2467. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  2468. SOCKET_IN_FLOAT(density, "Density", 1.0f);
  2469. SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2470. SOCKET_OUT_CLOSURE(volume, "Volume");
  2471. return type;
  2472. }
  2473. AbsorptionVolumeNode::AbsorptionVolumeNode() : VolumeNode(node_type)
  2474. {
  2475. closure = CLOSURE_VOLUME_ABSORPTION_ID;
  2476. }
  2477. void AbsorptionVolumeNode::compile(SVMCompiler &compiler)
  2478. {
  2479. VolumeNode::compile(compiler, input("Density"), NULL);
  2480. }
  2481. void AbsorptionVolumeNode::compile(OSLCompiler &compiler)
  2482. {
  2483. compiler.add(this, "node_absorption_volume");
  2484. }
  2485. /* Scatter Volume Closure */
  2486. NODE_DEFINE(ScatterVolumeNode)
  2487. {
  2488. NodeType *type = NodeType::add("scatter_volume", create, NodeType::SHADER);
  2489. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  2490. SOCKET_IN_FLOAT(density, "Density", 1.0f);
  2491. SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
  2492. SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2493. SOCKET_OUT_CLOSURE(volume, "Volume");
  2494. return type;
  2495. }
  2496. ScatterVolumeNode::ScatterVolumeNode() : VolumeNode(node_type)
  2497. {
  2498. closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
  2499. }
  2500. void ScatterVolumeNode::compile(SVMCompiler &compiler)
  2501. {
  2502. VolumeNode::compile(compiler, input("Density"), input("Anisotropy"));
  2503. }
  2504. void ScatterVolumeNode::compile(OSLCompiler &compiler)
  2505. {
  2506. compiler.add(this, "node_scatter_volume");
  2507. }
  2508. /* Principled Volume Closure */
  2509. NODE_DEFINE(PrincipledVolumeNode)
  2510. {
  2511. NodeType *type = NodeType::add("principled_volume", create, NodeType::SHADER);
  2512. SOCKET_IN_STRING(density_attribute, "Density Attribute", ustring());
  2513. SOCKET_IN_STRING(color_attribute, "Color Attribute", ustring());
  2514. SOCKET_IN_STRING(temperature_attribute, "Temperature Attribute", ustring());
  2515. SOCKET_IN_COLOR(color, "Color", make_float3(0.5f, 0.5f, 0.5f));
  2516. SOCKET_IN_FLOAT(density, "Density", 1.0f);
  2517. SOCKET_IN_FLOAT(anisotropy, "Anisotropy", 0.0f);
  2518. SOCKET_IN_COLOR(absorption_color, "Absorption Color", make_float3(0.0f, 0.0f, 0.0f));
  2519. SOCKET_IN_FLOAT(emission_strength, "Emission Strength", 0.0f);
  2520. SOCKET_IN_COLOR(emission_color, "Emission Color", make_float3(1.0f, 1.0f, 1.0f));
  2521. SOCKET_IN_FLOAT(blackbody_intensity, "Blackbody Intensity", 0.0f);
  2522. SOCKET_IN_COLOR(blackbody_tint, "Blackbody Tint", make_float3(1.0f, 1.0f, 1.0f));
  2523. SOCKET_IN_FLOAT(temperature, "Temperature", 1500.0f);
  2524. SOCKET_IN_FLOAT(volume_mix_weight, "VolumeMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2525. SOCKET_OUT_CLOSURE(volume, "Volume");
  2526. return type;
  2527. }
  2528. PrincipledVolumeNode::PrincipledVolumeNode() : VolumeNode(node_type)
  2529. {
  2530. closure = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
  2531. }
  2532. void PrincipledVolumeNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  2533. {
  2534. if (shader->has_volume) {
  2535. ShaderInput *density_in = input("Density");
  2536. ShaderInput *blackbody_in = input("Blackbody Intensity");
  2537. if (density_in->link || density > 0.0f) {
  2538. attributes->add_standard(density_attribute);
  2539. attributes->add_standard(color_attribute);
  2540. }
  2541. if (blackbody_in->link || blackbody_intensity > 0.0f) {
  2542. attributes->add_standard(temperature_attribute);
  2543. }
  2544. attributes->add(ATTR_STD_GENERATED_TRANSFORM);
  2545. }
  2546. ShaderNode::attributes(shader, attributes);
  2547. }
  2548. void PrincipledVolumeNode::compile(SVMCompiler &compiler)
  2549. {
  2550. ShaderInput *color_in = input("Color");
  2551. ShaderInput *density_in = input("Density");
  2552. ShaderInput *anisotropy_in = input("Anisotropy");
  2553. ShaderInput *absorption_color_in = input("Absorption Color");
  2554. ShaderInput *emission_in = input("Emission Strength");
  2555. ShaderInput *emission_color_in = input("Emission Color");
  2556. ShaderInput *blackbody_in = input("Blackbody Intensity");
  2557. ShaderInput *blackbody_tint_in = input("Blackbody Tint");
  2558. ShaderInput *temperature_in = input("Temperature");
  2559. if (color_in->link)
  2560. compiler.add_node(NODE_CLOSURE_WEIGHT, compiler.stack_assign(color_in));
  2561. else
  2562. compiler.add_node(NODE_CLOSURE_SET_WEIGHT, color);
  2563. compiler.add_node(NODE_PRINCIPLED_VOLUME,
  2564. compiler.encode_uchar4(compiler.stack_assign_if_linked(density_in),
  2565. compiler.stack_assign_if_linked(anisotropy_in),
  2566. compiler.stack_assign(absorption_color_in),
  2567. compiler.closure_mix_weight_offset()),
  2568. compiler.encode_uchar4(compiler.stack_assign_if_linked(emission_in),
  2569. compiler.stack_assign(emission_color_in),
  2570. compiler.stack_assign_if_linked(blackbody_in),
  2571. compiler.stack_assign(temperature_in)),
  2572. compiler.stack_assign(blackbody_tint_in));
  2573. int attr_density = compiler.attribute_standard(density_attribute);
  2574. int attr_color = compiler.attribute_standard(color_attribute);
  2575. int attr_temperature = compiler.attribute_standard(temperature_attribute);
  2576. compiler.add_node(__float_as_int(density),
  2577. __float_as_int(anisotropy),
  2578. __float_as_int(emission_strength),
  2579. __float_as_int(blackbody_intensity));
  2580. compiler.add_node(attr_density, attr_color, attr_temperature);
  2581. }
  2582. void PrincipledVolumeNode::compile(OSLCompiler &compiler)
  2583. {
  2584. if (Attribute::name_standard(density_attribute.c_str())) {
  2585. density_attribute = ustring("geom:" + density_attribute.string());
  2586. }
  2587. if (Attribute::name_standard(color_attribute.c_str())) {
  2588. color_attribute = ustring("geom:" + color_attribute.string());
  2589. }
  2590. if (Attribute::name_standard(temperature_attribute.c_str())) {
  2591. temperature_attribute = ustring("geom:" + temperature_attribute.string());
  2592. }
  2593. compiler.add(this, "node_principled_volume");
  2594. }
  2595. /* Principled Hair BSDF Closure */
  2596. NODE_DEFINE(PrincipledHairBsdfNode)
  2597. {
  2598. NodeType *type = NodeType::add("principled_hair_bsdf", create, NodeType::SHADER);
  2599. /* Color parametrization specified as enum. */
  2600. static NodeEnum parametrization_enum;
  2601. parametrization_enum.insert("Direct coloring", NODE_PRINCIPLED_HAIR_REFLECTANCE);
  2602. parametrization_enum.insert("Melanin concentration", NODE_PRINCIPLED_HAIR_PIGMENT_CONCENTRATION);
  2603. parametrization_enum.insert("Absorption coefficient", NODE_PRINCIPLED_HAIR_DIRECT_ABSORPTION);
  2604. SOCKET_ENUM(
  2605. parametrization, "Parametrization", parametrization_enum, NODE_PRINCIPLED_HAIR_REFLECTANCE);
  2606. /* Initialize sockets to their default values. */
  2607. SOCKET_IN_COLOR(color, "Color", make_float3(0.017513f, 0.005763f, 0.002059f));
  2608. SOCKET_IN_FLOAT(melanin, "Melanin", 0.8f);
  2609. SOCKET_IN_FLOAT(melanin_redness, "Melanin Redness", 1.0f);
  2610. SOCKET_IN_COLOR(tint, "Tint", make_float3(1.f, 1.f, 1.f));
  2611. SOCKET_IN_VECTOR(absorption_coefficient,
  2612. "Absorption Coefficient",
  2613. make_float3(0.245531f, 0.52f, 1.365f),
  2614. SocketType::VECTOR);
  2615. SOCKET_IN_FLOAT(offset, "Offset", 2.f * M_PI_F / 180.f);
  2616. SOCKET_IN_FLOAT(roughness, "Roughness", 0.3f);
  2617. SOCKET_IN_FLOAT(radial_roughness, "Radial Roughness", 0.3f);
  2618. SOCKET_IN_FLOAT(coat, "Coat", 0.0f);
  2619. SOCKET_IN_FLOAT(ior, "IOR", 1.55f);
  2620. SOCKET_IN_FLOAT(random_roughness, "Random Roughness", 0.0f);
  2621. SOCKET_IN_FLOAT(random_color, "Random Color", 0.0f);
  2622. SOCKET_IN_FLOAT(random, "Random", 0.0f);
  2623. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  2624. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2625. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  2626. return type;
  2627. }
  2628. PrincipledHairBsdfNode::PrincipledHairBsdfNode() : BsdfBaseNode(node_type)
  2629. {
  2630. closure = CLOSURE_BSDF_HAIR_PRINCIPLED_ID;
  2631. }
  2632. /* Enable retrieving Hair Info -> Random if Random isn't linked. */
  2633. void PrincipledHairBsdfNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  2634. {
  2635. if (!input("Random")->link) {
  2636. attributes->add(ATTR_STD_CURVE_RANDOM);
  2637. }
  2638. ShaderNode::attributes(shader, attributes);
  2639. }
  2640. /* Prepares the input data for the SVM shader. */
  2641. void PrincipledHairBsdfNode::compile(SVMCompiler &compiler)
  2642. {
  2643. compiler.add_node(NODE_CLOSURE_SET_WEIGHT, make_float3(1.0f, 1.0f, 1.0f));
  2644. ShaderInput *roughness_in = input("Roughness");
  2645. ShaderInput *radial_roughness_in = input("Radial Roughness");
  2646. ShaderInput *random_roughness_in = input("Random Roughness");
  2647. ShaderInput *offset_in = input("Offset");
  2648. ShaderInput *coat_in = input("Coat");
  2649. ShaderInput *ior_in = input("IOR");
  2650. ShaderInput *melanin_in = input("Melanin");
  2651. ShaderInput *melanin_redness_in = input("Melanin Redness");
  2652. ShaderInput *random_color_in = input("Random Color");
  2653. int color_ofs = compiler.stack_assign(input("Color"));
  2654. int tint_ofs = compiler.stack_assign(input("Tint"));
  2655. int absorption_coefficient_ofs = compiler.stack_assign(input("Absorption Coefficient"));
  2656. ShaderInput *random_in = input("Random");
  2657. int attr_random = random_in->link ? SVM_STACK_INVALID :
  2658. compiler.attribute(ATTR_STD_CURVE_RANDOM);
  2659. /* Encode all parameters into data nodes. */
  2660. compiler.add_node(NODE_CLOSURE_BSDF,
  2661. /* Socket IDs can be packed 4 at a time into a single data packet */
  2662. compiler.encode_uchar4(closure,
  2663. compiler.stack_assign_if_linked(roughness_in),
  2664. compiler.stack_assign_if_linked(radial_roughness_in),
  2665. compiler.closure_mix_weight_offset()),
  2666. /* The rest are stored as unsigned integers */
  2667. __float_as_uint(roughness),
  2668. __float_as_uint(radial_roughness));
  2669. compiler.add_node(compiler.stack_assign_if_linked(input("Normal")),
  2670. compiler.encode_uchar4(compiler.stack_assign_if_linked(offset_in),
  2671. compiler.stack_assign_if_linked(ior_in),
  2672. color_ofs,
  2673. parametrization),
  2674. __float_as_uint(offset),
  2675. __float_as_uint(ior));
  2676. compiler.add_node(compiler.encode_uchar4(compiler.stack_assign_if_linked(coat_in),
  2677. compiler.stack_assign_if_linked(melanin_in),
  2678. compiler.stack_assign_if_linked(melanin_redness_in),
  2679. absorption_coefficient_ofs),
  2680. __float_as_uint(coat),
  2681. __float_as_uint(melanin),
  2682. __float_as_uint(melanin_redness));
  2683. compiler.add_node(compiler.encode_uchar4(tint_ofs,
  2684. compiler.stack_assign_if_linked(random_in),
  2685. compiler.stack_assign_if_linked(random_color_in),
  2686. compiler.stack_assign_if_linked(random_roughness_in)),
  2687. __float_as_uint(random),
  2688. __float_as_uint(random_color),
  2689. __float_as_uint(random_roughness));
  2690. compiler.add_node(
  2691. compiler.encode_uchar4(
  2692. SVM_STACK_INVALID, SVM_STACK_INVALID, SVM_STACK_INVALID, SVM_STACK_INVALID),
  2693. attr_random,
  2694. SVM_STACK_INVALID,
  2695. SVM_STACK_INVALID);
  2696. }
  2697. /* Prepares the input data for the OSL shader. */
  2698. void PrincipledHairBsdfNode::compile(OSLCompiler &compiler)
  2699. {
  2700. compiler.parameter(this, "parametrization");
  2701. compiler.add(this, "node_principled_hair_bsdf");
  2702. }
  2703. /* Hair BSDF Closure */
  2704. NODE_DEFINE(HairBsdfNode)
  2705. {
  2706. NodeType *type = NodeType::add("hair_bsdf", create, NodeType::SHADER);
  2707. SOCKET_IN_COLOR(color, "Color", make_float3(0.8f, 0.8f, 0.8f));
  2708. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  2709. SOCKET_IN_FLOAT(surface_mix_weight, "SurfaceMixWeight", 0.0f, SocketType::SVM_INTERNAL);
  2710. static NodeEnum component_enum;
  2711. component_enum.insert("reflection", CLOSURE_BSDF_HAIR_REFLECTION_ID);
  2712. component_enum.insert("transmission", CLOSURE_BSDF_HAIR_TRANSMISSION_ID);
  2713. SOCKET_ENUM(component, "Component", component_enum, CLOSURE_BSDF_HAIR_REFLECTION_ID);
  2714. SOCKET_IN_FLOAT(offset, "Offset", 0.0f);
  2715. SOCKET_IN_FLOAT(roughness_u, "RoughnessU", 0.2f);
  2716. SOCKET_IN_FLOAT(roughness_v, "RoughnessV", 0.2f);
  2717. SOCKET_IN_VECTOR(tangent, "Tangent", make_float3(0.0f, 0.0f, 0.0f));
  2718. SOCKET_OUT_CLOSURE(BSDF, "BSDF");
  2719. return type;
  2720. }
  2721. HairBsdfNode::HairBsdfNode() : BsdfNode(node_type)
  2722. {
  2723. closure = CLOSURE_BSDF_HAIR_REFLECTION_ID;
  2724. }
  2725. void HairBsdfNode::compile(SVMCompiler &compiler)
  2726. {
  2727. closure = component;
  2728. BsdfNode::compile(compiler, input("RoughnessU"), input("RoughnessV"), input("Offset"));
  2729. }
  2730. void HairBsdfNode::compile(OSLCompiler &compiler)
  2731. {
  2732. compiler.parameter(this, "component");
  2733. compiler.add(this, "node_hair_bsdf");
  2734. }
  2735. /* Geometry */
  2736. NODE_DEFINE(GeometryNode)
  2737. {
  2738. NodeType *type = NodeType::add("geometry", create, NodeType::SHADER);
  2739. SOCKET_IN_NORMAL(normal_osl,
  2740. "NormalIn",
  2741. make_float3(0.0f, 0.0f, 0.0f),
  2742. SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
  2743. SOCKET_OUT_POINT(position, "Position");
  2744. SOCKET_OUT_NORMAL(normal, "Normal");
  2745. SOCKET_OUT_NORMAL(tangent, "Tangent");
  2746. SOCKET_OUT_NORMAL(true_normal, "True Normal");
  2747. SOCKET_OUT_VECTOR(incoming, "Incoming");
  2748. SOCKET_OUT_POINT(parametric, "Parametric");
  2749. SOCKET_OUT_FLOAT(backfacing, "Backfacing");
  2750. SOCKET_OUT_FLOAT(pointiness, "Pointiness");
  2751. return type;
  2752. }
  2753. GeometryNode::GeometryNode() : ShaderNode(node_type)
  2754. {
  2755. special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
  2756. }
  2757. void GeometryNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  2758. {
  2759. if (shader->has_surface) {
  2760. if (!output("Tangent")->links.empty()) {
  2761. attributes->add(ATTR_STD_GENERATED);
  2762. }
  2763. if (!output("Pointiness")->links.empty()) {
  2764. attributes->add(ATTR_STD_POINTINESS);
  2765. }
  2766. }
  2767. ShaderNode::attributes(shader, attributes);
  2768. }
  2769. void GeometryNode::compile(SVMCompiler &compiler)
  2770. {
  2771. ShaderOutput *out;
  2772. ShaderNodeType geom_node = NODE_GEOMETRY;
  2773. ShaderNodeType attr_node = NODE_ATTR;
  2774. if (bump == SHADER_BUMP_DX) {
  2775. geom_node = NODE_GEOMETRY_BUMP_DX;
  2776. attr_node = NODE_ATTR_BUMP_DX;
  2777. }
  2778. else if (bump == SHADER_BUMP_DY) {
  2779. geom_node = NODE_GEOMETRY_BUMP_DY;
  2780. attr_node = NODE_ATTR_BUMP_DY;
  2781. }
  2782. out = output("Position");
  2783. if (!out->links.empty()) {
  2784. compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
  2785. }
  2786. out = output("Normal");
  2787. if (!out->links.empty()) {
  2788. compiler.add_node(geom_node, NODE_GEOM_N, compiler.stack_assign(out));
  2789. }
  2790. out = output("Tangent");
  2791. if (!out->links.empty()) {
  2792. compiler.add_node(geom_node, NODE_GEOM_T, compiler.stack_assign(out));
  2793. }
  2794. out = output("True Normal");
  2795. if (!out->links.empty()) {
  2796. compiler.add_node(geom_node, NODE_GEOM_Ng, compiler.stack_assign(out));
  2797. }
  2798. out = output("Incoming");
  2799. if (!out->links.empty()) {
  2800. compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
  2801. }
  2802. out = output("Parametric");
  2803. if (!out->links.empty()) {
  2804. compiler.add_node(geom_node, NODE_GEOM_uv, compiler.stack_assign(out));
  2805. }
  2806. out = output("Backfacing");
  2807. if (!out->links.empty()) {
  2808. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_backfacing, compiler.stack_assign(out));
  2809. }
  2810. out = output("Pointiness");
  2811. if (!out->links.empty()) {
  2812. if (compiler.output_type() != SHADER_TYPE_VOLUME) {
  2813. compiler.add_node(
  2814. attr_node, ATTR_STD_POINTINESS, compiler.stack_assign(out), NODE_ATTR_FLOAT);
  2815. }
  2816. else {
  2817. compiler.add_node(NODE_VALUE_F, __float_as_int(0.0f), compiler.stack_assign(out));
  2818. }
  2819. }
  2820. }
  2821. void GeometryNode::compile(OSLCompiler &compiler)
  2822. {
  2823. if (bump == SHADER_BUMP_DX)
  2824. compiler.parameter("bump_offset", "dx");
  2825. else if (bump == SHADER_BUMP_DY)
  2826. compiler.parameter("bump_offset", "dy");
  2827. else
  2828. compiler.parameter("bump_offset", "center");
  2829. compiler.add(this, "node_geometry");
  2830. }
  2831. int GeometryNode::get_group()
  2832. {
  2833. ShaderOutput *out;
  2834. int result = ShaderNode::get_group();
  2835. /* Backfacing uses NODE_LIGHT_PATH */
  2836. out = output("Backfacing");
  2837. if (!out->links.empty()) {
  2838. result = max(result, NODE_GROUP_LEVEL_1);
  2839. }
  2840. return result;
  2841. }
  2842. /* TextureCoordinate */
  2843. NODE_DEFINE(TextureCoordinateNode)
  2844. {
  2845. NodeType *type = NodeType::add("texture_coordinate", create, NodeType::SHADER);
  2846. SOCKET_BOOLEAN(from_dupli, "From Dupli", false);
  2847. SOCKET_BOOLEAN(use_transform, "Use Transform", false);
  2848. SOCKET_TRANSFORM(ob_tfm, "Object Transform", transform_identity());
  2849. SOCKET_IN_NORMAL(normal_osl,
  2850. "NormalIn",
  2851. make_float3(0.0f, 0.0f, 0.0f),
  2852. SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
  2853. SOCKET_OUT_POINT(generated, "Generated");
  2854. SOCKET_OUT_NORMAL(normal, "Normal");
  2855. SOCKET_OUT_POINT(UV, "UV");
  2856. SOCKET_OUT_POINT(object, "Object");
  2857. SOCKET_OUT_POINT(camera, "Camera");
  2858. SOCKET_OUT_POINT(window, "Window");
  2859. SOCKET_OUT_NORMAL(reflection, "Reflection");
  2860. return type;
  2861. }
  2862. TextureCoordinateNode::TextureCoordinateNode() : ShaderNode(node_type)
  2863. {
  2864. }
  2865. void TextureCoordinateNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  2866. {
  2867. if (shader->has_surface) {
  2868. if (!from_dupli) {
  2869. if (!output("Generated")->links.empty())
  2870. attributes->add(ATTR_STD_GENERATED);
  2871. if (!output("UV")->links.empty())
  2872. attributes->add(ATTR_STD_UV);
  2873. }
  2874. }
  2875. if (shader->has_volume) {
  2876. if (!from_dupli) {
  2877. if (!output("Generated")->links.empty()) {
  2878. attributes->add(ATTR_STD_GENERATED_TRANSFORM);
  2879. }
  2880. }
  2881. }
  2882. ShaderNode::attributes(shader, attributes);
  2883. }
  2884. void TextureCoordinateNode::compile(SVMCompiler &compiler)
  2885. {
  2886. ShaderOutput *out;
  2887. ShaderNodeType texco_node = NODE_TEX_COORD;
  2888. ShaderNodeType attr_node = NODE_ATTR;
  2889. ShaderNodeType geom_node = NODE_GEOMETRY;
  2890. if (bump == SHADER_BUMP_DX) {
  2891. texco_node = NODE_TEX_COORD_BUMP_DX;
  2892. attr_node = NODE_ATTR_BUMP_DX;
  2893. geom_node = NODE_GEOMETRY_BUMP_DX;
  2894. }
  2895. else if (bump == SHADER_BUMP_DY) {
  2896. texco_node = NODE_TEX_COORD_BUMP_DY;
  2897. attr_node = NODE_ATTR_BUMP_DY;
  2898. geom_node = NODE_GEOMETRY_BUMP_DY;
  2899. }
  2900. out = output("Generated");
  2901. if (!out->links.empty()) {
  2902. if (compiler.background) {
  2903. compiler.add_node(geom_node, NODE_GEOM_P, compiler.stack_assign(out));
  2904. }
  2905. else {
  2906. if (from_dupli) {
  2907. compiler.add_node(texco_node, NODE_TEXCO_DUPLI_GENERATED, compiler.stack_assign(out));
  2908. }
  2909. else if (compiler.output_type() == SHADER_TYPE_VOLUME) {
  2910. compiler.add_node(texco_node, NODE_TEXCO_VOLUME_GENERATED, compiler.stack_assign(out));
  2911. }
  2912. else {
  2913. int attr = compiler.attribute(ATTR_STD_GENERATED);
  2914. compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
  2915. }
  2916. }
  2917. }
  2918. out = output("Normal");
  2919. if (!out->links.empty()) {
  2920. compiler.add_node(texco_node, NODE_TEXCO_NORMAL, compiler.stack_assign(out));
  2921. }
  2922. out = output("UV");
  2923. if (!out->links.empty()) {
  2924. if (from_dupli) {
  2925. compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
  2926. }
  2927. else {
  2928. int attr = compiler.attribute(ATTR_STD_UV);
  2929. compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
  2930. }
  2931. }
  2932. out = output("Object");
  2933. if (!out->links.empty()) {
  2934. compiler.add_node(texco_node, NODE_TEXCO_OBJECT, compiler.stack_assign(out), use_transform);
  2935. if (use_transform) {
  2936. Transform ob_itfm = transform_inverse(ob_tfm);
  2937. compiler.add_node(ob_itfm.x);
  2938. compiler.add_node(ob_itfm.y);
  2939. compiler.add_node(ob_itfm.z);
  2940. }
  2941. }
  2942. out = output("Camera");
  2943. if (!out->links.empty()) {
  2944. compiler.add_node(texco_node, NODE_TEXCO_CAMERA, compiler.stack_assign(out));
  2945. }
  2946. out = output("Window");
  2947. if (!out->links.empty()) {
  2948. compiler.add_node(texco_node, NODE_TEXCO_WINDOW, compiler.stack_assign(out));
  2949. }
  2950. out = output("Reflection");
  2951. if (!out->links.empty()) {
  2952. if (compiler.background) {
  2953. compiler.add_node(geom_node, NODE_GEOM_I, compiler.stack_assign(out));
  2954. }
  2955. else {
  2956. compiler.add_node(texco_node, NODE_TEXCO_REFLECTION, compiler.stack_assign(out));
  2957. }
  2958. }
  2959. }
  2960. void TextureCoordinateNode::compile(OSLCompiler &compiler)
  2961. {
  2962. if (bump == SHADER_BUMP_DX)
  2963. compiler.parameter("bump_offset", "dx");
  2964. else if (bump == SHADER_BUMP_DY)
  2965. compiler.parameter("bump_offset", "dy");
  2966. else
  2967. compiler.parameter("bump_offset", "center");
  2968. if (compiler.background)
  2969. compiler.parameter("is_background", true);
  2970. if (compiler.output_type() == SHADER_TYPE_VOLUME)
  2971. compiler.parameter("is_volume", true);
  2972. compiler.parameter(this, "use_transform");
  2973. Transform ob_itfm = transform_inverse(ob_tfm);
  2974. compiler.parameter("object_itfm", ob_itfm);
  2975. compiler.parameter(this, "from_dupli");
  2976. compiler.add(this, "node_texture_coordinate");
  2977. }
  2978. /* UV Map */
  2979. NODE_DEFINE(UVMapNode)
  2980. {
  2981. NodeType *type = NodeType::add("uvmap", create, NodeType::SHADER);
  2982. SOCKET_STRING(attribute, "attribute", ustring());
  2983. SOCKET_IN_BOOLEAN(from_dupli, "from dupli", false);
  2984. SOCKET_OUT_POINT(UV, "UV");
  2985. return type;
  2986. }
  2987. UVMapNode::UVMapNode() : ShaderNode(node_type)
  2988. {
  2989. }
  2990. void UVMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  2991. {
  2992. if (shader->has_surface) {
  2993. if (!from_dupli) {
  2994. if (!output("UV")->links.empty()) {
  2995. if (attribute != "")
  2996. attributes->add(attribute);
  2997. else
  2998. attributes->add(ATTR_STD_UV);
  2999. }
  3000. }
  3001. }
  3002. ShaderNode::attributes(shader, attributes);
  3003. }
  3004. void UVMapNode::compile(SVMCompiler &compiler)
  3005. {
  3006. ShaderOutput *out = output("UV");
  3007. ShaderNodeType texco_node = NODE_TEX_COORD;
  3008. ShaderNodeType attr_node = NODE_ATTR;
  3009. int attr;
  3010. if (bump == SHADER_BUMP_DX) {
  3011. texco_node = NODE_TEX_COORD_BUMP_DX;
  3012. attr_node = NODE_ATTR_BUMP_DX;
  3013. }
  3014. else if (bump == SHADER_BUMP_DY) {
  3015. texco_node = NODE_TEX_COORD_BUMP_DY;
  3016. attr_node = NODE_ATTR_BUMP_DY;
  3017. }
  3018. if (!out->links.empty()) {
  3019. if (from_dupli) {
  3020. compiler.add_node(texco_node, NODE_TEXCO_DUPLI_UV, compiler.stack_assign(out));
  3021. }
  3022. else {
  3023. if (attribute != "")
  3024. attr = compiler.attribute(attribute);
  3025. else
  3026. attr = compiler.attribute(ATTR_STD_UV);
  3027. compiler.add_node(attr_node, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT3);
  3028. }
  3029. }
  3030. }
  3031. void UVMapNode::compile(OSLCompiler &compiler)
  3032. {
  3033. if (bump == SHADER_BUMP_DX)
  3034. compiler.parameter("bump_offset", "dx");
  3035. else if (bump == SHADER_BUMP_DY)
  3036. compiler.parameter("bump_offset", "dy");
  3037. else
  3038. compiler.parameter("bump_offset", "center");
  3039. compiler.parameter(this, "from_dupli");
  3040. compiler.parameter(this, "attribute");
  3041. compiler.add(this, "node_uv_map");
  3042. }
  3043. /* Light Path */
  3044. NODE_DEFINE(LightPathNode)
  3045. {
  3046. NodeType *type = NodeType::add("light_path", create, NodeType::SHADER);
  3047. SOCKET_OUT_FLOAT(is_camera_ray, "Is Camera Ray");
  3048. SOCKET_OUT_FLOAT(is_shadow_ray, "Is Shadow Ray");
  3049. SOCKET_OUT_FLOAT(is_diffuse_ray, "Is Diffuse Ray");
  3050. SOCKET_OUT_FLOAT(is_glossy_ray, "Is Glossy Ray");
  3051. SOCKET_OUT_FLOAT(is_singular_ray, "Is Singular Ray");
  3052. SOCKET_OUT_FLOAT(is_reflection_ray, "Is Reflection Ray");
  3053. SOCKET_OUT_FLOAT(is_transmission_ray, "Is Transmission Ray");
  3054. SOCKET_OUT_FLOAT(is_volume_scatter_ray, "Is Volume Scatter Ray");
  3055. SOCKET_OUT_FLOAT(ray_length, "Ray Length");
  3056. SOCKET_OUT_FLOAT(ray_depth, "Ray Depth");
  3057. SOCKET_OUT_FLOAT(diffuse_depth, "Diffuse Depth");
  3058. SOCKET_OUT_FLOAT(glossy_depth, "Glossy Depth");
  3059. SOCKET_OUT_FLOAT(transparent_depth, "Transparent Depth");
  3060. SOCKET_OUT_FLOAT(transmission_depth, "Transmission Depth");
  3061. return type;
  3062. }
  3063. LightPathNode::LightPathNode() : ShaderNode(node_type)
  3064. {
  3065. }
  3066. void LightPathNode::compile(SVMCompiler &compiler)
  3067. {
  3068. ShaderOutput *out;
  3069. out = output("Is Camera Ray");
  3070. if (!out->links.empty()) {
  3071. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_camera, compiler.stack_assign(out));
  3072. }
  3073. out = output("Is Shadow Ray");
  3074. if (!out->links.empty()) {
  3075. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_shadow, compiler.stack_assign(out));
  3076. }
  3077. out = output("Is Diffuse Ray");
  3078. if (!out->links.empty()) {
  3079. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_diffuse, compiler.stack_assign(out));
  3080. }
  3081. out = output("Is Glossy Ray");
  3082. if (!out->links.empty()) {
  3083. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_glossy, compiler.stack_assign(out));
  3084. }
  3085. out = output("Is Singular Ray");
  3086. if (!out->links.empty()) {
  3087. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_singular, compiler.stack_assign(out));
  3088. }
  3089. out = output("Is Reflection Ray");
  3090. if (!out->links.empty()) {
  3091. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_reflection, compiler.stack_assign(out));
  3092. }
  3093. out = output("Is Transmission Ray");
  3094. if (!out->links.empty()) {
  3095. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_transmission, compiler.stack_assign(out));
  3096. }
  3097. out = output("Is Volume Scatter Ray");
  3098. if (!out->links.empty()) {
  3099. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_volume_scatter, compiler.stack_assign(out));
  3100. }
  3101. out = output("Ray Length");
  3102. if (!out->links.empty()) {
  3103. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_length, compiler.stack_assign(out));
  3104. }
  3105. out = output("Ray Depth");
  3106. if (!out->links.empty()) {
  3107. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_depth, compiler.stack_assign(out));
  3108. }
  3109. out = output("Diffuse Depth");
  3110. if (!out->links.empty()) {
  3111. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_diffuse, compiler.stack_assign(out));
  3112. }
  3113. out = output("Glossy Depth");
  3114. if (!out->links.empty()) {
  3115. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_glossy, compiler.stack_assign(out));
  3116. }
  3117. out = output("Transparent Depth");
  3118. if (!out->links.empty()) {
  3119. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transparent, compiler.stack_assign(out));
  3120. }
  3121. out = output("Transmission Depth");
  3122. if (!out->links.empty()) {
  3123. compiler.add_node(NODE_LIGHT_PATH, NODE_LP_ray_transmission, compiler.stack_assign(out));
  3124. }
  3125. }
  3126. void LightPathNode::compile(OSLCompiler &compiler)
  3127. {
  3128. compiler.add(this, "node_light_path");
  3129. }
  3130. /* Light Falloff */
  3131. NODE_DEFINE(LightFalloffNode)
  3132. {
  3133. NodeType *type = NodeType::add("light_falloff", create, NodeType::SHADER);
  3134. SOCKET_IN_FLOAT(strength, "Strength", 100.0f);
  3135. SOCKET_IN_FLOAT(smooth, "Smooth", 0.0f);
  3136. SOCKET_OUT_FLOAT(quadratic, "Quadratic");
  3137. SOCKET_OUT_FLOAT(linear, "Linear");
  3138. SOCKET_OUT_FLOAT(constant, "Constant");
  3139. return type;
  3140. }
  3141. LightFalloffNode::LightFalloffNode() : ShaderNode(node_type)
  3142. {
  3143. }
  3144. void LightFalloffNode::compile(SVMCompiler &compiler)
  3145. {
  3146. ShaderInput *strength_in = input("Strength");
  3147. ShaderInput *smooth_in = input("Smooth");
  3148. ShaderOutput *out = output("Quadratic");
  3149. if (!out->links.empty()) {
  3150. compiler.add_node(NODE_LIGHT_FALLOFF,
  3151. NODE_LIGHT_FALLOFF_QUADRATIC,
  3152. compiler.encode_uchar4(compiler.stack_assign(strength_in),
  3153. compiler.stack_assign(smooth_in),
  3154. compiler.stack_assign(out)));
  3155. }
  3156. out = output("Linear");
  3157. if (!out->links.empty()) {
  3158. compiler.add_node(NODE_LIGHT_FALLOFF,
  3159. NODE_LIGHT_FALLOFF_LINEAR,
  3160. compiler.encode_uchar4(compiler.stack_assign(strength_in),
  3161. compiler.stack_assign(smooth_in),
  3162. compiler.stack_assign(out)));
  3163. }
  3164. out = output("Constant");
  3165. if (!out->links.empty()) {
  3166. compiler.add_node(NODE_LIGHT_FALLOFF,
  3167. NODE_LIGHT_FALLOFF_CONSTANT,
  3168. compiler.encode_uchar4(compiler.stack_assign(strength_in),
  3169. compiler.stack_assign(smooth_in),
  3170. compiler.stack_assign(out)));
  3171. }
  3172. }
  3173. void LightFalloffNode::compile(OSLCompiler &compiler)
  3174. {
  3175. compiler.add(this, "node_light_falloff");
  3176. }
  3177. /* Object Info */
  3178. NODE_DEFINE(ObjectInfoNode)
  3179. {
  3180. NodeType *type = NodeType::add("object_info", create, NodeType::SHADER);
  3181. SOCKET_OUT_VECTOR(location, "Location");
  3182. SOCKET_OUT_FLOAT(object_index, "Object Index");
  3183. SOCKET_OUT_FLOAT(material_index, "Material Index");
  3184. SOCKET_OUT_FLOAT(random, "Random");
  3185. return type;
  3186. }
  3187. ObjectInfoNode::ObjectInfoNode() : ShaderNode(node_type)
  3188. {
  3189. }
  3190. void ObjectInfoNode::compile(SVMCompiler &compiler)
  3191. {
  3192. ShaderOutput *out = output("Location");
  3193. if (!out->links.empty()) {
  3194. compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_LOCATION, compiler.stack_assign(out));
  3195. }
  3196. out = output("Object Index");
  3197. if (!out->links.empty()) {
  3198. compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_INDEX, compiler.stack_assign(out));
  3199. }
  3200. out = output("Material Index");
  3201. if (!out->links.empty()) {
  3202. compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_MAT_INDEX, compiler.stack_assign(out));
  3203. }
  3204. out = output("Random");
  3205. if (!out->links.empty()) {
  3206. compiler.add_node(NODE_OBJECT_INFO, NODE_INFO_OB_RANDOM, compiler.stack_assign(out));
  3207. }
  3208. }
  3209. void ObjectInfoNode::compile(OSLCompiler &compiler)
  3210. {
  3211. compiler.add(this, "node_object_info");
  3212. }
  3213. /* Particle Info */
  3214. NODE_DEFINE(ParticleInfoNode)
  3215. {
  3216. NodeType *type = NodeType::add("particle_info", create, NodeType::SHADER);
  3217. SOCKET_OUT_FLOAT(index, "Index");
  3218. SOCKET_OUT_FLOAT(random, "Random");
  3219. SOCKET_OUT_FLOAT(age, "Age");
  3220. SOCKET_OUT_FLOAT(lifetime, "Lifetime");
  3221. SOCKET_OUT_POINT(location, "Location");
  3222. #if 0 /* not yet supported */
  3223. SOCKET_OUT_QUATERNION(rotation, "Rotation");
  3224. #endif
  3225. SOCKET_OUT_FLOAT(size, "Size");
  3226. SOCKET_OUT_VECTOR(velocity, "Velocity");
  3227. SOCKET_OUT_VECTOR(angular_velocity, "Angular Velocity");
  3228. return type;
  3229. }
  3230. ParticleInfoNode::ParticleInfoNode() : ShaderNode(node_type)
  3231. {
  3232. }
  3233. void ParticleInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  3234. {
  3235. if (!output("Index")->links.empty())
  3236. attributes->add(ATTR_STD_PARTICLE);
  3237. if (!output("Random")->links.empty())
  3238. attributes->add(ATTR_STD_PARTICLE);
  3239. if (!output("Age")->links.empty())
  3240. attributes->add(ATTR_STD_PARTICLE);
  3241. if (!output("Lifetime")->links.empty())
  3242. attributes->add(ATTR_STD_PARTICLE);
  3243. if (!output("Location")->links.empty())
  3244. attributes->add(ATTR_STD_PARTICLE);
  3245. #if 0 /* not yet supported */
  3246. if (!output("Rotation")->links.empty())
  3247. attributes->add(ATTR_STD_PARTICLE);
  3248. #endif
  3249. if (!output("Size")->links.empty())
  3250. attributes->add(ATTR_STD_PARTICLE);
  3251. if (!output("Velocity")->links.empty())
  3252. attributes->add(ATTR_STD_PARTICLE);
  3253. if (!output("Angular Velocity")->links.empty())
  3254. attributes->add(ATTR_STD_PARTICLE);
  3255. ShaderNode::attributes(shader, attributes);
  3256. }
  3257. void ParticleInfoNode::compile(SVMCompiler &compiler)
  3258. {
  3259. ShaderOutput *out;
  3260. out = output("Index");
  3261. if (!out->links.empty()) {
  3262. compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_INDEX, compiler.stack_assign(out));
  3263. }
  3264. out = output("Random");
  3265. if (!out->links.empty()) {
  3266. compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_RANDOM, compiler.stack_assign(out));
  3267. }
  3268. out = output("Age");
  3269. if (!out->links.empty()) {
  3270. compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_AGE, compiler.stack_assign(out));
  3271. }
  3272. out = output("Lifetime");
  3273. if (!out->links.empty()) {
  3274. compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LIFETIME, compiler.stack_assign(out));
  3275. }
  3276. out = output("Location");
  3277. if (!out->links.empty()) {
  3278. compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_LOCATION, compiler.stack_assign(out));
  3279. }
  3280. /* quaternion data is not yet supported by Cycles */
  3281. #if 0
  3282. out = output("Rotation");
  3283. if (!out->links.empty()) {
  3284. compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_ROTATION, compiler.stack_assign(out));
  3285. }
  3286. #endif
  3287. out = output("Size");
  3288. if (!out->links.empty()) {
  3289. compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_SIZE, compiler.stack_assign(out));
  3290. }
  3291. out = output("Velocity");
  3292. if (!out->links.empty()) {
  3293. compiler.add_node(NODE_PARTICLE_INFO, NODE_INFO_PAR_VELOCITY, compiler.stack_assign(out));
  3294. }
  3295. out = output("Angular Velocity");
  3296. if (!out->links.empty()) {
  3297. compiler.add_node(
  3298. NODE_PARTICLE_INFO, NODE_INFO_PAR_ANGULAR_VELOCITY, compiler.stack_assign(out));
  3299. }
  3300. }
  3301. void ParticleInfoNode::compile(OSLCompiler &compiler)
  3302. {
  3303. compiler.add(this, "node_particle_info");
  3304. }
  3305. /* Hair Info */
  3306. NODE_DEFINE(HairInfoNode)
  3307. {
  3308. NodeType *type = NodeType::add("hair_info", create, NodeType::SHADER);
  3309. SOCKET_OUT_FLOAT(is_strand, "Is Strand");
  3310. SOCKET_OUT_FLOAT(intercept, "Intercept");
  3311. SOCKET_OUT_FLOAT(thickness, "Thickness");
  3312. SOCKET_OUT_NORMAL(tangent_normal, "Tangent Normal");
  3313. #if 0 /*output for minimum hair width transparency - deactivated */
  3314. SOCKET_OUT_FLOAT(fade, "Fade");
  3315. #endif
  3316. SOCKET_OUT_FLOAT(index, "Random");
  3317. return type;
  3318. }
  3319. HairInfoNode::HairInfoNode() : ShaderNode(node_type)
  3320. {
  3321. }
  3322. void HairInfoNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  3323. {
  3324. if (shader->has_surface) {
  3325. ShaderOutput *intercept_out = output("Intercept");
  3326. if (!intercept_out->links.empty())
  3327. attributes->add(ATTR_STD_CURVE_INTERCEPT);
  3328. if (!output("Random")->links.empty())
  3329. attributes->add(ATTR_STD_CURVE_RANDOM);
  3330. }
  3331. ShaderNode::attributes(shader, attributes);
  3332. }
  3333. void HairInfoNode::compile(SVMCompiler &compiler)
  3334. {
  3335. ShaderOutput *out;
  3336. out = output("Is Strand");
  3337. if (!out->links.empty()) {
  3338. compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_IS_STRAND, compiler.stack_assign(out));
  3339. }
  3340. out = output("Intercept");
  3341. if (!out->links.empty()) {
  3342. int attr = compiler.attribute(ATTR_STD_CURVE_INTERCEPT);
  3343. compiler.add_node(NODE_ATTR, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT);
  3344. }
  3345. out = output("Thickness");
  3346. if (!out->links.empty()) {
  3347. compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_THICKNESS, compiler.stack_assign(out));
  3348. }
  3349. out = output("Tangent Normal");
  3350. if (!out->links.empty()) {
  3351. compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_TANGENT_NORMAL, compiler.stack_assign(out));
  3352. }
  3353. /*out = output("Fade");
  3354. if(!out->links.empty()) {
  3355. compiler.add_node(NODE_HAIR_INFO, NODE_INFO_CURVE_FADE, compiler.stack_assign(out));
  3356. }*/
  3357. out = output("Random");
  3358. if (!out->links.empty()) {
  3359. int attr = compiler.attribute(ATTR_STD_CURVE_RANDOM);
  3360. compiler.add_node(NODE_ATTR, attr, compiler.stack_assign(out), NODE_ATTR_FLOAT);
  3361. }
  3362. }
  3363. void HairInfoNode::compile(OSLCompiler &compiler)
  3364. {
  3365. compiler.add(this, "node_hair_info");
  3366. }
  3367. /* Value */
  3368. NODE_DEFINE(ValueNode)
  3369. {
  3370. NodeType *type = NodeType::add("value", create, NodeType::SHADER);
  3371. SOCKET_FLOAT(value, "Value", 0.0f);
  3372. SOCKET_OUT_FLOAT(value, "Value");
  3373. return type;
  3374. }
  3375. ValueNode::ValueNode() : ShaderNode(node_type)
  3376. {
  3377. }
  3378. void ValueNode::constant_fold(const ConstantFolder &folder)
  3379. {
  3380. folder.make_constant(value);
  3381. }
  3382. void ValueNode::compile(SVMCompiler &compiler)
  3383. {
  3384. ShaderOutput *val_out = output("Value");
  3385. compiler.add_node(NODE_VALUE_F, __float_as_int(value), compiler.stack_assign(val_out));
  3386. }
  3387. void ValueNode::compile(OSLCompiler &compiler)
  3388. {
  3389. compiler.parameter("value_value", value);
  3390. compiler.add(this, "node_value");
  3391. }
  3392. /* Color */
  3393. NODE_DEFINE(ColorNode)
  3394. {
  3395. NodeType *type = NodeType::add("color", create, NodeType::SHADER);
  3396. SOCKET_COLOR(value, "Value", make_float3(0.0f, 0.0f, 0.0f));
  3397. SOCKET_OUT_COLOR(color, "Color");
  3398. return type;
  3399. }
  3400. ColorNode::ColorNode() : ShaderNode(node_type)
  3401. {
  3402. }
  3403. void ColorNode::constant_fold(const ConstantFolder &folder)
  3404. {
  3405. folder.make_constant(value);
  3406. }
  3407. void ColorNode::compile(SVMCompiler &compiler)
  3408. {
  3409. ShaderOutput *color_out = output("Color");
  3410. if (!color_out->links.empty()) {
  3411. compiler.add_node(NODE_VALUE_V, compiler.stack_assign(color_out));
  3412. compiler.add_node(NODE_VALUE_V, value);
  3413. }
  3414. }
  3415. void ColorNode::compile(OSLCompiler &compiler)
  3416. {
  3417. compiler.parameter_color("color_value", value);
  3418. compiler.add(this, "node_value");
  3419. }
  3420. /* Add Closure */
  3421. NODE_DEFINE(AddClosureNode)
  3422. {
  3423. NodeType *type = NodeType::add("add_closure", create, NodeType::SHADER);
  3424. SOCKET_IN_CLOSURE(closure1, "Closure1");
  3425. SOCKET_IN_CLOSURE(closure2, "Closure2");
  3426. SOCKET_OUT_CLOSURE(closure, "Closure");
  3427. return type;
  3428. }
  3429. AddClosureNode::AddClosureNode() : ShaderNode(node_type)
  3430. {
  3431. special_type = SHADER_SPECIAL_TYPE_COMBINE_CLOSURE;
  3432. }
  3433. void AddClosureNode::compile(SVMCompiler & /*compiler*/)
  3434. {
  3435. /* handled in the SVM compiler */
  3436. }
  3437. void AddClosureNode::compile(OSLCompiler &compiler)
  3438. {
  3439. compiler.add(this, "node_add_closure");
  3440. }
  3441. void AddClosureNode::constant_fold(const ConstantFolder &folder)
  3442. {
  3443. ShaderInput *closure1_in = input("Closure1");
  3444. ShaderInput *closure2_in = input("Closure2");
  3445. /* remove useless add closures nodes */
  3446. if (!closure1_in->link) {
  3447. folder.bypass_or_discard(closure2_in);
  3448. }
  3449. else if (!closure2_in->link) {
  3450. folder.bypass_or_discard(closure1_in);
  3451. }
  3452. }
  3453. /* Mix Closure */
  3454. NODE_DEFINE(MixClosureNode)
  3455. {
  3456. NodeType *type = NodeType::add("mix_closure", create, NodeType::SHADER);
  3457. SOCKET_IN_FLOAT(fac, "Fac", 0.5f);
  3458. SOCKET_IN_CLOSURE(closure1, "Closure1");
  3459. SOCKET_IN_CLOSURE(closure2, "Closure2");
  3460. SOCKET_OUT_CLOSURE(closure, "Closure");
  3461. return type;
  3462. }
  3463. MixClosureNode::MixClosureNode() : ShaderNode(node_type)
  3464. {
  3465. special_type = SHADER_SPECIAL_TYPE_COMBINE_CLOSURE;
  3466. }
  3467. void MixClosureNode::compile(SVMCompiler & /*compiler*/)
  3468. {
  3469. /* handled in the SVM compiler */
  3470. }
  3471. void MixClosureNode::compile(OSLCompiler &compiler)
  3472. {
  3473. compiler.add(this, "node_mix_closure");
  3474. }
  3475. void MixClosureNode::constant_fold(const ConstantFolder &folder)
  3476. {
  3477. ShaderInput *fac_in = input("Fac");
  3478. ShaderInput *closure1_in = input("Closure1");
  3479. ShaderInput *closure2_in = input("Closure2");
  3480. /* remove useless mix closures nodes */
  3481. if (closure1_in->link == closure2_in->link) {
  3482. folder.bypass_or_discard(closure1_in);
  3483. }
  3484. /* remove unused mix closure input when factor is 0.0 or 1.0
  3485. * check for closure links and make sure factor link is disconnected */
  3486. else if (!fac_in->link) {
  3487. /* factor 0.0 */
  3488. if (fac <= 0.0f) {
  3489. folder.bypass_or_discard(closure1_in);
  3490. }
  3491. /* factor 1.0 */
  3492. else if (fac >= 1.0f) {
  3493. folder.bypass_or_discard(closure2_in);
  3494. }
  3495. }
  3496. }
  3497. /* Mix Closure */
  3498. NODE_DEFINE(MixClosureWeightNode)
  3499. {
  3500. NodeType *type = NodeType::add("mix_closure_weight", create, NodeType::SHADER);
  3501. SOCKET_IN_FLOAT(weight, "Weight", 1.0f);
  3502. SOCKET_IN_FLOAT(fac, "Fac", 1.0f);
  3503. SOCKET_OUT_FLOAT(weight1, "Weight1");
  3504. SOCKET_OUT_FLOAT(weight2, "Weight2");
  3505. return type;
  3506. }
  3507. MixClosureWeightNode::MixClosureWeightNode() : ShaderNode(node_type)
  3508. {
  3509. }
  3510. void MixClosureWeightNode::compile(SVMCompiler &compiler)
  3511. {
  3512. ShaderInput *weight_in = input("Weight");
  3513. ShaderInput *fac_in = input("Fac");
  3514. ShaderOutput *weight1_out = output("Weight1");
  3515. ShaderOutput *weight2_out = output("Weight2");
  3516. compiler.add_node(NODE_MIX_CLOSURE,
  3517. compiler.encode_uchar4(compiler.stack_assign(fac_in),
  3518. compiler.stack_assign(weight_in),
  3519. compiler.stack_assign(weight1_out),
  3520. compiler.stack_assign(weight2_out)));
  3521. }
  3522. void MixClosureWeightNode::compile(OSLCompiler & /*compiler*/)
  3523. {
  3524. assert(0);
  3525. }
  3526. /* Invert */
  3527. NODE_DEFINE(InvertNode)
  3528. {
  3529. NodeType *type = NodeType::add("invert", create, NodeType::SHADER);
  3530. SOCKET_IN_FLOAT(fac, "Fac", 1.0f);
  3531. SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
  3532. SOCKET_OUT_COLOR(color, "Color");
  3533. return type;
  3534. }
  3535. InvertNode::InvertNode() : ShaderNode(node_type)
  3536. {
  3537. }
  3538. void InvertNode::constant_fold(const ConstantFolder &folder)
  3539. {
  3540. ShaderInput *fac_in = input("Fac");
  3541. ShaderInput *color_in = input("Color");
  3542. if (!fac_in->link) {
  3543. /* evaluate fully constant node */
  3544. if (!color_in->link) {
  3545. folder.make_constant(interp(color, make_float3(1.0f, 1.0f, 1.0f) - color, fac));
  3546. }
  3547. /* remove no-op node */
  3548. else if (fac == 0.0f) {
  3549. folder.bypass(color_in->link);
  3550. }
  3551. }
  3552. }
  3553. void InvertNode::compile(SVMCompiler &compiler)
  3554. {
  3555. ShaderInput *fac_in = input("Fac");
  3556. ShaderInput *color_in = input("Color");
  3557. ShaderOutput *color_out = output("Color");
  3558. compiler.add_node(NODE_INVERT,
  3559. compiler.stack_assign(fac_in),
  3560. compiler.stack_assign(color_in),
  3561. compiler.stack_assign(color_out));
  3562. }
  3563. void InvertNode::compile(OSLCompiler &compiler)
  3564. {
  3565. compiler.add(this, "node_invert");
  3566. }
  3567. /* Mix */
  3568. NODE_DEFINE(MixNode)
  3569. {
  3570. NodeType *type = NodeType::add("mix", create, NodeType::SHADER);
  3571. static NodeEnum type_enum;
  3572. type_enum.insert("mix", NODE_MIX_BLEND);
  3573. type_enum.insert("add", NODE_MIX_ADD);
  3574. type_enum.insert("multiply", NODE_MIX_MUL);
  3575. type_enum.insert("screen", NODE_MIX_SCREEN);
  3576. type_enum.insert("overlay", NODE_MIX_OVERLAY);
  3577. type_enum.insert("subtract", NODE_MIX_SUB);
  3578. type_enum.insert("divide", NODE_MIX_DIV);
  3579. type_enum.insert("difference", NODE_MIX_DIFF);
  3580. type_enum.insert("darken", NODE_MIX_DARK);
  3581. type_enum.insert("lighten", NODE_MIX_LIGHT);
  3582. type_enum.insert("dodge", NODE_MIX_DODGE);
  3583. type_enum.insert("burn", NODE_MIX_BURN);
  3584. type_enum.insert("hue", NODE_MIX_HUE);
  3585. type_enum.insert("saturation", NODE_MIX_SAT);
  3586. type_enum.insert("value", NODE_MIX_VAL);
  3587. type_enum.insert("color", NODE_MIX_COLOR);
  3588. type_enum.insert("soft_light", NODE_MIX_SOFT);
  3589. type_enum.insert("linear_light", NODE_MIX_LINEAR);
  3590. SOCKET_ENUM(type, "Type", type_enum, NODE_MIX_BLEND);
  3591. SOCKET_BOOLEAN(use_clamp, "Use Clamp", false);
  3592. SOCKET_IN_FLOAT(fac, "Fac", 0.5f);
  3593. SOCKET_IN_COLOR(color1, "Color1", make_float3(0.0f, 0.0f, 0.0f));
  3594. SOCKET_IN_COLOR(color2, "Color2", make_float3(0.0f, 0.0f, 0.0f));
  3595. SOCKET_OUT_COLOR(color, "Color");
  3596. return type;
  3597. }
  3598. MixNode::MixNode() : ShaderNode(node_type)
  3599. {
  3600. }
  3601. void MixNode::compile(SVMCompiler &compiler)
  3602. {
  3603. ShaderInput *fac_in = input("Fac");
  3604. ShaderInput *color1_in = input("Color1");
  3605. ShaderInput *color2_in = input("Color2");
  3606. ShaderOutput *color_out = output("Color");
  3607. compiler.add_node(NODE_MIX,
  3608. compiler.stack_assign(fac_in),
  3609. compiler.stack_assign(color1_in),
  3610. compiler.stack_assign(color2_in));
  3611. compiler.add_node(NODE_MIX, type, compiler.stack_assign(color_out));
  3612. if (use_clamp) {
  3613. compiler.add_node(NODE_MIX, 0, compiler.stack_assign(color_out));
  3614. compiler.add_node(NODE_MIX, NODE_MIX_CLAMP, compiler.stack_assign(color_out));
  3615. }
  3616. }
  3617. void MixNode::compile(OSLCompiler &compiler)
  3618. {
  3619. compiler.parameter(this, "type");
  3620. compiler.parameter(this, "use_clamp");
  3621. compiler.add(this, "node_mix");
  3622. }
  3623. void MixNode::constant_fold(const ConstantFolder &folder)
  3624. {
  3625. if (folder.all_inputs_constant()) {
  3626. folder.make_constant_clamp(svm_mix(type, fac, color1, color2), use_clamp);
  3627. }
  3628. else {
  3629. folder.fold_mix(type, use_clamp);
  3630. }
  3631. }
  3632. /* Combine RGB */
  3633. NODE_DEFINE(CombineRGBNode)
  3634. {
  3635. NodeType *type = NodeType::add("combine_rgb", create, NodeType::SHADER);
  3636. SOCKET_IN_FLOAT(r, "R", 0.0f);
  3637. SOCKET_IN_FLOAT(g, "G", 0.0f);
  3638. SOCKET_IN_FLOAT(b, "B", 0.0f);
  3639. SOCKET_OUT_COLOR(image, "Image");
  3640. return type;
  3641. }
  3642. CombineRGBNode::CombineRGBNode() : ShaderNode(node_type)
  3643. {
  3644. }
  3645. void CombineRGBNode::constant_fold(const ConstantFolder &folder)
  3646. {
  3647. if (folder.all_inputs_constant()) {
  3648. folder.make_constant(make_float3(r, g, b));
  3649. }
  3650. }
  3651. void CombineRGBNode::compile(SVMCompiler &compiler)
  3652. {
  3653. ShaderInput *red_in = input("R");
  3654. ShaderInput *green_in = input("G");
  3655. ShaderInput *blue_in = input("B");
  3656. ShaderOutput *color_out = output("Image");
  3657. compiler.add_node(
  3658. NODE_COMBINE_VECTOR, compiler.stack_assign(red_in), 0, compiler.stack_assign(color_out));
  3659. compiler.add_node(
  3660. NODE_COMBINE_VECTOR, compiler.stack_assign(green_in), 1, compiler.stack_assign(color_out));
  3661. compiler.add_node(
  3662. NODE_COMBINE_VECTOR, compiler.stack_assign(blue_in), 2, compiler.stack_assign(color_out));
  3663. }
  3664. void CombineRGBNode::compile(OSLCompiler &compiler)
  3665. {
  3666. compiler.add(this, "node_combine_rgb");
  3667. }
  3668. /* Combine XYZ */
  3669. NODE_DEFINE(CombineXYZNode)
  3670. {
  3671. NodeType *type = NodeType::add("combine_xyz", create, NodeType::SHADER);
  3672. SOCKET_IN_FLOAT(x, "X", 0.0f);
  3673. SOCKET_IN_FLOAT(y, "Y", 0.0f);
  3674. SOCKET_IN_FLOAT(z, "Z", 0.0f);
  3675. SOCKET_OUT_VECTOR(vector, "Vector");
  3676. return type;
  3677. }
  3678. CombineXYZNode::CombineXYZNode() : ShaderNode(node_type)
  3679. {
  3680. }
  3681. void CombineXYZNode::constant_fold(const ConstantFolder &folder)
  3682. {
  3683. if (folder.all_inputs_constant()) {
  3684. folder.make_constant(make_float3(x, y, z));
  3685. }
  3686. }
  3687. void CombineXYZNode::compile(SVMCompiler &compiler)
  3688. {
  3689. ShaderInput *x_in = input("X");
  3690. ShaderInput *y_in = input("Y");
  3691. ShaderInput *z_in = input("Z");
  3692. ShaderOutput *vector_out = output("Vector");
  3693. compiler.add_node(
  3694. NODE_COMBINE_VECTOR, compiler.stack_assign(x_in), 0, compiler.stack_assign(vector_out));
  3695. compiler.add_node(
  3696. NODE_COMBINE_VECTOR, compiler.stack_assign(y_in), 1, compiler.stack_assign(vector_out));
  3697. compiler.add_node(
  3698. NODE_COMBINE_VECTOR, compiler.stack_assign(z_in), 2, compiler.stack_assign(vector_out));
  3699. }
  3700. void CombineXYZNode::compile(OSLCompiler &compiler)
  3701. {
  3702. compiler.add(this, "node_combine_xyz");
  3703. }
  3704. /* Combine HSV */
  3705. NODE_DEFINE(CombineHSVNode)
  3706. {
  3707. NodeType *type = NodeType::add("combine_hsv", create, NodeType::SHADER);
  3708. SOCKET_IN_FLOAT(h, "H", 0.0f);
  3709. SOCKET_IN_FLOAT(s, "S", 0.0f);
  3710. SOCKET_IN_FLOAT(v, "V", 0.0f);
  3711. SOCKET_OUT_COLOR(color, "Color");
  3712. return type;
  3713. }
  3714. CombineHSVNode::CombineHSVNode() : ShaderNode(node_type)
  3715. {
  3716. }
  3717. void CombineHSVNode::constant_fold(const ConstantFolder &folder)
  3718. {
  3719. if (folder.all_inputs_constant()) {
  3720. folder.make_constant(hsv_to_rgb(make_float3(h, s, v)));
  3721. }
  3722. }
  3723. void CombineHSVNode::compile(SVMCompiler &compiler)
  3724. {
  3725. ShaderInput *hue_in = input("H");
  3726. ShaderInput *saturation_in = input("S");
  3727. ShaderInput *value_in = input("V");
  3728. ShaderOutput *color_out = output("Color");
  3729. compiler.add_node(NODE_COMBINE_HSV,
  3730. compiler.stack_assign(hue_in),
  3731. compiler.stack_assign(saturation_in),
  3732. compiler.stack_assign(value_in));
  3733. compiler.add_node(NODE_COMBINE_HSV, compiler.stack_assign(color_out));
  3734. }
  3735. void CombineHSVNode::compile(OSLCompiler &compiler)
  3736. {
  3737. compiler.add(this, "node_combine_hsv");
  3738. }
  3739. /* Gamma */
  3740. NODE_DEFINE(GammaNode)
  3741. {
  3742. NodeType *type = NodeType::add("gamma", create, NodeType::SHADER);
  3743. SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
  3744. SOCKET_IN_FLOAT(gamma, "Gamma", 1.0f);
  3745. SOCKET_OUT_COLOR(color, "Color");
  3746. return type;
  3747. }
  3748. GammaNode::GammaNode() : ShaderNode(node_type)
  3749. {
  3750. }
  3751. void GammaNode::constant_fold(const ConstantFolder &folder)
  3752. {
  3753. if (folder.all_inputs_constant()) {
  3754. folder.make_constant(svm_math_gamma_color(color, gamma));
  3755. }
  3756. else {
  3757. ShaderInput *color_in = input("Color");
  3758. ShaderInput *gamma_in = input("Gamma");
  3759. /* 1 ^ X == X ^ 0 == 1 */
  3760. if (folder.is_one(color_in) || folder.is_zero(gamma_in)) {
  3761. folder.make_one();
  3762. }
  3763. /* X ^ 1 == X */
  3764. else if (folder.is_one(gamma_in)) {
  3765. folder.try_bypass_or_make_constant(color_in, false);
  3766. }
  3767. }
  3768. }
  3769. void GammaNode::compile(SVMCompiler &compiler)
  3770. {
  3771. ShaderInput *color_in = input("Color");
  3772. ShaderInput *gamma_in = input("Gamma");
  3773. ShaderOutput *color_out = output("Color");
  3774. compiler.add_node(NODE_GAMMA,
  3775. compiler.stack_assign(gamma_in),
  3776. compiler.stack_assign(color_in),
  3777. compiler.stack_assign(color_out));
  3778. }
  3779. void GammaNode::compile(OSLCompiler &compiler)
  3780. {
  3781. compiler.add(this, "node_gamma");
  3782. }
  3783. /* Bright Contrast */
  3784. NODE_DEFINE(BrightContrastNode)
  3785. {
  3786. NodeType *type = NodeType::add("brightness_contrast", create, NodeType::SHADER);
  3787. SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
  3788. SOCKET_IN_FLOAT(bright, "Bright", 0.0f);
  3789. SOCKET_IN_FLOAT(contrast, "Contrast", 0.0f);
  3790. SOCKET_OUT_COLOR(color, "Color");
  3791. return type;
  3792. }
  3793. BrightContrastNode::BrightContrastNode() : ShaderNode(node_type)
  3794. {
  3795. }
  3796. void BrightContrastNode::constant_fold(const ConstantFolder &folder)
  3797. {
  3798. if (folder.all_inputs_constant()) {
  3799. folder.make_constant(svm_brightness_contrast(color, bright, contrast));
  3800. }
  3801. }
  3802. void BrightContrastNode::compile(SVMCompiler &compiler)
  3803. {
  3804. ShaderInput *color_in = input("Color");
  3805. ShaderInput *bright_in = input("Bright");
  3806. ShaderInput *contrast_in = input("Contrast");
  3807. ShaderOutput *color_out = output("Color");
  3808. compiler.add_node(NODE_BRIGHTCONTRAST,
  3809. compiler.stack_assign(color_in),
  3810. compiler.stack_assign(color_out),
  3811. compiler.encode_uchar4(compiler.stack_assign(bright_in),
  3812. compiler.stack_assign(contrast_in)));
  3813. }
  3814. void BrightContrastNode::compile(OSLCompiler &compiler)
  3815. {
  3816. compiler.add(this, "node_brightness");
  3817. }
  3818. /* Separate RGB */
  3819. NODE_DEFINE(SeparateRGBNode)
  3820. {
  3821. NodeType *type = NodeType::add("separate_rgb", create, NodeType::SHADER);
  3822. SOCKET_IN_COLOR(color, "Image", make_float3(0.0f, 0.0f, 0.0f));
  3823. SOCKET_OUT_FLOAT(r, "R");
  3824. SOCKET_OUT_FLOAT(g, "G");
  3825. SOCKET_OUT_FLOAT(b, "B");
  3826. return type;
  3827. }
  3828. SeparateRGBNode::SeparateRGBNode() : ShaderNode(node_type)
  3829. {
  3830. }
  3831. void SeparateRGBNode::constant_fold(const ConstantFolder &folder)
  3832. {
  3833. if (folder.all_inputs_constant()) {
  3834. for (int channel = 0; channel < 3; channel++) {
  3835. if (outputs[channel] == folder.output) {
  3836. folder.make_constant(color[channel]);
  3837. return;
  3838. }
  3839. }
  3840. }
  3841. }
  3842. void SeparateRGBNode::compile(SVMCompiler &compiler)
  3843. {
  3844. ShaderInput *color_in = input("Image");
  3845. ShaderOutput *red_out = output("R");
  3846. ShaderOutput *green_out = output("G");
  3847. ShaderOutput *blue_out = output("B");
  3848. compiler.add_node(
  3849. NODE_SEPARATE_VECTOR, compiler.stack_assign(color_in), 0, compiler.stack_assign(red_out));
  3850. compiler.add_node(
  3851. NODE_SEPARATE_VECTOR, compiler.stack_assign(color_in), 1, compiler.stack_assign(green_out));
  3852. compiler.add_node(
  3853. NODE_SEPARATE_VECTOR, compiler.stack_assign(color_in), 2, compiler.stack_assign(blue_out));
  3854. }
  3855. void SeparateRGBNode::compile(OSLCompiler &compiler)
  3856. {
  3857. compiler.add(this, "node_separate_rgb");
  3858. }
  3859. /* Separate XYZ */
  3860. NODE_DEFINE(SeparateXYZNode)
  3861. {
  3862. NodeType *type = NodeType::add("separate_xyz", create, NodeType::SHADER);
  3863. SOCKET_IN_COLOR(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
  3864. SOCKET_OUT_FLOAT(x, "X");
  3865. SOCKET_OUT_FLOAT(y, "Y");
  3866. SOCKET_OUT_FLOAT(z, "Z");
  3867. return type;
  3868. }
  3869. SeparateXYZNode::SeparateXYZNode() : ShaderNode(node_type)
  3870. {
  3871. }
  3872. void SeparateXYZNode::constant_fold(const ConstantFolder &folder)
  3873. {
  3874. if (folder.all_inputs_constant()) {
  3875. for (int channel = 0; channel < 3; channel++) {
  3876. if (outputs[channel] == folder.output) {
  3877. folder.make_constant(vector[channel]);
  3878. return;
  3879. }
  3880. }
  3881. }
  3882. }
  3883. void SeparateXYZNode::compile(SVMCompiler &compiler)
  3884. {
  3885. ShaderInput *vector_in = input("Vector");
  3886. ShaderOutput *x_out = output("X");
  3887. ShaderOutput *y_out = output("Y");
  3888. ShaderOutput *z_out = output("Z");
  3889. compiler.add_node(
  3890. NODE_SEPARATE_VECTOR, compiler.stack_assign(vector_in), 0, compiler.stack_assign(x_out));
  3891. compiler.add_node(
  3892. NODE_SEPARATE_VECTOR, compiler.stack_assign(vector_in), 1, compiler.stack_assign(y_out));
  3893. compiler.add_node(
  3894. NODE_SEPARATE_VECTOR, compiler.stack_assign(vector_in), 2, compiler.stack_assign(z_out));
  3895. }
  3896. void SeparateXYZNode::compile(OSLCompiler &compiler)
  3897. {
  3898. compiler.add(this, "node_separate_xyz");
  3899. }
  3900. /* Separate HSV */
  3901. NODE_DEFINE(SeparateHSVNode)
  3902. {
  3903. NodeType *type = NodeType::add("separate_hsv", create, NodeType::SHADER);
  3904. SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
  3905. SOCKET_OUT_FLOAT(h, "H");
  3906. SOCKET_OUT_FLOAT(s, "S");
  3907. SOCKET_OUT_FLOAT(v, "V");
  3908. return type;
  3909. }
  3910. SeparateHSVNode::SeparateHSVNode() : ShaderNode(node_type)
  3911. {
  3912. }
  3913. void SeparateHSVNode::constant_fold(const ConstantFolder &folder)
  3914. {
  3915. if (folder.all_inputs_constant()) {
  3916. float3 hsv = rgb_to_hsv(color);
  3917. for (int channel = 0; channel < 3; channel++) {
  3918. if (outputs[channel] == folder.output) {
  3919. folder.make_constant(hsv[channel]);
  3920. return;
  3921. }
  3922. }
  3923. }
  3924. }
  3925. void SeparateHSVNode::compile(SVMCompiler &compiler)
  3926. {
  3927. ShaderInput *color_in = input("Color");
  3928. ShaderOutput *hue_out = output("H");
  3929. ShaderOutput *saturation_out = output("S");
  3930. ShaderOutput *value_out = output("V");
  3931. compiler.add_node(NODE_SEPARATE_HSV,
  3932. compiler.stack_assign(color_in),
  3933. compiler.stack_assign(hue_out),
  3934. compiler.stack_assign(saturation_out));
  3935. compiler.add_node(NODE_SEPARATE_HSV, compiler.stack_assign(value_out));
  3936. }
  3937. void SeparateHSVNode::compile(OSLCompiler &compiler)
  3938. {
  3939. compiler.add(this, "node_separate_hsv");
  3940. }
  3941. /* Hue Saturation Value */
  3942. NODE_DEFINE(HSVNode)
  3943. {
  3944. NodeType *type = NodeType::add("hsv", create, NodeType::SHADER);
  3945. SOCKET_IN_FLOAT(hue, "Hue", 0.5f);
  3946. SOCKET_IN_FLOAT(saturation, "Saturation", 1.0f);
  3947. SOCKET_IN_FLOAT(value, "Value", 1.0f);
  3948. SOCKET_IN_FLOAT(fac, "Fac", 1.0f);
  3949. SOCKET_IN_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
  3950. SOCKET_OUT_COLOR(color, "Color");
  3951. return type;
  3952. }
  3953. HSVNode::HSVNode() : ShaderNode(node_type)
  3954. {
  3955. }
  3956. void HSVNode::compile(SVMCompiler &compiler)
  3957. {
  3958. ShaderInput *hue_in = input("Hue");
  3959. ShaderInput *saturation_in = input("Saturation");
  3960. ShaderInput *value_in = input("Value");
  3961. ShaderInput *fac_in = input("Fac");
  3962. ShaderInput *color_in = input("Color");
  3963. ShaderOutput *color_out = output("Color");
  3964. compiler.add_node(NODE_HSV,
  3965. compiler.encode_uchar4(compiler.stack_assign(color_in),
  3966. compiler.stack_assign(fac_in),
  3967. compiler.stack_assign(color_out)),
  3968. compiler.encode_uchar4(compiler.stack_assign(hue_in),
  3969. compiler.stack_assign(saturation_in),
  3970. compiler.stack_assign(value_in)));
  3971. }
  3972. void HSVNode::compile(OSLCompiler &compiler)
  3973. {
  3974. compiler.add(this, "node_hsv");
  3975. }
  3976. /* Attribute */
  3977. NODE_DEFINE(AttributeNode)
  3978. {
  3979. NodeType *type = NodeType::add("attribute", create, NodeType::SHADER);
  3980. SOCKET_STRING(attribute, "Attribute", ustring());
  3981. SOCKET_OUT_COLOR(color, "Color");
  3982. SOCKET_OUT_VECTOR(vector, "Vector");
  3983. SOCKET_OUT_FLOAT(fac, "Fac");
  3984. return type;
  3985. }
  3986. AttributeNode::AttributeNode() : ShaderNode(node_type)
  3987. {
  3988. }
  3989. void AttributeNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  3990. {
  3991. ShaderOutput *color_out = output("Color");
  3992. ShaderOutput *vector_out = output("Vector");
  3993. ShaderOutput *fac_out = output("Fac");
  3994. if (!color_out->links.empty() || !vector_out->links.empty() || !fac_out->links.empty()) {
  3995. attributes->add_standard(attribute);
  3996. }
  3997. if (shader->has_volume) {
  3998. attributes->add(ATTR_STD_GENERATED_TRANSFORM);
  3999. }
  4000. ShaderNode::attributes(shader, attributes);
  4001. }
  4002. void AttributeNode::compile(SVMCompiler &compiler)
  4003. {
  4004. ShaderOutput *color_out = output("Color");
  4005. ShaderOutput *vector_out = output("Vector");
  4006. ShaderOutput *fac_out = output("Fac");
  4007. ShaderNodeType attr_node = NODE_ATTR;
  4008. int attr = compiler.attribute_standard(attribute);
  4009. if (bump == SHADER_BUMP_DX)
  4010. attr_node = NODE_ATTR_BUMP_DX;
  4011. else if (bump == SHADER_BUMP_DY)
  4012. attr_node = NODE_ATTR_BUMP_DY;
  4013. if (!color_out->links.empty() || !vector_out->links.empty()) {
  4014. if (!color_out->links.empty()) {
  4015. compiler.add_node(attr_node, attr, compiler.stack_assign(color_out), NODE_ATTR_FLOAT3);
  4016. }
  4017. if (!vector_out->links.empty()) {
  4018. compiler.add_node(attr_node, attr, compiler.stack_assign(vector_out), NODE_ATTR_FLOAT3);
  4019. }
  4020. }
  4021. if (!fac_out->links.empty()) {
  4022. compiler.add_node(attr_node, attr, compiler.stack_assign(fac_out), NODE_ATTR_FLOAT);
  4023. }
  4024. }
  4025. void AttributeNode::compile(OSLCompiler &compiler)
  4026. {
  4027. if (bump == SHADER_BUMP_DX)
  4028. compiler.parameter("bump_offset", "dx");
  4029. else if (bump == SHADER_BUMP_DY)
  4030. compiler.parameter("bump_offset", "dy");
  4031. else
  4032. compiler.parameter("bump_offset", "center");
  4033. if (Attribute::name_standard(attribute.c_str()) != ATTR_STD_NONE)
  4034. compiler.parameter("name", (string("geom:") + attribute.c_str()).c_str());
  4035. else
  4036. compiler.parameter("name", attribute.c_str());
  4037. compiler.add(this, "node_attribute");
  4038. }
  4039. /* Camera */
  4040. NODE_DEFINE(CameraNode)
  4041. {
  4042. NodeType *type = NodeType::add("camera_info", create, NodeType::SHADER);
  4043. SOCKET_OUT_VECTOR(view_vector, "View Vector");
  4044. SOCKET_OUT_FLOAT(view_z_depth, "View Z Depth");
  4045. SOCKET_OUT_FLOAT(view_distance, "View Distance");
  4046. return type;
  4047. }
  4048. CameraNode::CameraNode() : ShaderNode(node_type)
  4049. {
  4050. }
  4051. void CameraNode::compile(SVMCompiler &compiler)
  4052. {
  4053. ShaderOutput *vector_out = output("View Vector");
  4054. ShaderOutput *z_depth_out = output("View Z Depth");
  4055. ShaderOutput *distance_out = output("View Distance");
  4056. compiler.add_node(NODE_CAMERA,
  4057. compiler.stack_assign(vector_out),
  4058. compiler.stack_assign(z_depth_out),
  4059. compiler.stack_assign(distance_out));
  4060. }
  4061. void CameraNode::compile(OSLCompiler &compiler)
  4062. {
  4063. compiler.add(this, "node_camera");
  4064. }
  4065. /* Fresnel */
  4066. NODE_DEFINE(FresnelNode)
  4067. {
  4068. NodeType *type = NodeType::add("fresnel", create, NodeType::SHADER);
  4069. SOCKET_IN_NORMAL(normal,
  4070. "Normal",
  4071. make_float3(0.0f, 0.0f, 0.0f),
  4072. SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
  4073. SOCKET_IN_FLOAT(IOR, "IOR", 1.45f);
  4074. SOCKET_OUT_FLOAT(fac, "Fac");
  4075. return type;
  4076. }
  4077. FresnelNode::FresnelNode() : ShaderNode(node_type)
  4078. {
  4079. }
  4080. void FresnelNode::compile(SVMCompiler &compiler)
  4081. {
  4082. ShaderInput *normal_in = input("Normal");
  4083. ShaderInput *IOR_in = input("IOR");
  4084. ShaderOutput *fac_out = output("Fac");
  4085. compiler.add_node(NODE_FRESNEL,
  4086. compiler.stack_assign(IOR_in),
  4087. __float_as_int(IOR),
  4088. compiler.encode_uchar4(compiler.stack_assign_if_linked(normal_in),
  4089. compiler.stack_assign(fac_out)));
  4090. }
  4091. void FresnelNode::compile(OSLCompiler &compiler)
  4092. {
  4093. compiler.add(this, "node_fresnel");
  4094. }
  4095. /* Layer Weight */
  4096. NODE_DEFINE(LayerWeightNode)
  4097. {
  4098. NodeType *type = NodeType::add("layer_weight", create, NodeType::SHADER);
  4099. SOCKET_IN_NORMAL(normal,
  4100. "Normal",
  4101. make_float3(0.0f, 0.0f, 0.0f),
  4102. SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
  4103. SOCKET_IN_FLOAT(blend, "Blend", 0.5f);
  4104. SOCKET_OUT_FLOAT(fresnel, "Fresnel");
  4105. SOCKET_OUT_FLOAT(facing, "Facing");
  4106. return type;
  4107. }
  4108. LayerWeightNode::LayerWeightNode() : ShaderNode(node_type)
  4109. {
  4110. }
  4111. void LayerWeightNode::compile(SVMCompiler &compiler)
  4112. {
  4113. ShaderInput *normal_in = input("Normal");
  4114. ShaderInput *blend_in = input("Blend");
  4115. ShaderOutput *fresnel_out = output("Fresnel");
  4116. ShaderOutput *facing_out = output("Facing");
  4117. if (!fresnel_out->links.empty()) {
  4118. compiler.add_node(NODE_LAYER_WEIGHT,
  4119. compiler.stack_assign_if_linked(blend_in),
  4120. __float_as_int(blend),
  4121. compiler.encode_uchar4(NODE_LAYER_WEIGHT_FRESNEL,
  4122. compiler.stack_assign_if_linked(normal_in),
  4123. compiler.stack_assign(fresnel_out)));
  4124. }
  4125. if (!facing_out->links.empty()) {
  4126. compiler.add_node(NODE_LAYER_WEIGHT,
  4127. compiler.stack_assign_if_linked(blend_in),
  4128. __float_as_int(blend),
  4129. compiler.encode_uchar4(NODE_LAYER_WEIGHT_FACING,
  4130. compiler.stack_assign_if_linked(normal_in),
  4131. compiler.stack_assign(facing_out)));
  4132. }
  4133. }
  4134. void LayerWeightNode::compile(OSLCompiler &compiler)
  4135. {
  4136. compiler.add(this, "node_layer_weight");
  4137. }
  4138. /* Wireframe */
  4139. NODE_DEFINE(WireframeNode)
  4140. {
  4141. NodeType *type = NodeType::add("wireframe", create, NodeType::SHADER);
  4142. SOCKET_BOOLEAN(use_pixel_size, "Use Pixel Size", false);
  4143. SOCKET_IN_FLOAT(size, "Size", 0.01f);
  4144. SOCKET_OUT_FLOAT(fac, "Fac");
  4145. return type;
  4146. }
  4147. WireframeNode::WireframeNode() : ShaderNode(node_type)
  4148. {
  4149. }
  4150. void WireframeNode::compile(SVMCompiler &compiler)
  4151. {
  4152. ShaderInput *size_in = input("Size");
  4153. ShaderOutput *fac_out = output("Fac");
  4154. NodeBumpOffset bump_offset = NODE_BUMP_OFFSET_CENTER;
  4155. if (bump == SHADER_BUMP_DX) {
  4156. bump_offset = NODE_BUMP_OFFSET_DX;
  4157. }
  4158. else if (bump == SHADER_BUMP_DY) {
  4159. bump_offset = NODE_BUMP_OFFSET_DY;
  4160. }
  4161. compiler.add_node(NODE_WIREFRAME,
  4162. compiler.stack_assign(size_in),
  4163. compiler.stack_assign(fac_out),
  4164. compiler.encode_uchar4(use_pixel_size, bump_offset, 0, 0));
  4165. }
  4166. void WireframeNode::compile(OSLCompiler &compiler)
  4167. {
  4168. if (bump == SHADER_BUMP_DX) {
  4169. compiler.parameter("bump_offset", "dx");
  4170. }
  4171. else if (bump == SHADER_BUMP_DY) {
  4172. compiler.parameter("bump_offset", "dy");
  4173. }
  4174. else {
  4175. compiler.parameter("bump_offset", "center");
  4176. }
  4177. compiler.parameter(this, "use_pixel_size");
  4178. compiler.add(this, "node_wireframe");
  4179. }
  4180. /* Wavelength */
  4181. NODE_DEFINE(WavelengthNode)
  4182. {
  4183. NodeType *type = NodeType::add("wavelength", create, NodeType::SHADER);
  4184. SOCKET_IN_FLOAT(wavelength, "Wavelength", 500.0f);
  4185. SOCKET_OUT_COLOR(color, "Color");
  4186. return type;
  4187. }
  4188. WavelengthNode::WavelengthNode() : ShaderNode(node_type)
  4189. {
  4190. }
  4191. void WavelengthNode::compile(SVMCompiler &compiler)
  4192. {
  4193. ShaderInput *wavelength_in = input("Wavelength");
  4194. ShaderOutput *color_out = output("Color");
  4195. compiler.add_node(
  4196. NODE_WAVELENGTH, compiler.stack_assign(wavelength_in), compiler.stack_assign(color_out));
  4197. }
  4198. void WavelengthNode::compile(OSLCompiler &compiler)
  4199. {
  4200. compiler.add(this, "node_wavelength");
  4201. }
  4202. /* Blackbody */
  4203. NODE_DEFINE(BlackbodyNode)
  4204. {
  4205. NodeType *type = NodeType::add("blackbody", create, NodeType::SHADER);
  4206. SOCKET_IN_FLOAT(temperature, "Temperature", 1200.0f);
  4207. SOCKET_OUT_COLOR(color, "Color");
  4208. return type;
  4209. }
  4210. BlackbodyNode::BlackbodyNode() : ShaderNode(node_type)
  4211. {
  4212. }
  4213. void BlackbodyNode::constant_fold(const ConstantFolder &folder)
  4214. {
  4215. if (folder.all_inputs_constant()) {
  4216. folder.make_constant(svm_math_blackbody_color(temperature));
  4217. }
  4218. }
  4219. void BlackbodyNode::compile(SVMCompiler &compiler)
  4220. {
  4221. ShaderInput *temperature_in = input("Temperature");
  4222. ShaderOutput *color_out = output("Color");
  4223. compiler.add_node(
  4224. NODE_BLACKBODY, compiler.stack_assign(temperature_in), compiler.stack_assign(color_out));
  4225. }
  4226. void BlackbodyNode::compile(OSLCompiler &compiler)
  4227. {
  4228. compiler.add(this, "node_blackbody");
  4229. }
  4230. /* Output */
  4231. NODE_DEFINE(OutputNode)
  4232. {
  4233. NodeType *type = NodeType::add("output", create, NodeType::SHADER);
  4234. SOCKET_IN_CLOSURE(surface, "Surface");
  4235. SOCKET_IN_CLOSURE(volume, "Volume");
  4236. SOCKET_IN_VECTOR(displacement, "Displacement", make_float3(0.0f, 0.0f, 0.0f));
  4237. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f));
  4238. return type;
  4239. }
  4240. OutputNode::OutputNode() : ShaderNode(node_type)
  4241. {
  4242. special_type = SHADER_SPECIAL_TYPE_OUTPUT;
  4243. }
  4244. void OutputNode::compile(SVMCompiler &compiler)
  4245. {
  4246. if (compiler.output_type() == SHADER_TYPE_DISPLACEMENT) {
  4247. ShaderInput *displacement_in = input("Displacement");
  4248. if (displacement_in->link) {
  4249. compiler.add_node(NODE_SET_DISPLACEMENT, compiler.stack_assign(displacement_in));
  4250. }
  4251. }
  4252. }
  4253. void OutputNode::compile(OSLCompiler &compiler)
  4254. {
  4255. if (compiler.output_type() == SHADER_TYPE_SURFACE)
  4256. compiler.add(this, "node_output_surface");
  4257. else if (compiler.output_type() == SHADER_TYPE_VOLUME)
  4258. compiler.add(this, "node_output_volume");
  4259. else if (compiler.output_type() == SHADER_TYPE_DISPLACEMENT)
  4260. compiler.add(this, "node_output_displacement");
  4261. }
  4262. /* Math */
  4263. NODE_DEFINE(MathNode)
  4264. {
  4265. NodeType *type = NodeType::add("math", create, NodeType::SHADER);
  4266. static NodeEnum type_enum;
  4267. type_enum.insert("add", NODE_MATH_ADD);
  4268. type_enum.insert("subtract", NODE_MATH_SUBTRACT);
  4269. type_enum.insert("multiply", NODE_MATH_MULTIPLY);
  4270. type_enum.insert("divide", NODE_MATH_DIVIDE);
  4271. type_enum.insert("sine", NODE_MATH_SINE);
  4272. type_enum.insert("cosine", NODE_MATH_COSINE);
  4273. type_enum.insert("tangent", NODE_MATH_TANGENT);
  4274. type_enum.insert("arcsine", NODE_MATH_ARCSINE);
  4275. type_enum.insert("arccosine", NODE_MATH_ARCCOSINE);
  4276. type_enum.insert("arctangent", NODE_MATH_ARCTANGENT);
  4277. type_enum.insert("power", NODE_MATH_POWER);
  4278. type_enum.insert("logarithm", NODE_MATH_LOGARITHM);
  4279. type_enum.insert("minimum", NODE_MATH_MINIMUM);
  4280. type_enum.insert("maximum", NODE_MATH_MAXIMUM);
  4281. type_enum.insert("round", NODE_MATH_ROUND);
  4282. type_enum.insert("less_than", NODE_MATH_LESS_THAN);
  4283. type_enum.insert("greater_than", NODE_MATH_GREATER_THAN);
  4284. type_enum.insert("modulo", NODE_MATH_MODULO);
  4285. type_enum.insert("absolute", NODE_MATH_ABSOLUTE);
  4286. type_enum.insert("arctan2", NODE_MATH_ARCTAN2);
  4287. type_enum.insert("floor", NODE_MATH_FLOOR);
  4288. type_enum.insert("ceil", NODE_MATH_CEIL);
  4289. type_enum.insert("fract", NODE_MATH_FRACT);
  4290. type_enum.insert("sqrt", NODE_MATH_SQRT);
  4291. SOCKET_ENUM(type, "Type", type_enum, NODE_MATH_ADD);
  4292. SOCKET_BOOLEAN(use_clamp, "Use Clamp", false);
  4293. SOCKET_IN_FLOAT(value1, "Value1", 0.0f);
  4294. SOCKET_IN_FLOAT(value2, "Value2", 0.0f);
  4295. SOCKET_OUT_FLOAT(value, "Value");
  4296. return type;
  4297. }
  4298. MathNode::MathNode() : ShaderNode(node_type)
  4299. {
  4300. }
  4301. void MathNode::constant_fold(const ConstantFolder &folder)
  4302. {
  4303. if (folder.all_inputs_constant()) {
  4304. folder.make_constant_clamp(svm_math(type, value1, value2), use_clamp);
  4305. }
  4306. else {
  4307. folder.fold_math(type, use_clamp);
  4308. }
  4309. }
  4310. void MathNode::compile(SVMCompiler &compiler)
  4311. {
  4312. ShaderInput *value1_in = input("Value1");
  4313. ShaderInput *value2_in = input("Value2");
  4314. ShaderOutput *value_out = output("Value");
  4315. compiler.add_node(
  4316. NODE_MATH, type, compiler.stack_assign(value1_in), compiler.stack_assign(value2_in));
  4317. compiler.add_node(NODE_MATH, compiler.stack_assign(value_out));
  4318. if (use_clamp) {
  4319. compiler.add_node(NODE_MATH, NODE_MATH_CLAMP, compiler.stack_assign(value_out));
  4320. compiler.add_node(NODE_MATH, compiler.stack_assign(value_out));
  4321. }
  4322. }
  4323. void MathNode::compile(OSLCompiler &compiler)
  4324. {
  4325. compiler.parameter(this, "type");
  4326. compiler.parameter(this, "use_clamp");
  4327. compiler.add(this, "node_math");
  4328. }
  4329. /* VectorMath */
  4330. NODE_DEFINE(VectorMathNode)
  4331. {
  4332. NodeType *type = NodeType::add("vector_math", create, NodeType::SHADER);
  4333. static NodeEnum type_enum;
  4334. type_enum.insert("add", NODE_VECTOR_MATH_ADD);
  4335. type_enum.insert("subtract", NODE_VECTOR_MATH_SUBTRACT);
  4336. type_enum.insert("average", NODE_VECTOR_MATH_AVERAGE);
  4337. type_enum.insert("dot_product", NODE_VECTOR_MATH_DOT_PRODUCT);
  4338. type_enum.insert("cross_product", NODE_VECTOR_MATH_CROSS_PRODUCT);
  4339. type_enum.insert("normalize", NODE_VECTOR_MATH_NORMALIZE);
  4340. SOCKET_ENUM(type, "Type", type_enum, NODE_VECTOR_MATH_ADD);
  4341. SOCKET_IN_VECTOR(vector1, "Vector1", make_float3(0.0f, 0.0f, 0.0f));
  4342. SOCKET_IN_VECTOR(vector2, "Vector2", make_float3(0.0f, 0.0f, 0.0f));
  4343. SOCKET_OUT_FLOAT(value, "Value");
  4344. SOCKET_OUT_VECTOR(vector, "Vector");
  4345. return type;
  4346. }
  4347. VectorMathNode::VectorMathNode() : ShaderNode(node_type)
  4348. {
  4349. }
  4350. void VectorMathNode::constant_fold(const ConstantFolder &folder)
  4351. {
  4352. float value;
  4353. float3 vector;
  4354. if (folder.all_inputs_constant()) {
  4355. svm_vector_math(&value, &vector, type, vector1, vector2);
  4356. if (folder.output == output("Value")) {
  4357. folder.make_constant(value);
  4358. }
  4359. else if (folder.output == output("Vector")) {
  4360. folder.make_constant(vector);
  4361. }
  4362. }
  4363. else {
  4364. folder.fold_vector_math(type);
  4365. }
  4366. }
  4367. void VectorMathNode::compile(SVMCompiler &compiler)
  4368. {
  4369. ShaderInput *vector1_in = input("Vector1");
  4370. ShaderInput *vector2_in = input("Vector2");
  4371. ShaderOutput *value_out = output("Value");
  4372. ShaderOutput *vector_out = output("Vector");
  4373. compiler.add_node(NODE_VECTOR_MATH,
  4374. type,
  4375. compiler.stack_assign(vector1_in),
  4376. compiler.stack_assign(vector2_in));
  4377. compiler.add_node(
  4378. NODE_VECTOR_MATH, compiler.stack_assign(value_out), compiler.stack_assign(vector_out));
  4379. }
  4380. void VectorMathNode::compile(OSLCompiler &compiler)
  4381. {
  4382. compiler.parameter(this, "type");
  4383. compiler.add(this, "node_vector_math");
  4384. }
  4385. /* VectorTransform */
  4386. NODE_DEFINE(VectorTransformNode)
  4387. {
  4388. NodeType *type = NodeType::add("vector_transform", create, NodeType::SHADER);
  4389. static NodeEnum type_enum;
  4390. type_enum.insert("vector", NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
  4391. type_enum.insert("point", NODE_VECTOR_TRANSFORM_TYPE_POINT);
  4392. type_enum.insert("normal", NODE_VECTOR_TRANSFORM_TYPE_NORMAL);
  4393. SOCKET_ENUM(type, "Type", type_enum, NODE_VECTOR_TRANSFORM_TYPE_VECTOR);
  4394. static NodeEnum space_enum;
  4395. space_enum.insert("world", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
  4396. space_enum.insert("object", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
  4397. space_enum.insert("camera", NODE_VECTOR_TRANSFORM_CONVERT_SPACE_CAMERA);
  4398. SOCKET_ENUM(convert_from, "Convert From", space_enum, NODE_VECTOR_TRANSFORM_CONVERT_SPACE_WORLD);
  4399. SOCKET_ENUM(convert_to, "Convert To", space_enum, NODE_VECTOR_TRANSFORM_CONVERT_SPACE_OBJECT);
  4400. SOCKET_IN_VECTOR(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
  4401. SOCKET_OUT_VECTOR(vector, "Vector");
  4402. return type;
  4403. }
  4404. VectorTransformNode::VectorTransformNode() : ShaderNode(node_type)
  4405. {
  4406. }
  4407. void VectorTransformNode::compile(SVMCompiler &compiler)
  4408. {
  4409. ShaderInput *vector_in = input("Vector");
  4410. ShaderOutput *vector_out = output("Vector");
  4411. compiler.add_node(
  4412. NODE_VECTOR_TRANSFORM,
  4413. compiler.encode_uchar4(type, convert_from, convert_to),
  4414. compiler.encode_uchar4(compiler.stack_assign(vector_in), compiler.stack_assign(vector_out)));
  4415. }
  4416. void VectorTransformNode::compile(OSLCompiler &compiler)
  4417. {
  4418. compiler.parameter(this, "type");
  4419. compiler.parameter(this, "convert_from");
  4420. compiler.parameter(this, "convert_to");
  4421. compiler.add(this, "node_vector_transform");
  4422. }
  4423. /* BumpNode */
  4424. NODE_DEFINE(BumpNode)
  4425. {
  4426. NodeType *type = NodeType::add("bump", create, NodeType::SHADER);
  4427. SOCKET_BOOLEAN(invert, "Invert", false);
  4428. SOCKET_BOOLEAN(use_object_space, "UseObjectSpace", false);
  4429. /* this input is used by the user, but after graph transform it is no longer
  4430. * used and moved to sampler center/x/y instead */
  4431. SOCKET_IN_FLOAT(height, "Height", 1.0f);
  4432. SOCKET_IN_FLOAT(sample_center, "SampleCenter", 0.0f);
  4433. SOCKET_IN_FLOAT(sample_x, "SampleX", 0.0f);
  4434. SOCKET_IN_FLOAT(sample_y, "SampleY", 0.0f);
  4435. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  4436. SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
  4437. SOCKET_IN_FLOAT(distance, "Distance", 0.1f);
  4438. SOCKET_OUT_NORMAL(normal, "Normal");
  4439. return type;
  4440. }
  4441. BumpNode::BumpNode() : ShaderNode(node_type)
  4442. {
  4443. special_type = SHADER_SPECIAL_TYPE_BUMP;
  4444. }
  4445. void BumpNode::compile(SVMCompiler &compiler)
  4446. {
  4447. ShaderInput *center_in = input("SampleCenter");
  4448. ShaderInput *dx_in = input("SampleX");
  4449. ShaderInput *dy_in = input("SampleY");
  4450. ShaderInput *normal_in = input("Normal");
  4451. ShaderInput *strength_in = input("Strength");
  4452. ShaderInput *distance_in = input("Distance");
  4453. ShaderOutput *normal_out = output("Normal");
  4454. /* pack all parameters in the node */
  4455. compiler.add_node(NODE_SET_BUMP,
  4456. compiler.encode_uchar4(compiler.stack_assign_if_linked(normal_in),
  4457. compiler.stack_assign(distance_in),
  4458. invert,
  4459. use_object_space),
  4460. compiler.encode_uchar4(compiler.stack_assign(center_in),
  4461. compiler.stack_assign(dx_in),
  4462. compiler.stack_assign(dy_in),
  4463. compiler.stack_assign(strength_in)),
  4464. compiler.stack_assign(normal_out));
  4465. }
  4466. void BumpNode::compile(OSLCompiler &compiler)
  4467. {
  4468. compiler.parameter(this, "invert");
  4469. compiler.parameter(this, "use_object_space");
  4470. compiler.add(this, "node_bump");
  4471. }
  4472. void BumpNode::constant_fold(const ConstantFolder &folder)
  4473. {
  4474. ShaderInput *height_in = input("Height");
  4475. ShaderInput *normal_in = input("Normal");
  4476. if (height_in->link == NULL) {
  4477. if (normal_in->link == NULL) {
  4478. GeometryNode *geom = new GeometryNode();
  4479. folder.graph->add(geom);
  4480. folder.bypass(geom->output("Normal"));
  4481. }
  4482. else {
  4483. folder.bypass(normal_in->link);
  4484. }
  4485. }
  4486. /* TODO(sergey): Ignore bump with zero strength. */
  4487. }
  4488. /* Curve node */
  4489. CurvesNode::CurvesNode(const NodeType *node_type) : ShaderNode(node_type)
  4490. {
  4491. }
  4492. void CurvesNode::constant_fold(const ConstantFolder &folder, ShaderInput *value_in)
  4493. {
  4494. ShaderInput *fac_in = input("Fac");
  4495. /* evaluate fully constant node */
  4496. if (folder.all_inputs_constant()) {
  4497. if (curves.size() == 0) {
  4498. return;
  4499. }
  4500. float3 pos = (value - make_float3(min_x, min_x, min_x)) / (max_x - min_x);
  4501. float3 result;
  4502. result[0] = rgb_ramp_lookup(curves.data(), pos[0], true, true, curves.size()).x;
  4503. result[1] = rgb_ramp_lookup(curves.data(), pos[1], true, true, curves.size()).y;
  4504. result[2] = rgb_ramp_lookup(curves.data(), pos[2], true, true, curves.size()).z;
  4505. folder.make_constant(interp(value, result, fac));
  4506. }
  4507. /* remove no-op node */
  4508. else if (!fac_in->link && fac == 0.0f) {
  4509. /* link is not null because otherwise all inputs are constant */
  4510. folder.bypass(value_in->link);
  4511. }
  4512. }
  4513. void CurvesNode::compile(SVMCompiler &compiler,
  4514. int type,
  4515. ShaderInput *value_in,
  4516. ShaderOutput *value_out)
  4517. {
  4518. if (curves.size() == 0)
  4519. return;
  4520. ShaderInput *fac_in = input("Fac");
  4521. compiler.add_node(type,
  4522. compiler.encode_uchar4(compiler.stack_assign(fac_in),
  4523. compiler.stack_assign(value_in),
  4524. compiler.stack_assign(value_out)),
  4525. __float_as_int(min_x),
  4526. __float_as_int(max_x));
  4527. compiler.add_node(curves.size());
  4528. for (int i = 0; i < curves.size(); i++)
  4529. compiler.add_node(float3_to_float4(curves[i]));
  4530. }
  4531. void CurvesNode::compile(OSLCompiler &compiler, const char *name)
  4532. {
  4533. if (curves.size() == 0)
  4534. return;
  4535. compiler.parameter_color_array("ramp", curves);
  4536. compiler.parameter(this, "min_x");
  4537. compiler.parameter(this, "max_x");
  4538. compiler.add(this, name);
  4539. }
  4540. void CurvesNode::compile(SVMCompiler & /*compiler*/)
  4541. {
  4542. assert(0);
  4543. }
  4544. void CurvesNode::compile(OSLCompiler & /*compiler*/)
  4545. {
  4546. assert(0);
  4547. }
  4548. /* RGBCurvesNode */
  4549. NODE_DEFINE(RGBCurvesNode)
  4550. {
  4551. NodeType *type = NodeType::add("rgb_curves", create, NodeType::SHADER);
  4552. SOCKET_COLOR_ARRAY(curves, "Curves", array<float3>());
  4553. SOCKET_FLOAT(min_x, "Min X", 0.0f);
  4554. SOCKET_FLOAT(max_x, "Max X", 1.0f);
  4555. SOCKET_IN_FLOAT(fac, "Fac", 0.0f);
  4556. SOCKET_IN_COLOR(value, "Color", make_float3(0.0f, 0.0f, 0.0f));
  4557. SOCKET_OUT_COLOR(value, "Color");
  4558. return type;
  4559. }
  4560. RGBCurvesNode::RGBCurvesNode() : CurvesNode(node_type)
  4561. {
  4562. }
  4563. void RGBCurvesNode::constant_fold(const ConstantFolder &folder)
  4564. {
  4565. CurvesNode::constant_fold(folder, input("Color"));
  4566. }
  4567. void RGBCurvesNode::compile(SVMCompiler &compiler)
  4568. {
  4569. CurvesNode::compile(compiler, NODE_RGB_CURVES, input("Color"), output("Color"));
  4570. }
  4571. void RGBCurvesNode::compile(OSLCompiler &compiler)
  4572. {
  4573. CurvesNode::compile(compiler, "node_rgb_curves");
  4574. }
  4575. /* VectorCurvesNode */
  4576. NODE_DEFINE(VectorCurvesNode)
  4577. {
  4578. NodeType *type = NodeType::add("vector_curves", create, NodeType::SHADER);
  4579. SOCKET_VECTOR_ARRAY(curves, "Curves", array<float3>());
  4580. SOCKET_FLOAT(min_x, "Min X", 0.0f);
  4581. SOCKET_FLOAT(max_x, "Max X", 1.0f);
  4582. SOCKET_IN_FLOAT(fac, "Fac", 0.0f);
  4583. SOCKET_IN_VECTOR(value, "Vector", make_float3(0.0f, 0.0f, 0.0f));
  4584. SOCKET_OUT_VECTOR(value, "Vector");
  4585. return type;
  4586. }
  4587. VectorCurvesNode::VectorCurvesNode() : CurvesNode(node_type)
  4588. {
  4589. }
  4590. void VectorCurvesNode::constant_fold(const ConstantFolder &folder)
  4591. {
  4592. CurvesNode::constant_fold(folder, input("Vector"));
  4593. }
  4594. void VectorCurvesNode::compile(SVMCompiler &compiler)
  4595. {
  4596. CurvesNode::compile(compiler, NODE_VECTOR_CURVES, input("Vector"), output("Vector"));
  4597. }
  4598. void VectorCurvesNode::compile(OSLCompiler &compiler)
  4599. {
  4600. CurvesNode::compile(compiler, "node_vector_curves");
  4601. }
  4602. /* RGBRampNode */
  4603. NODE_DEFINE(RGBRampNode)
  4604. {
  4605. NodeType *type = NodeType::add("rgb_ramp", create, NodeType::SHADER);
  4606. SOCKET_COLOR_ARRAY(ramp, "Ramp", array<float3>());
  4607. SOCKET_FLOAT_ARRAY(ramp_alpha, "Ramp Alpha", array<float>());
  4608. SOCKET_BOOLEAN(interpolate, "Interpolate", true);
  4609. SOCKET_IN_FLOAT(fac, "Fac", 0.0f);
  4610. SOCKET_OUT_COLOR(color, "Color");
  4611. SOCKET_OUT_FLOAT(alpha, "Alpha");
  4612. return type;
  4613. }
  4614. RGBRampNode::RGBRampNode() : ShaderNode(node_type)
  4615. {
  4616. }
  4617. void RGBRampNode::constant_fold(const ConstantFolder &folder)
  4618. {
  4619. if (ramp.size() == 0 || ramp.size() != ramp_alpha.size())
  4620. return;
  4621. if (folder.all_inputs_constant()) {
  4622. float f = clamp(fac, 0.0f, 1.0f) * (ramp.size() - 1);
  4623. /* clamp int as well in case of NaN */
  4624. int i = clamp((int)f, 0, ramp.size() - 1);
  4625. float t = f - (float)i;
  4626. bool use_lerp = interpolate && t > 0.0f;
  4627. if (folder.output == output("Color")) {
  4628. float3 color = rgb_ramp_lookup(ramp.data(), fac, use_lerp, false, ramp.size());
  4629. folder.make_constant(color);
  4630. }
  4631. else if (folder.output == output("Alpha")) {
  4632. float alpha = float_ramp_lookup(ramp_alpha.data(), fac, use_lerp, false, ramp_alpha.size());
  4633. folder.make_constant(alpha);
  4634. }
  4635. }
  4636. }
  4637. void RGBRampNode::compile(SVMCompiler &compiler)
  4638. {
  4639. if (ramp.size() == 0 || ramp.size() != ramp_alpha.size())
  4640. return;
  4641. ShaderInput *fac_in = input("Fac");
  4642. ShaderOutput *color_out = output("Color");
  4643. ShaderOutput *alpha_out = output("Alpha");
  4644. compiler.add_node(NODE_RGB_RAMP,
  4645. compiler.encode_uchar4(compiler.stack_assign(fac_in),
  4646. compiler.stack_assign_if_linked(color_out),
  4647. compiler.stack_assign_if_linked(alpha_out)),
  4648. interpolate);
  4649. compiler.add_node(ramp.size());
  4650. for (int i = 0; i < ramp.size(); i++)
  4651. compiler.add_node(make_float4(ramp[i].x, ramp[i].y, ramp[i].z, ramp_alpha[i]));
  4652. }
  4653. void RGBRampNode::compile(OSLCompiler &compiler)
  4654. {
  4655. if (ramp.size() == 0 || ramp.size() != ramp_alpha.size())
  4656. return;
  4657. compiler.parameter_color_array("ramp_color", ramp);
  4658. compiler.parameter_array("ramp_alpha", ramp_alpha.data(), ramp_alpha.size());
  4659. compiler.parameter(this, "interpolate");
  4660. compiler.add(this, "node_rgb_ramp");
  4661. }
  4662. /* Set Normal Node */
  4663. NODE_DEFINE(SetNormalNode)
  4664. {
  4665. NodeType *type = NodeType::add("set_normal", create, NodeType::SHADER);
  4666. SOCKET_IN_VECTOR(direction, "Direction", make_float3(0.0f, 0.0f, 0.0f));
  4667. SOCKET_OUT_NORMAL(normal, "Normal");
  4668. return type;
  4669. }
  4670. SetNormalNode::SetNormalNode() : ShaderNode(node_type)
  4671. {
  4672. }
  4673. void SetNormalNode::compile(SVMCompiler &compiler)
  4674. {
  4675. ShaderInput *direction_in = input("Direction");
  4676. ShaderOutput *normal_out = output("Normal");
  4677. compiler.add_node(NODE_CLOSURE_SET_NORMAL,
  4678. compiler.stack_assign(direction_in),
  4679. compiler.stack_assign(normal_out));
  4680. }
  4681. void SetNormalNode::compile(OSLCompiler &compiler)
  4682. {
  4683. compiler.add(this, "node_set_normal");
  4684. }
  4685. /* OSLNode */
  4686. OSLNode::OSLNode() : ShaderNode(new NodeType(NodeType::SHADER))
  4687. {
  4688. special_type = SHADER_SPECIAL_TYPE_OSL;
  4689. }
  4690. OSLNode::~OSLNode()
  4691. {
  4692. delete type;
  4693. }
  4694. ShaderNode *OSLNode::clone() const
  4695. {
  4696. return OSLNode::create(this->inputs.size(), this);
  4697. }
  4698. OSLNode *OSLNode::create(size_t num_inputs, const OSLNode *from)
  4699. {
  4700. /* allocate space for the node itself and parameters, aligned to 16 bytes
  4701. * assuming that's the most parameter types need */
  4702. size_t node_size = align_up(sizeof(OSLNode), 16);
  4703. size_t inputs_size = align_up(SocketType::max_size(), 16) * num_inputs;
  4704. char *node_memory = (char *)operator new(node_size + inputs_size);
  4705. memset(node_memory, 0, node_size + inputs_size);
  4706. if (!from) {
  4707. return new (node_memory) OSLNode();
  4708. }
  4709. else {
  4710. /* copy input default values and node type for cloning */
  4711. memcpy(node_memory + node_size, (char *)from + node_size, inputs_size);
  4712. OSLNode *node = new (node_memory) OSLNode(*from);
  4713. node->type = new NodeType(*(from->type));
  4714. return node;
  4715. }
  4716. }
  4717. char *OSLNode::input_default_value()
  4718. {
  4719. /* pointer to default value storage, which is the same as our actual value */
  4720. size_t num_inputs = type->inputs.size();
  4721. size_t inputs_size = align_up(SocketType::max_size(), 16) * num_inputs;
  4722. return (char *)this + align_up(sizeof(OSLNode), 16) + inputs_size;
  4723. }
  4724. void OSLNode::add_input(ustring name, SocketType::Type socket_type)
  4725. {
  4726. char *memory = input_default_value();
  4727. size_t offset = memory - (char *)this;
  4728. const_cast<NodeType *>(type)->register_input(
  4729. name, name, socket_type, offset, memory, NULL, NULL, SocketType::LINKABLE);
  4730. }
  4731. void OSLNode::add_output(ustring name, SocketType::Type socket_type)
  4732. {
  4733. const_cast<NodeType *>(type)->register_output(name, name, socket_type);
  4734. }
  4735. void OSLNode::compile(SVMCompiler &)
  4736. {
  4737. /* doesn't work for SVM, obviously ... */
  4738. }
  4739. void OSLNode::compile(OSLCompiler &compiler)
  4740. {
  4741. if (!filepath.empty())
  4742. compiler.add(this, filepath.c_str(), true);
  4743. else
  4744. compiler.add(this, bytecode_hash.c_str(), false);
  4745. }
  4746. /* Normal Map */
  4747. NODE_DEFINE(NormalMapNode)
  4748. {
  4749. NodeType *type = NodeType::add("normal_map", create, NodeType::SHADER);
  4750. static NodeEnum space_enum;
  4751. space_enum.insert("tangent", NODE_NORMAL_MAP_TANGENT);
  4752. space_enum.insert("object", NODE_NORMAL_MAP_OBJECT);
  4753. space_enum.insert("world", NODE_NORMAL_MAP_WORLD);
  4754. space_enum.insert("blender_object", NODE_NORMAL_MAP_BLENDER_OBJECT);
  4755. space_enum.insert("blender_world", NODE_NORMAL_MAP_BLENDER_WORLD);
  4756. SOCKET_ENUM(space, "Space", space_enum, NODE_NORMAL_MAP_TANGENT);
  4757. SOCKET_STRING(attribute, "Attribute", ustring());
  4758. SOCKET_IN_NORMAL(normal_osl,
  4759. "NormalIn",
  4760. make_float3(0.0f, 0.0f, 0.0f),
  4761. SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
  4762. SOCKET_IN_FLOAT(strength, "Strength", 1.0f);
  4763. SOCKET_IN_COLOR(color, "Color", make_float3(0.5f, 0.5f, 1.0f));
  4764. SOCKET_OUT_NORMAL(normal, "Normal");
  4765. return type;
  4766. }
  4767. NormalMapNode::NormalMapNode() : ShaderNode(node_type)
  4768. {
  4769. }
  4770. void NormalMapNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  4771. {
  4772. if (shader->has_surface && space == NODE_NORMAL_MAP_TANGENT) {
  4773. if (attribute.empty()) {
  4774. attributes->add(ATTR_STD_UV_TANGENT);
  4775. attributes->add(ATTR_STD_UV_TANGENT_SIGN);
  4776. }
  4777. else {
  4778. attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
  4779. attributes->add(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
  4780. }
  4781. attributes->add(ATTR_STD_VERTEX_NORMAL);
  4782. }
  4783. ShaderNode::attributes(shader, attributes);
  4784. }
  4785. void NormalMapNode::compile(SVMCompiler &compiler)
  4786. {
  4787. ShaderInput *color_in = input("Color");
  4788. ShaderInput *strength_in = input("Strength");
  4789. ShaderOutput *normal_out = output("Normal");
  4790. int attr = 0, attr_sign = 0;
  4791. if (space == NODE_NORMAL_MAP_TANGENT) {
  4792. if (attribute.empty()) {
  4793. attr = compiler.attribute(ATTR_STD_UV_TANGENT);
  4794. attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
  4795. }
  4796. else {
  4797. attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
  4798. attr_sign = compiler.attribute(
  4799. ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
  4800. }
  4801. }
  4802. compiler.add_node(NODE_NORMAL_MAP,
  4803. compiler.encode_uchar4(compiler.stack_assign(color_in),
  4804. compiler.stack_assign(strength_in),
  4805. compiler.stack_assign(normal_out),
  4806. space),
  4807. attr,
  4808. attr_sign);
  4809. }
  4810. void NormalMapNode::compile(OSLCompiler &compiler)
  4811. {
  4812. if (space == NODE_NORMAL_MAP_TANGENT) {
  4813. if (attribute.empty()) {
  4814. compiler.parameter("attr_name", ustring("geom:tangent"));
  4815. compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
  4816. }
  4817. else {
  4818. compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
  4819. compiler.parameter("attr_sign_name",
  4820. ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
  4821. }
  4822. }
  4823. compiler.parameter(this, "space");
  4824. compiler.add(this, "node_normal_map");
  4825. }
  4826. /* Tangent */
  4827. NODE_DEFINE(TangentNode)
  4828. {
  4829. NodeType *type = NodeType::add("tangent", create, NodeType::SHADER);
  4830. static NodeEnum direction_type_enum;
  4831. direction_type_enum.insert("radial", NODE_TANGENT_RADIAL);
  4832. direction_type_enum.insert("uv_map", NODE_TANGENT_UVMAP);
  4833. SOCKET_ENUM(direction_type, "Direction Type", direction_type_enum, NODE_TANGENT_RADIAL);
  4834. static NodeEnum axis_enum;
  4835. axis_enum.insert("x", NODE_TANGENT_AXIS_X);
  4836. axis_enum.insert("y", NODE_TANGENT_AXIS_Y);
  4837. axis_enum.insert("z", NODE_TANGENT_AXIS_Z);
  4838. SOCKET_ENUM(axis, "Axis", axis_enum, NODE_TANGENT_AXIS_X);
  4839. SOCKET_STRING(attribute, "Attribute", ustring());
  4840. SOCKET_IN_NORMAL(normal_osl,
  4841. "NormalIn",
  4842. make_float3(0.0f, 0.0f, 0.0f),
  4843. SocketType::LINK_NORMAL | SocketType::OSL_INTERNAL);
  4844. SOCKET_OUT_NORMAL(tangent, "Tangent");
  4845. return type;
  4846. }
  4847. TangentNode::TangentNode() : ShaderNode(node_type)
  4848. {
  4849. }
  4850. void TangentNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  4851. {
  4852. if (shader->has_surface) {
  4853. if (direction_type == NODE_TANGENT_UVMAP) {
  4854. if (attribute.empty())
  4855. attributes->add(ATTR_STD_UV_TANGENT);
  4856. else
  4857. attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
  4858. }
  4859. else
  4860. attributes->add(ATTR_STD_GENERATED);
  4861. }
  4862. ShaderNode::attributes(shader, attributes);
  4863. }
  4864. void TangentNode::compile(SVMCompiler &compiler)
  4865. {
  4866. ShaderOutput *tangent_out = output("Tangent");
  4867. int attr;
  4868. if (direction_type == NODE_TANGENT_UVMAP) {
  4869. if (attribute.empty())
  4870. attr = compiler.attribute(ATTR_STD_UV_TANGENT);
  4871. else
  4872. attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
  4873. }
  4874. else
  4875. attr = compiler.attribute(ATTR_STD_GENERATED);
  4876. compiler.add_node(
  4877. NODE_TANGENT,
  4878. compiler.encode_uchar4(compiler.stack_assign(tangent_out), direction_type, axis),
  4879. attr);
  4880. }
  4881. void TangentNode::compile(OSLCompiler &compiler)
  4882. {
  4883. if (direction_type == NODE_TANGENT_UVMAP) {
  4884. if (attribute.empty())
  4885. compiler.parameter("attr_name", ustring("geom:tangent"));
  4886. else
  4887. compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
  4888. }
  4889. compiler.parameter(this, "direction_type");
  4890. compiler.parameter(this, "axis");
  4891. compiler.add(this, "node_tangent");
  4892. }
  4893. /* Bevel */
  4894. NODE_DEFINE(BevelNode)
  4895. {
  4896. NodeType *type = NodeType::add("bevel", create, NodeType::SHADER);
  4897. SOCKET_INT(samples, "Samples", 4);
  4898. SOCKET_IN_FLOAT(radius, "Radius", 0.05f);
  4899. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  4900. SOCKET_OUT_NORMAL(bevel, "Normal");
  4901. return type;
  4902. }
  4903. BevelNode::BevelNode() : ShaderNode(node_type)
  4904. {
  4905. }
  4906. void BevelNode::compile(SVMCompiler &compiler)
  4907. {
  4908. ShaderInput *radius_in = input("Radius");
  4909. ShaderInput *normal_in = input("Normal");
  4910. ShaderOutput *normal_out = output("Normal");
  4911. compiler.add_node(NODE_BEVEL,
  4912. compiler.encode_uchar4(samples,
  4913. compiler.stack_assign(radius_in),
  4914. compiler.stack_assign_if_linked(normal_in),
  4915. compiler.stack_assign(normal_out)));
  4916. }
  4917. void BevelNode::compile(OSLCompiler &compiler)
  4918. {
  4919. compiler.parameter(this, "samples");
  4920. compiler.add(this, "node_bevel");
  4921. }
  4922. /* Displacement */
  4923. NODE_DEFINE(DisplacementNode)
  4924. {
  4925. NodeType *type = NodeType::add("displacement", create, NodeType::SHADER);
  4926. static NodeEnum space_enum;
  4927. space_enum.insert("object", NODE_NORMAL_MAP_OBJECT);
  4928. space_enum.insert("world", NODE_NORMAL_MAP_WORLD);
  4929. SOCKET_ENUM(space, "Space", space_enum, NODE_NORMAL_MAP_OBJECT);
  4930. SOCKET_IN_FLOAT(height, "Height", 0.0f);
  4931. SOCKET_IN_FLOAT(midlevel, "Midlevel", 0.5f);
  4932. SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
  4933. SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
  4934. SOCKET_OUT_VECTOR(displacement, "Displacement");
  4935. return type;
  4936. }
  4937. DisplacementNode::DisplacementNode() : ShaderNode(node_type)
  4938. {
  4939. }
  4940. void DisplacementNode::constant_fold(const ConstantFolder &folder)
  4941. {
  4942. if (folder.all_inputs_constant()) {
  4943. if ((height - midlevel == 0.0f) || (scale == 0.0f)) {
  4944. folder.make_zero();
  4945. }
  4946. }
  4947. }
  4948. void DisplacementNode::compile(SVMCompiler &compiler)
  4949. {
  4950. ShaderInput *height_in = input("Height");
  4951. ShaderInput *midlevel_in = input("Midlevel");
  4952. ShaderInput *scale_in = input("Scale");
  4953. ShaderInput *normal_in = input("Normal");
  4954. ShaderOutput *displacement_out = output("Displacement");
  4955. compiler.add_node(NODE_DISPLACEMENT,
  4956. compiler.encode_uchar4(compiler.stack_assign(height_in),
  4957. compiler.stack_assign(midlevel_in),
  4958. compiler.stack_assign(scale_in),
  4959. compiler.stack_assign_if_linked(normal_in)),
  4960. compiler.stack_assign(displacement_out),
  4961. space);
  4962. }
  4963. void DisplacementNode::compile(OSLCompiler &compiler)
  4964. {
  4965. compiler.parameter(this, "space");
  4966. compiler.add(this, "node_displacement");
  4967. }
  4968. /* Vector Displacement */
  4969. NODE_DEFINE(VectorDisplacementNode)
  4970. {
  4971. NodeType *type = NodeType::add("vector_displacement", create, NodeType::SHADER);
  4972. static NodeEnum space_enum;
  4973. space_enum.insert("tangent", NODE_NORMAL_MAP_TANGENT);
  4974. space_enum.insert("object", NODE_NORMAL_MAP_OBJECT);
  4975. space_enum.insert("world", NODE_NORMAL_MAP_WORLD);
  4976. SOCKET_ENUM(space, "Space", space_enum, NODE_NORMAL_MAP_TANGENT);
  4977. SOCKET_STRING(attribute, "Attribute", ustring());
  4978. SOCKET_IN_COLOR(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
  4979. SOCKET_IN_FLOAT(midlevel, "Midlevel", 0.0f);
  4980. SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
  4981. SOCKET_OUT_VECTOR(displacement, "Displacement");
  4982. return type;
  4983. }
  4984. VectorDisplacementNode::VectorDisplacementNode() : ShaderNode(node_type)
  4985. {
  4986. }
  4987. void VectorDisplacementNode::constant_fold(const ConstantFolder &folder)
  4988. {
  4989. if (folder.all_inputs_constant()) {
  4990. if ((vector == make_float3(0.0f, 0.0f, 0.0f) && midlevel == 0.0f) || (scale == 0.0f)) {
  4991. folder.make_zero();
  4992. }
  4993. }
  4994. }
  4995. void VectorDisplacementNode::attributes(Shader *shader, AttributeRequestSet *attributes)
  4996. {
  4997. if (shader->has_surface && space == NODE_NORMAL_MAP_TANGENT) {
  4998. if (attribute.empty()) {
  4999. attributes->add(ATTR_STD_UV_TANGENT);
  5000. attributes->add(ATTR_STD_UV_TANGENT_SIGN);
  5001. }
  5002. else {
  5003. attributes->add(ustring((string(attribute.c_str()) + ".tangent").c_str()));
  5004. attributes->add(ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
  5005. }
  5006. attributes->add(ATTR_STD_VERTEX_NORMAL);
  5007. }
  5008. ShaderNode::attributes(shader, attributes);
  5009. }
  5010. void VectorDisplacementNode::compile(SVMCompiler &compiler)
  5011. {
  5012. ShaderInput *vector_in = input("Vector");
  5013. ShaderInput *midlevel_in = input("Midlevel");
  5014. ShaderInput *scale_in = input("Scale");
  5015. ShaderOutput *displacement_out = output("Displacement");
  5016. int attr = 0, attr_sign = 0;
  5017. if (space == NODE_NORMAL_MAP_TANGENT) {
  5018. if (attribute.empty()) {
  5019. attr = compiler.attribute(ATTR_STD_UV_TANGENT);
  5020. attr_sign = compiler.attribute(ATTR_STD_UV_TANGENT_SIGN);
  5021. }
  5022. else {
  5023. attr = compiler.attribute(ustring((string(attribute.c_str()) + ".tangent").c_str()));
  5024. attr_sign = compiler.attribute(
  5025. ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
  5026. }
  5027. }
  5028. compiler.add_node(NODE_VECTOR_DISPLACEMENT,
  5029. compiler.encode_uchar4(compiler.stack_assign(vector_in),
  5030. compiler.stack_assign(midlevel_in),
  5031. compiler.stack_assign(scale_in),
  5032. compiler.stack_assign(displacement_out)),
  5033. attr,
  5034. attr_sign);
  5035. compiler.add_node(space);
  5036. }
  5037. void VectorDisplacementNode::compile(OSLCompiler &compiler)
  5038. {
  5039. if (space == NODE_NORMAL_MAP_TANGENT) {
  5040. if (attribute.empty()) {
  5041. compiler.parameter("attr_name", ustring("geom:tangent"));
  5042. compiler.parameter("attr_sign_name", ustring("geom:tangent_sign"));
  5043. }
  5044. else {
  5045. compiler.parameter("attr_name", ustring((string(attribute.c_str()) + ".tangent").c_str()));
  5046. compiler.parameter("attr_sign_name",
  5047. ustring((string(attribute.c_str()) + ".tangent_sign").c_str()));
  5048. }
  5049. }
  5050. compiler.parameter(this, "space");
  5051. compiler.add(this, "node_vector_displacement");
  5052. }
  5053. CCL_NAMESPACE_END