llface.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  1. /**
  2. * @file llface.cpp
  3. * @brief LLFace class implementation
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llface.h"
  34. #include "llfasttimer.h"
  35. #include "llgl.h"
  36. #include "llmatrix4a.h"
  37. #include "llrender.h"
  38. #include "llvolume.h"
  39. #include "lldrawpoolavatar.h"
  40. #include "lldrawpoolbump.h"
  41. #include "llpipeline.h"
  42. //MK
  43. #include "mkrlinterface.h"
  44. //mk
  45. #include "llsky.h"
  46. #include "llviewercamera.h"
  47. #include "llviewercontrol.h"
  48. #include "llviewerregion.h"
  49. #include "llviewershadermgr.h"
  50. #include "llviewertextureanim.h"
  51. #include "llviewertexturelist.h"
  52. #include "llviewerwindow.h"
  53. #include "llvoclouds.h"
  54. #include "llvopartgroup.h"
  55. #include "llvosky.h"
  56. #include "llvovolume.h"
  57. // gcc 12+ sees uninitialized LLVector4a's where there are none... HB
  58. #if defined(GCC_VERSION) && GCC_VERSION >= 120000
  59. # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  60. #endif
  61. #define LL_MAX_INDICES_COUNT 1000000
  62. static LLStaticHashedString sTextureIndexIn("texture_index_in");
  63. static LLStaticHashedString sColorIn("color_in");
  64. #define DOTVEC(a,b) (a.mV[0]*b.mV[0] + a.mV[1]*b.mV[1] + a.mV[2]*b.mV[2])
  65. /*
  66. For each vertex, given:
  67. B - binormal
  68. T - tangent
  69. N - normal
  70. P - position
  71. The resulting texture coordinate <u,v> is:
  72. u = 2(B dot P)
  73. v = 2(T dot P)
  74. */
  75. void planarProjection(LLVector2& tc, const LLVector4a& normal,
  76. const LLVector4a& center, const LLVector4a& vec)
  77. {
  78. LLVector4a binormal;
  79. F32 d = normal[0];
  80. if (d <= -0.5f)
  81. {
  82. binormal.set(0.f, -1.f, 0.f);
  83. }
  84. else if (d >= 0.5f)
  85. {
  86. binormal.set(0.f, 1.f, 0.f);
  87. }
  88. else if (normal[1] > 0.f)
  89. {
  90. binormal.set(-1.f, 0.f, 0.f);
  91. }
  92. else
  93. {
  94. binormal.set(1.f, 0.f, 0.f);
  95. }
  96. LLVector4a tangent;
  97. tangent.setCross3(binormal, normal);
  98. tc.mV[1] = -2.f * tangent.dot3(vec).getF32() + 0.5f;
  99. tc.mV[0] = 2.f * binormal.dot3(vec).getF32() + 0.5f;
  100. }
  101. void LLFace::init(LLDrawable* drawablep, LLViewerObject* objp)
  102. {
  103. mLastUpdateTime = gFrameTimeSeconds;
  104. mLastMoveTime = 0.f;
  105. mLastSkinTime = gFrameTimeSeconds;
  106. mVSize = 0.f;
  107. mPixelArea = 16.f;
  108. mState = GLOBAL;
  109. mDrawOrderIndex = 0;
  110. mDrawPoolp = NULL;
  111. mPoolType = 0;
  112. mCenterLocal = objp->getPosition();
  113. mCenterAgent = drawablep->getPositionAgent();
  114. mDistance = 0.f;
  115. mGeomCount = 0;
  116. mGeomIndex = 0;
  117. mIndicesCount = 0;
  118. // Special value to indicate uninitialized position
  119. mIndicesIndex = 0xFFFFFFFF;
  120. for (U32 i = 0; i < LLRender::NUM_TEXTURE_CHANNELS; ++i)
  121. {
  122. mIndexInTex[i] = 0;
  123. mTexture[i] = NULL;
  124. }
  125. mTEOffset = -1;
  126. mTextureIndex = FACE_DO_NOT_BATCH_TEXTURES;
  127. setDrawable(drawablep);
  128. mVObjp = objp;
  129. mReferenceIndex = -1;
  130. mTextureMatrix = NULL;
  131. mDrawInfo = NULL;
  132. mAvatar = NULL;
  133. mFaceColor = LLColor4(1.f, 0.f, 0.f, 1.f);
  134. mImportanceToCamera = 0.f;
  135. mBoundingSphereRadius = 0.f;
  136. mHasMedia = false;
  137. mIsMediaAllowed = true;
  138. }
  139. void LLFace::destroy()
  140. {
  141. #if LL_DEBUG
  142. if (gDebugGL)
  143. {
  144. gPipeline.checkReferences(this);
  145. }
  146. #endif
  147. for (U32 i = 0; i < LLRender::NUM_TEXTURE_CHANNELS; ++i)
  148. {
  149. if (mTexture[i].notNull())
  150. {
  151. mTexture[i]->removeFace(i, this);
  152. }
  153. }
  154. if (mDrawPoolp)
  155. {
  156. mDrawPoolp->removeFace(this);
  157. mDrawPoolp = NULL;
  158. }
  159. if (mTextureMatrix)
  160. {
  161. delete mTextureMatrix;
  162. mTextureMatrix = NULL;
  163. if (mDrawablep.notNull())
  164. {
  165. LLSpatialGroup* group = mDrawablep->getSpatialGroup();
  166. if (group)
  167. {
  168. group->dirtyGeom();
  169. gPipeline.markRebuild(group);
  170. }
  171. }
  172. }
  173. mDrawInfo = NULL;
  174. mDrawablep = NULL;
  175. mVObjp = NULL;
  176. }
  177. void LLFace::setPool(LLFacePool* poolp, LLViewerTexture* texp)
  178. {
  179. if (!poolp)
  180. {
  181. llerrs << "Setting pool to null !" << llendl;
  182. }
  183. if (poolp != mDrawPoolp)
  184. {
  185. // Remove from old pool
  186. if (mDrawPoolp)
  187. {
  188. mDrawPoolp->removeFace(this);
  189. if (mDrawablep)
  190. {
  191. gPipeline.markRebuild(mDrawablep);
  192. }
  193. }
  194. mGeomIndex = 0;
  195. // Add to new pool
  196. if (poolp)
  197. {
  198. poolp->addFace(this);
  199. }
  200. mDrawPoolp = poolp;
  201. }
  202. setDiffuseMap(texp);
  203. }
  204. void LLFace::setTexture(U32 ch, LLViewerTexture* texp)
  205. {
  206. llassert(ch < LLRender::NUM_TEXTURE_CHANNELS);
  207. if (mTexture[ch] == texp)
  208. {
  209. return;
  210. }
  211. if (mTexture[ch].notNull())
  212. {
  213. mTexture[ch]->removeFace(ch, this);
  214. }
  215. if (texp)
  216. {
  217. texp->addFace(ch, this);
  218. }
  219. mTexture[ch] = texp;
  220. }
  221. void LLFace::dirtyTexture()
  222. {
  223. LLDrawable* drawablep = getDrawable();
  224. if (!drawablep)
  225. {
  226. return;
  227. }
  228. if (mVObjp.notNull() && mVObjp->getVolume())
  229. {
  230. LLVOVolume* vobj = drawablep->getVOVolume();
  231. bool mark_rebuild = false;
  232. bool update_complexity = false;
  233. for (U32 ch = 0; ch < LLRender::NUM_TEXTURE_CHANNELS; ++ch)
  234. {
  235. if (mTexture[ch].notNull() && mTexture[ch]->getComponents() == 4)
  236. {
  237. mark_rebuild = true;
  238. // Dirty texture on an alpha object should be treated as an LoD
  239. // update
  240. if (vobj)
  241. {
  242. vobj->mLODChanged = true;
  243. // If vobj is an avatar, its render complexity may have
  244. // changed
  245. update_complexity = true;
  246. }
  247. }
  248. }
  249. if (mark_rebuild)
  250. {
  251. gPipeline.markRebuild(drawablep, LLDrawable::REBUILD_VOLUME);
  252. }
  253. if (update_complexity)
  254. {
  255. vobj->updateVisualComplexity();
  256. }
  257. }
  258. gPipeline.markTextured(drawablep);
  259. }
  260. #if LL_FIX_MAT_TRANSPARENCY
  261. void LLFace::notifyAboutCreatingTexture(LLViewerTexture* texp)
  262. {
  263. LLDrawable* drawablep = getDrawable();
  264. if (drawablep && mVObjp.notNull() && mVObjp->getVolume())
  265. {
  266. LLVOVolume* vobj = drawablep->getVOVolume();
  267. if (vobj && vobj->notifyAboutCreatingTexture(texp))
  268. {
  269. gPipeline.markTextured(drawablep);
  270. gPipeline.markRebuild(drawablep, LLDrawable::REBUILD_VOLUME);
  271. }
  272. }
  273. }
  274. void LLFace::notifyAboutMissingAsset(LLViewerTexture* texp)
  275. {
  276. LLDrawable* drawablep = getDrawable();
  277. if (drawablep && mVObjp.notNull() && mVObjp->getVolume())
  278. {
  279. LLVOVolume* vobj = drawablep->getVOVolume();
  280. if (vobj && vobj->notifyAboutMissingAsset(texp))
  281. {
  282. gPipeline.markTextured(drawablep);
  283. gPipeline.markRebuild(drawablep, LLDrawable::REBUILD_VOLUME);
  284. }
  285. }
  286. }
  287. #endif
  288. void LLFace::switchTexture(U32 ch, LLViewerTexture* texp)
  289. {
  290. llassert(ch < LLRender::NUM_TEXTURE_CHANNELS);
  291. if (mTexture[ch] == texp)
  292. {
  293. return;
  294. }
  295. if (!texp)
  296. {
  297. llerrs << "Cannot switch to a null texture." << llendl;
  298. return;
  299. }
  300. if (mTexture[ch].notNull())
  301. {
  302. texp->addTextureStats(mTexture[ch]->getMaxVirtualSize());
  303. }
  304. if (ch == LLRender::DIFFUSE_MAP)
  305. {
  306. LLViewerObject* objp = getViewerObject();
  307. if (objp)
  308. {
  309. objp->changeTEImage(mTEOffset, texp);
  310. }
  311. }
  312. setTexture(ch, texp);
  313. dirtyTexture();
  314. }
  315. void LLFace::switchDiffuseTex(const LLUUID& tex_id)
  316. {
  317. LLPointer<LLViewerTexture>& diff_texp = mTexture[LLRender::DIFFUSE_MAP];
  318. if (diff_texp.notNull() && diff_texp->getID() == tex_id)
  319. {
  320. return;
  321. }
  322. // Make sure the texture will be fetched if not yet in memory. HB
  323. LLPointer<LLViewerFetchedTexture> texp =
  324. LLViewerTextureManager::getFetchedTexture(tex_id, FTT_DEFAULT, true,
  325. LLGLTexture::BOOST_NONE,
  326. LLViewerTexture::LOD_TEXTURE);
  327. if (diff_texp.notNull())
  328. {
  329. texp->addTextureStats(diff_texp->getMaxVirtualSize());
  330. }
  331. else
  332. {
  333. texp->addTextureStats(256.f * 256.f);
  334. }
  335. LLViewerObject* objp = getViewerObject();
  336. if (objp)
  337. {
  338. objp->changeTEImage(mTEOffset, texp);
  339. }
  340. setTexture(LLRender::DIFFUSE_MAP, texp);
  341. dirtyTexture();
  342. }
  343. void LLFace::setDrawable(LLDrawable* drawablep)
  344. {
  345. mDrawablep = drawablep;
  346. mXform = &drawablep->mXform;
  347. }
  348. void LLFace::setSize(U32 num_vertices, U32 num_indices, bool align)
  349. {
  350. if (align)
  351. {
  352. // Allocate vertices in blocks of 4 for alignment
  353. num_vertices = (num_vertices + 0x3) & ~0x3;
  354. }
  355. if (mGeomCount != num_vertices || mIndicesCount != num_indices)
  356. {
  357. mGeomCount = num_vertices;
  358. mIndicesCount = num_indices;
  359. mVertexBuffer = NULL;
  360. }
  361. llassert(verify());
  362. }
  363. void LLFace::setGeomIndex(U16 idx)
  364. {
  365. if (mGeomIndex != idx)
  366. {
  367. mGeomIndex = idx;
  368. mVertexBuffer = NULL;
  369. }
  370. }
  371. void LLFace::setTextureIndex(U8 index)
  372. {
  373. if (index != mTextureIndex)
  374. {
  375. mTextureIndex = index;
  376. if (mTextureIndex != FACE_DO_NOT_BATCH_TEXTURES)
  377. {
  378. mDrawablep->setState(LLDrawable::REBUILD_POSITION);
  379. }
  380. else if (mDrawInfo && !mDrawInfo->mTextureList.empty())
  381. {
  382. llwarns << "Face " << std::hex << (intptr_t)this << std::dec
  383. << " with no texture index references indexed texture draw info."
  384. << llendl;
  385. }
  386. }
  387. }
  388. void LLFace::setIndicesIndex(U32 idx)
  389. {
  390. if (mIndicesIndex != idx)
  391. {
  392. mIndicesIndex = idx;
  393. mVertexBuffer = NULL;
  394. }
  395. }
  396. U16 LLFace::getGeometryAvatar(LLStrider<LLVector3>& vertices,
  397. LLStrider<LLVector3>& normals,
  398. LLStrider<LLVector2>& tex_coords,
  399. LLStrider<F32>& vertex_weights,
  400. LLStrider<LLVector4a>& clothing_weights)
  401. {
  402. if (mVertexBuffer.notNull())
  403. {
  404. mVertexBuffer->getVertexStrider(vertices, mGeomIndex, mGeomCount);
  405. mVertexBuffer->getNormalStrider(normals, mGeomIndex, mGeomCount);
  406. mVertexBuffer->getTexCoord0Strider(tex_coords, mGeomIndex, mGeomCount);
  407. mVertexBuffer->getWeightStrider(vertex_weights, mGeomIndex,
  408. mGeomCount);
  409. mVertexBuffer->getClothWeightStrider(clothing_weights, mGeomIndex,
  410. mGeomCount);
  411. }
  412. return mGeomIndex;
  413. }
  414. U16 LLFace::getGeometry(LLStrider<LLVector3>& vertices,
  415. LLStrider<LLVector3>& normals,
  416. LLStrider<LLVector2>& tex_coords,
  417. LLStrider<U16> &indicesp)
  418. {
  419. if (mVertexBuffer.notNull())
  420. {
  421. mVertexBuffer->getVertexStrider(vertices, mGeomIndex, mGeomCount);
  422. if (mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_NORMAL))
  423. {
  424. mVertexBuffer->getNormalStrider(normals, mGeomIndex, mGeomCount);
  425. }
  426. if (mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_TEXCOORD0))
  427. {
  428. mVertexBuffer->getTexCoord0Strider(tex_coords, mGeomIndex,
  429. mGeomCount);
  430. }
  431. mVertexBuffer->getIndexStrider(indicesp, mIndicesIndex, mIndicesCount);
  432. }
  433. return mGeomIndex;
  434. }
  435. void LLFace::updateCenterAgent()
  436. {
  437. if (mDrawablep->isActive())
  438. {
  439. mCenterAgent = mCenterLocal * getRenderMatrix();
  440. }
  441. else
  442. {
  443. mCenterAgent = mCenterLocal;
  444. }
  445. }
  446. void LLFace::renderSelected(LLViewerTexture* imagep, const LLColor4& color)
  447. {
  448. if (mDrawablep.isNull() || mDrawablep->getSpatialGroup() == NULL)
  449. {
  450. return;
  451. }
  452. mDrawablep->getSpatialGroup()->rebuildGeom();
  453. mDrawablep->getSpatialGroup()->rebuildMesh();
  454. if (!mGeomCount || !mIndicesCount || mDrawablep.isNull() ||
  455. mVertexBuffer.isNull())
  456. {
  457. return;
  458. }
  459. gGL.getTexUnit(0)->bind(imagep);
  460. gGL.pushMatrix();
  461. if (mDrawablep->isActive())
  462. {
  463. gGL.multMatrix(mDrawablep->getRenderMatrix().getF32ptr());
  464. }
  465. else
  466. {
  467. gGL.multMatrix(mDrawablep->getRegion()->mRenderMatrix.getF32ptr());
  468. }
  469. if (mDrawablep->isState(LLDrawable::RIGGED))
  470. {
  471. LLVOVolume* volumep = mDrawablep->getVOVolume();
  472. // For now, we have no selection outline for rigged meshes in PBR mode
  473. // (disabled too and marked as "TODO" in LL's PBR viewer). HB
  474. if (volumep && !gUsePBRShaders)
  475. {
  476. // BENTO: called when selecting a face during edit of a mesh object
  477. LLRiggedVolume* riggedp = volumep->getRiggedVolume();
  478. if (riggedp)
  479. {
  480. LLGLEnable offset(GL_POLYGON_OFFSET_FILL);
  481. glPolygonOffset(-1.f, -1.f);
  482. gGL.multMatrix(volumep->getRelativeXform().getF32ptr());
  483. const LLVolumeFace& vol_face =
  484. riggedp->getVolumeFace(getTEOffset());
  485. LLVertexBuffer::unbind();
  486. glVertexPointer(3, GL_FLOAT, 16, vol_face.mPositions);
  487. if (vol_face.mTexCoords)
  488. {
  489. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  490. glTexCoordPointer(2, GL_FLOAT, 8, vol_face.mTexCoords);
  491. }
  492. gGL.syncMatrices();
  493. glDrawElements(GL_TRIANGLES, vol_face.mNumIndices,
  494. GL_UNSIGNED_SHORT, vol_face.mIndices);
  495. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  496. }
  497. }
  498. }
  499. else if (gUsePBRShaders)
  500. {
  501. gGL.diffuseColor4fv(color.mV);
  502. mVertexBuffer->setBuffer();
  503. mVertexBuffer->draw(LLRender::TRIANGLES, mIndicesCount, mIndicesIndex);
  504. }
  505. else
  506. {
  507. gGL.diffuseColor4fv(color.mV);
  508. LLGLEnable poly_offset(GL_POLYGON_OFFSET_FILL);
  509. glPolygonOffset(-1.f, -1.f);
  510. // Disable per-vertex color to prevent fixed-function pipeline from
  511. // using it. We want glColor color, not vertex color !
  512. mVertexBuffer->setBuffer(mVertexBuffer->getTypeMask() &
  513. ~LLVertexBuffer::MAP_COLOR);
  514. mVertexBuffer->draw(LLRender::TRIANGLES, mIndicesCount, mIndicesIndex);
  515. }
  516. gGL.popMatrix();
  517. }
  518. void LLFace::printDebugInfo() const
  519. {
  520. LLFacePool* poolp = getPool();
  521. llinfos << "Object: " << getViewerObject()->mID << llendl;
  522. if (getDrawable().notNull())
  523. {
  524. llinfos << "Type: "
  525. << LLPrimitive::pCodeToString(getDrawable()->getVObj()->getPCode())
  526. << llendl;
  527. }
  528. if (getTexture())
  529. {
  530. llinfos << "Texture: " << getTexture() << " Comps: "
  531. << (U32)getTexture()->getComponents() << llendl;
  532. }
  533. else
  534. {
  535. llinfos << "No texture: " << llendl;
  536. }
  537. llinfos << "Face: " << this << llendl;
  538. llinfos << "State: " << getState() << llendl;
  539. llinfos << "Geom Index Data:" << llendl;
  540. llinfos << "--------------------" << llendl;
  541. llinfos << "GI: " << mGeomIndex << " Count:" << mGeomCount << llendl;
  542. llinfos << "Face Index Data:" << llendl;
  543. llinfos << "--------------------" << llendl;
  544. llinfos << "II: " << mIndicesIndex << " Count:" << mIndicesCount << llendl;
  545. llinfos << llendl;
  546. if (poolp)
  547. {
  548. poolp->printDebugInfo();
  549. S32 pool_references = 0;
  550. for (std::vector<LLFace*>::iterator iter = poolp->mReferences.begin();
  551. iter != poolp->mReferences.end(); iter++)
  552. {
  553. LLFace *facep = *iter;
  554. if (facep == this)
  555. {
  556. llinfos << "Pool reference: " << pool_references << llendl;
  557. pool_references++;
  558. }
  559. }
  560. if (pool_references != 1)
  561. {
  562. llinfos << "Incorrect number of pool references!" << llendl;
  563. }
  564. }
  565. #if 0
  566. llinfos << "Indices:" << llendl;
  567. llinfos << "--------------------" << llendl;
  568. const U32* indicesp = getRawIndices();
  569. U32 indices_count = getIndicesCount();
  570. U32 geom_start = getGeomStart();
  571. for (U32 i = 0; i < indices_count; ++i)
  572. {
  573. llinfos << i << ":" << indicesp[i] << ":"
  574. << indicesp[i] - geom_start << llendl;
  575. }
  576. llinfos << llendl;
  577. llinfos << "Vertices:" << llendl;
  578. llinfos << "--------------------" << llendl;
  579. for (U16 i = 0; i < mGeomCount; ++i)
  580. {
  581. llinfos << mGeomIndex + i << ":" << poolp->getVertex(mGeomIndex + i)
  582. << llendl;
  583. }
  584. llinfos << llendl;
  585. #endif
  586. }
  587. // Transform the texture coordinates for this face.
  588. static void xform(LLVector2& tex_coord, F32 cos_ang, F32 sin_ang, F32 off_s,
  589. F32 off_t, F32 mag_s, F32 mag_t)
  590. {
  591. // Texture transforms are done about the center of the face.
  592. F32 s = tex_coord.mV[0] - 0.5f;
  593. F32 t = tex_coord.mV[1] - 0.5f;
  594. // Handle rotation
  595. F32 temp = s;
  596. s = s * cos_ang + t * sin_ang;
  597. t = -temp * sin_ang + t * cos_ang;
  598. // Then scale
  599. s *= mag_s;
  600. t *= mag_t;
  601. // Then offset
  602. s += off_s + 0.5f;
  603. t += off_t + 0.5f;
  604. tex_coord.mV[0] = s;
  605. tex_coord.mV[1] = t;
  606. }
  607. // Transform the texture coordinates for this face.
  608. static void xform4a(LLVector4a& tex_coord, const LLVector4a& trans,
  609. const LLVector4Logical& mask, const LLVector4a& rot0,
  610. const LLVector4a& rot1, const LLVector4a& offset,
  611. const LLVector4a& scale)
  612. {
  613. // Tex coord is two coords, <s0, t0, s1, t1>
  614. LLVector4a st;
  615. // Texture transforms are done about the center of the face.
  616. st.setAdd(tex_coord, trans);
  617. // Handle rotation
  618. LLVector4a rot_st;
  619. // <s0 * cos_ang, s0*-sin_ang, s1*cos_ang, s1*-sin_ang>
  620. LLVector4a s0;
  621. s0.splat(st, 0.f);
  622. LLVector4a s1;
  623. s1.splat(st, 2.f);
  624. LLVector4a ss;
  625. ss.setSelectWithMask(mask, s1, s0);
  626. LLVector4a a;
  627. a.setMul(rot0, ss);
  628. // <t0*sin_ang, t0*cos_ang, t1*sin_ang, t1*cos_ang>
  629. LLVector4a t0;
  630. t0.splat(st, 1.f);
  631. LLVector4a t1;
  632. t1.splat(st, 3.f);
  633. LLVector4a tt;
  634. tt.setSelectWithMask(mask, t1, t0);
  635. LLVector4a b;
  636. b.setMul(rot1, tt);
  637. st.setAdd(a, b);
  638. // Then scale
  639. st.mul(scale);
  640. // Then offset
  641. tex_coord.setAdd(st, offset);
  642. }
  643. #if LL_HAS_ASSERT
  644. // Defined in llspatialpartition.cpp
  645. extern LLVector4a gOctreeMaxMag;
  646. bool less_than_max_mag(const LLVector4a& vec)
  647. {
  648. LLVector4a val;
  649. val.setAbs(vec);
  650. return (val.lessThan(gOctreeMaxMag).getGatheredBits() & 0x7) == 0x7;
  651. }
  652. #endif
  653. bool LLFace::genVolumeBBoxes(const LLVolume& volume, S32 f,
  654. const LLMatrix4& mat_vert_in,
  655. bool global_volume)
  656. {
  657. // Get the bounding box
  658. if (mDrawablep->isState(LLDrawable::REBUILD_VOLUME |
  659. LLDrawable::REBUILD_POSITION |
  660. LLDrawable::REBUILD_RIGGED))
  661. {
  662. if (f >= volume.getNumVolumeFaces())
  663. {
  664. llwarns << "Attempt to generate bounding box for invalid face index !"
  665. << llendl;
  666. return false;
  667. }
  668. const LLVolumeFace& face = volume.getVolumeFace(f);
  669. #if 0 // Disabled: it causes rigged meshes not to rez at all ! HB
  670. // MAINT-8264: stray vertices, especially in low LODs, cause bounding
  671. // box errors.
  672. if (face.mNumVertices < 3)
  673. {
  674. return false;
  675. }
  676. #endif
  677. llassert(less_than_max_mag(face.mExtents[0]));
  678. llassert(less_than_max_mag(face.mExtents[1]));
  679. // VECTORIZE THIS
  680. LLMatrix4a mat_vert;
  681. mat_vert.loadu(mat_vert_in);
  682. mat_vert.matMulBoundBox(face.mExtents, mExtents);
  683. LLVector4a& new_min = mExtents[0];
  684. LLVector4a& new_max = mExtents[1];
  685. if (!mDrawablep->isActive())
  686. {
  687. // Shift position for region
  688. LLVector4a offset;
  689. offset.load3(mDrawablep->getRegion()->getOriginAgent().mV);
  690. new_min.add(offset);
  691. new_max.add(offset);
  692. }
  693. LLVector4a t;
  694. t.setAdd(new_min, new_max);
  695. t.mul(0.5f);
  696. mCenterLocal.set(t.getF32ptr());
  697. t.setSub(new_max, new_min);
  698. mBoundingSphereRadius = t.getLength3().getF32() * 0.5f;
  699. updateCenterAgent();
  700. }
  701. return true;
  702. }
  703. // Converts surface coordinates to texture coordinates, based on the values in
  704. // the texture entry.
  705. // *TODO: VECTORIZE THIS
  706. LLVector2 LLFace::surfaceToTexture(LLVector2 surface_coord,
  707. const LLVector4a& position,
  708. const LLVector4a& normal)
  709. {
  710. const LLTextureEntry* tep = getTextureEntry();
  711. if (!tep)
  712. {
  713. // Cannot do much without the texture entry
  714. return surface_coord;
  715. }
  716. LLVector2 tc = surface_coord;
  717. // See if we have a non-default mapping
  718. if (tep->getTexGen() == LLTextureEntry::TEX_GEN_PLANAR)
  719. {
  720. LLVOVolume* volp = mDrawablep->getVOVolume();
  721. if (!volp) // Paranoia
  722. {
  723. return surface_coord;
  724. }
  725. LLVector4a volume_position;
  726. LLVector3 v_position(position.getF32ptr());
  727. volume_position.load3(volp->agentPositionToVolume(v_position).mV);
  728. if (!volp->isVolumeGlobal())
  729. {
  730. LLVector4a scale;
  731. scale.load3(mVObjp->getScale().mV);
  732. volume_position.mul(scale);
  733. }
  734. LLVector4a& c = *(volp->getVolume()->getVolumeFace(mTEOffset).mCenter);
  735. LLVector4a volume_normal;
  736. LLVector3 v_normal(normal.getF32ptr());
  737. volume_normal.load3(volp->agentDirectionToVolume(v_normal).mV);
  738. volume_normal.normalize3fast();
  739. planarProjection(tc, volume_normal, c, volume_position);
  740. }
  741. if (mTextureMatrix) // If we have a texture matrix, use it
  742. {
  743. return LLVector2(LLVector3(tc) * *mTextureMatrix);
  744. }
  745. // Otherwise use the texture entry parameters
  746. xform(tc, cosf(tep->getRotation()), sinf(tep->getRotation()),
  747. tep->getOffsetS(), tep->getOffsetT(), tep->getScaleS(),
  748. tep->getScaleT());
  749. return tc;
  750. }
  751. // Returns scale compared to default texgen, and face orientation as calculated
  752. // by planarProjection(). This is needed to match planar texgen parameters.
  753. void LLFace::getPlanarProjectedParams(LLQuaternion* face_rot,
  754. LLVector3* face_pos, F32* scale) const
  755. {
  756. LLViewerObject* objp = getViewerObject();
  757. if (!objp) return;
  758. const LLVolumeFace& vf = objp->getVolume()->getVolumeFace(mTEOffset);
  759. if (!vf.mNormals || !vf.mTangents) return;
  760. const LLVector4a& normal4a = vf.mNormals[0];
  761. const LLVector4a& tangent = vf.mTangents[0];
  762. LLVector4a binormal4a;
  763. binormal4a.setCross3(normal4a, tangent);
  764. binormal4a.mul(tangent.getF32ptr()[3]);
  765. LLVector2 projected_binormal;
  766. planarProjection(projected_binormal, normal4a, *vf.mCenter, binormal4a);
  767. // This normally happens in xform():
  768. projected_binormal -= LLVector2(0.5f, 0.5f);
  769. *scale = projected_binormal.length();
  770. // Rotate binormal to match what planarProjection() thinks it is, then find
  771. // rotation from that:
  772. projected_binormal.normalize();
  773. F32 ang = acosf(projected_binormal.mV[VY]);
  774. if (projected_binormal.mV[VX] < 0.f)
  775. {
  776. ang = -ang;
  777. }
  778. // VECTORIZE THIS
  779. LLVector3 binormal(binormal4a.getF32ptr());
  780. LLVector3 normal(normal4a.getF32ptr());
  781. binormal.rotVec(ang, normal);
  782. LLQuaternion local_rot(binormal % normal, binormal, normal);
  783. const LLMatrix4& vol_mat = getWorldMatrix();
  784. *face_rot = local_rot * vol_mat.quaternion();
  785. *face_pos = vol_mat.getTranslation();
  786. }
  787. // Returns the necessary texture transform to align this face's TE to align_to's TE
  788. bool LLFace::calcAlignedPlanarTE(const LLFace* align_to, LLVector2* res_st_offset,
  789. LLVector2* res_st_scale, F32* res_st_rot,
  790. S32 map) const
  791. {
  792. if (!align_to)
  793. {
  794. return false;
  795. }
  796. const LLTextureEntry* orig_tep = align_to->getTextureEntry();
  797. if (!orig_tep || orig_tep->getTexGen() != LLTextureEntry::TEX_GEN_PLANAR ||
  798. getTextureEntry()->getTexGen() != LLTextureEntry::TEX_GEN_PLANAR)
  799. {
  800. return false;
  801. }
  802. LLMaterial* matp = orig_tep->getMaterialParams();
  803. if (!matp && map != LLRender::DIFFUSE_MAP)
  804. {
  805. llwarns_once << "Face " << std::hex << (intptr_t)this << std::dec
  806. << " is set to use specular or normal map but has no material, defaulting to diffuse"
  807. << llendl;
  808. map = LLRender::DIFFUSE_MAP;
  809. }
  810. F32 map_rot = 0.f;
  811. F32 map_scl_s = 0.f;
  812. F32 map_scl_t = 0.f;
  813. F32 map_off_s = 0.f;
  814. F32 map_off_t = 0.f;
  815. switch (map)
  816. {
  817. case LLRender::DIFFUSE_MAP:
  818. map_rot = orig_tep->getRotation();
  819. map_scl_s = orig_tep->getScaleS();
  820. map_scl_t = orig_tep->getScaleT();
  821. map_off_s = orig_tep->getOffsetS();
  822. map_off_t = orig_tep->getOffsetT();
  823. break;
  824. case LLRender::NORMAL_MAP:
  825. if (matp->getNormalID().isNull())
  826. {
  827. return false;
  828. }
  829. map_rot = matp->getNormalRotation();
  830. map_scl_s = matp->getNormalRepeatX();
  831. map_scl_t = matp->getNormalRepeatY();
  832. map_off_s = matp->getNormalOffsetX();
  833. map_off_t = matp->getNormalOffsetY();
  834. break;
  835. case LLRender::SPECULAR_MAP:
  836. if (matp->getSpecularID().isNull())
  837. {
  838. return false;
  839. }
  840. map_rot = matp->getSpecularRotation();
  841. map_scl_s = matp->getSpecularRepeatX();
  842. map_scl_t = matp->getSpecularRepeatY();
  843. map_off_s = matp->getSpecularOffsetX();
  844. map_off_t = matp->getSpecularOffsetY();
  845. break;
  846. default:
  847. return false;
  848. }
  849. LLVector3 orig_pos, this_pos;
  850. LLQuaternion orig_face_rot, this_face_rot;
  851. F32 orig_proj_scale, this_proj_scale;
  852. align_to->getPlanarProjectedParams(&orig_face_rot, &orig_pos,
  853. &orig_proj_scale);
  854. getPlanarProjectedParams(&this_face_rot, &this_pos, &this_proj_scale);
  855. // The rotation of "this face's" texture:
  856. LLQuaternion orig_st_rot = LLQuaternion(map_rot, LLVector3::z_axis) *
  857. orig_face_rot;
  858. LLQuaternion this_st_rot = orig_st_rot * ~this_face_rot;
  859. F32 x_ang, y_ang, z_ang;
  860. this_st_rot.getEulerAngles(&x_ang, &y_ang, &z_ang);
  861. *res_st_rot = z_ang;
  862. // Offset and scale of "this face's" texture:
  863. LLVector3 centers_dist = (this_pos - orig_pos) * ~orig_st_rot;
  864. LLVector3 st_scale(map_scl_s, map_scl_t, 1.f);
  865. st_scale *= orig_proj_scale;
  866. centers_dist.scaleVec(st_scale);
  867. LLVector2 orig_st_offset(map_off_s, map_off_t);
  868. *res_st_offset = orig_st_offset + (LLVector2)centers_dist;
  869. res_st_offset->mV[VX] -= (S32)res_st_offset->mV[VX];
  870. res_st_offset->mV[VY] -= (S32)res_st_offset->mV[VY];
  871. st_scale /= this_proj_scale;
  872. *res_st_scale = (LLVector2)st_scale;
  873. return true;
  874. }
  875. void LLFace::updateRebuildFlags()
  876. {
  877. if (mDrawablep->isState(LLDrawable::REBUILD_VOLUME))
  878. {
  879. // This rebuild is zero overhead (direct consequence of some change
  880. // that affects this face)
  881. mLastUpdateTime = gFrameTimeSeconds;
  882. }
  883. else
  884. {
  885. // This rebuild is overhead (side effect of some change that does not
  886. // affect this face)
  887. mLastMoveTime = gFrameTimeSeconds;
  888. }
  889. }
  890. bool LLFace::canRenderAsMask()
  891. {
  892. if (isState(LLFace::RIGGED))
  893. {
  894. // Never auto alpha-mask rigged faces
  895. return false;
  896. }
  897. const LLTextureEntry* tep = getTextureEntry();
  898. if (!tep || !getViewerObject() || !getTexture())
  899. {
  900. return false;
  901. }
  902. if (gUsePBRShaders && tep->getGLTFRenderMaterial())
  903. {
  904. return false;
  905. }
  906. LLMaterial* matp = tep->getMaterialParams();
  907. if (matp &&
  908. matp->getDiffuseAlphaMode() == LLMaterial::DIFFUSE_ALPHA_MODE_BLEND)
  909. {
  910. return false;
  911. }
  912. const LLVolume* volp = getViewerObject()->getVolumeConst();
  913. if (!volp)
  914. {
  915. return false;
  916. }
  917. // Cannot treat as mask if face is part of a flexible object
  918. if (!volp->isUnique() &&
  919. // Cannot treat as mask if we have face alpha
  920. tep->getColor().mV[3] == 1.f &&
  921. // Glowing masks are hard to implement; do not mask
  922. !tep->hasGlow() &&
  923. // HUD attachments are NOT maskable (else they would get affected by
  924. // day light)
  925. !getViewerObject()->isHUDAttachment() &&
  926. // Texture actually qualifies for masking (lazily recalculated but
  927. // expensive)
  928. getTexture()->getIsAlphaMask())
  929. {
  930. // Fullbright objects are NOT subject to the deferred rendering
  931. if (LLPipeline::sRenderDeferred && !tep->getFullbright())
  932. {
  933. return LLPipeline::sAutoMaskAlphaDeferred;
  934. }
  935. return LLPipeline::sAutoMaskAlphaNonDeferred;
  936. }
  937. return false;
  938. }
  939. bool LLFace::getGeometryVolume(const LLVolume& volume, S32 f,
  940. const LLMatrix4& mat_vert_in,
  941. const LLMatrix3& mat_norm_in,
  942. const U16& index_offset, bool force_rebuild)
  943. {
  944. LL_FAST_TIMER(FTM_FACE_GET_GEOM);
  945. llassert(verify());
  946. if (f < 0 || f >= volume.getNumVolumeFaces())
  947. {
  948. llwarns << "Attempt to get a non-existent volume face: "
  949. << volume.getNumVolumeFaces()
  950. << " total faces and requested face index = " << f << llendl;
  951. return false;
  952. }
  953. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_OCTREE))
  954. {
  955. updateRebuildFlags();
  956. }
  957. if (mDrawablep.isNull())
  958. {
  959. llwarns << "NULL drawable !" << llendl;
  960. return false;
  961. }
  962. if (!mDrawablep->getVOVolume())
  963. {
  964. llwarns << "NULL volume !" << llendl;
  965. return false;
  966. }
  967. if (mVObjp.isNull())
  968. {
  969. llwarns << "NULL viewer object !" << llendl;
  970. return false;
  971. }
  972. if (!mVObjp->getVolume())
  973. {
  974. llwarns << "NULL viewer object volume !" << llendl;
  975. return false;
  976. }
  977. if (mVertexBuffer.isNull())
  978. {
  979. llwarns << "NULL vertex buffer !" << llendl;
  980. return false;
  981. }
  982. bool rigged = isState(RIGGED);
  983. const LLVolumeFace& vf = volume.getVolumeFace(f);
  984. U32 num_vertices = llclamp(vf.mNumVertices, 0, mGeomCount);
  985. U32 num_indices = llclamp(vf.mNumIndices, 0, mIndicesCount);
  986. if (num_indices + mIndicesIndex > mVertexBuffer->getNumIndices())
  987. {
  988. if (gDebugGL)
  989. {
  990. llwarns << "Index buffer overflow ! Indices Count: "
  991. << mIndicesCount << " - VF Num Indices: " << num_indices
  992. << " - Indices Index: " << mIndicesIndex
  993. << " - VB Num Indices: " << mVertexBuffer->getNumIndices()
  994. << " - Face Index: " << f << " - Pool Type: " << mPoolType
  995. << llendl;
  996. }
  997. return false;
  998. }
  999. if (num_vertices + mGeomIndex > mVertexBuffer->getNumVerts())
  1000. {
  1001. if (gDebugGL)
  1002. {
  1003. llwarns << "Vertex buffer overflow !" << llendl;
  1004. }
  1005. return false;
  1006. }
  1007. if (!vf.mTexCoords || !vf.mNormals || !vf.mPositions)
  1008. {
  1009. llwarns_sparse << "vf got NULL pointer(s) !" << llendl;
  1010. return false;
  1011. }
  1012. LLStrider<LLVector3> vert, norm, tangent;
  1013. LLStrider<LLVector2> tex_coords0, tex_coords1, tex_coords2;
  1014. LLStrider<LLColor4U> colors;
  1015. LLStrider<U16> indicesp;
  1016. LLStrider<LLVector4a> wght;
  1017. bool full_rebuild = force_rebuild ||
  1018. mDrawablep->isState(LLDrawable::REBUILD_VOLUME);
  1019. LLVector3 scale;
  1020. if (mDrawablep->getVOVolume()->isVolumeGlobal())
  1021. {
  1022. scale.set(1.f, 1.f, 1.f);
  1023. }
  1024. else
  1025. {
  1026. scale = mVObjp->getScale();
  1027. }
  1028. bool rebuild_pos = full_rebuild ||
  1029. mDrawablep->isState(LLDrawable::REBUILD_POSITION);
  1030. bool rebuild_color = full_rebuild ||
  1031. mDrawablep->isState(LLDrawable::REBUILD_COLOR);
  1032. bool rebuild_emissive =
  1033. rebuild_color &&
  1034. mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_EMISSIVE);
  1035. bool rebuild_tcoord = full_rebuild ||
  1036. mDrawablep->isState(LLDrawable::REBUILD_TCOORD);
  1037. bool rebuild_normal =
  1038. rebuild_pos && mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_NORMAL);
  1039. bool rebuild_tangent =
  1040. rebuild_pos &&
  1041. mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_TANGENT);
  1042. bool rebuild_weights =
  1043. rebuild_pos &&
  1044. mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_WEIGHT4);
  1045. //MK
  1046. bool rlv_cam_textures = gRLenabled && gRLInterface.mContainsCamTextures &&
  1047. getViewerObject() &&
  1048. !getViewerObject()->isAttachment();
  1049. //mk
  1050. const LLTextureEntry* tep = mVObjp->getTE(f);
  1051. U8 bump_code = 0;
  1052. LLMaterial* matp = NULL;
  1053. LLFetchedGLTFMaterial* gltfp = NULL;
  1054. LLColor4U color;
  1055. // Default values for rotation, offsets and scales.
  1056. F32 r = 0.f, os = 0.f, ot = 0.f, ms = 1.f, mt = 1.f;
  1057. if (tep)
  1058. {
  1059. bump_code = tep->getBumpmap();
  1060. matp = tep->getMaterialParams().get();
  1061. LLGLTFMaterial* rmatp = tep->getGLTFRenderMaterial();
  1062. gltfp = rmatp ? rmatp->asFetched() : NULL;
  1063. //MK
  1064. if (rlv_cam_textures)
  1065. {
  1066. matp = NULL;
  1067. gltfp = NULL;
  1068. }
  1069. //mk
  1070. if (rebuild_tcoord)
  1071. {
  1072. // Are we overriding the diffuse texture with a non-blank base
  1073. // color texture ? HB
  1074. if (gltfp && !matp && !gUsePBRShaders && isState(USE_FACE_COLOR) &&
  1075. gltfp->getBaseColorId() != IMG_BLANK)
  1076. {
  1077. // When we do override the diffuse texture with the GLTF base
  1078. // color texture, we need to use its own transforms. HB
  1079. r = gltfp->getBaseColorRotation();
  1080. const LLVector2& offset = gltfp->getBaseColorOffset();
  1081. os = offset.mV[0];
  1082. ot = offset.mV[1];
  1083. const LLVector2& scale = gltfp->getBaseColorScale();
  1084. ms = scale.mV[0];
  1085. mt = scale.mV[1];
  1086. }
  1087. // Scales, offsets and rotation irrelevant for a blank texture, so
  1088. // do not even bother to try and use them (which, when non-default,
  1089. // could cause useless xform transformations below), and use the
  1090. // default ones (scales at 1.0, offsets and rotation at 0.0). HB
  1091. else if (!tep->isBlank())
  1092. {
  1093. r = tep->getRotation();
  1094. os = tep->getOffsetS();
  1095. ot = tep->getOffsetT();
  1096. ms = tep->getScaleS();
  1097. mt = tep->getScaleT();
  1098. }
  1099. }
  1100. if (!gUsePBRShaders)
  1101. {
  1102. gltfp = NULL; // Do not use the GLTF material in non-PBR mode.
  1103. }
  1104. if (gltfp)
  1105. {
  1106. color = LLColor4U(gltfp->mBaseColor);
  1107. }
  1108. else
  1109. {
  1110. color = LLColor4U(getRenderColor());
  1111. }
  1112. }
  1113. else
  1114. {
  1115. color = LLColor4U::white;
  1116. rebuild_color = false; // Cannot get color when tep is NULL
  1117. }
  1118. //MK
  1119. if (gRLenabled && gRLInterface.mContainsCamTextures &&
  1120. getViewerObject() && !getViewerObject()->isAttachment())
  1121. {
  1122. color = LLColor4::white;
  1123. }
  1124. //mk
  1125. if (mDrawablep->isStatic())
  1126. {
  1127. setState(GLOBAL);
  1128. }
  1129. else
  1130. {
  1131. clearState(GLOBAL);
  1132. }
  1133. if (rebuild_color) // false if tep == NULL
  1134. {
  1135. // Decide if shiny goes in alpha channel of color
  1136. // Alpha channel MUST contain transparency, not shiny:
  1137. if (!isInAlphaPool() && !gltfp)
  1138. {
  1139. bool shiny_in_alpha = false;
  1140. if (LLPipeline::sRenderDeferred)
  1141. {
  1142. // Store shiny in alpha if we do not have a specular map
  1143. if (!matp || matp->getSpecularID().isNull())
  1144. {
  1145. shiny_in_alpha = true;
  1146. }
  1147. }
  1148. else if (!matp ||
  1149. matp->getDiffuseAlphaMode() !=
  1150. LLMaterial::DIFFUSE_ALPHA_MODE_MASK)
  1151. {
  1152. shiny_in_alpha = true;
  1153. }
  1154. if (shiny_in_alpha)
  1155. {
  1156. static const LLColor4U shine_steps(0, 64, 128, 191);
  1157. U8 index = tep->getShiny();
  1158. if (index > 3)
  1159. {
  1160. llwarns << "Shiny index too large (" << index
  1161. << ") for face " << f << " of object "
  1162. << mVObjp->getID() << llendl;
  1163. llassert(false);
  1164. index = 3;
  1165. }
  1166. color.mV[3] = shine_steps.mV[tep->getShiny()];
  1167. }
  1168. }
  1169. }
  1170. // INDICES
  1171. bool result;
  1172. if (full_rebuild)
  1173. {
  1174. LL_FAST_TIMER(FTM_FACE_GEOM_INDEX);
  1175. result = mVertexBuffer->getIndexStrider(indicesp, mIndicesIndex,
  1176. mIndicesCount);
  1177. if (!result)
  1178. {
  1179. llwarns << "getIndexStrider() failed !" << llendl;
  1180. return false;
  1181. }
  1182. #ifdef __AVX2__
  1183. // Kathrine Jansma's AVX2 optimized code
  1184. volatile __m256i* dst = (__m256i*)indicesp.get();
  1185. __m256i* src = (__m256i*)vf.mIndices;
  1186. __m256i offset = _mm256_set1_epi16(index_offset);
  1187. U32 end = num_indices / 16;
  1188. for (U32 i = 0; i < end; ++i)
  1189. {
  1190. __m256i res = _mm256_add_epi16(src[i], offset);
  1191. _mm256_storeu_si256((__m256i*)dst++, res);
  1192. }
  1193. U16* idx = (U16*)dst;
  1194. for (U32 i = end * 16; i < num_indices; ++i)
  1195. {
  1196. *idx++ = vf.mIndices[i] + index_offset;
  1197. }
  1198. #else
  1199. volatile __m128i* dst = (__m128i*)indicesp.get();
  1200. __m128i* src = (__m128i*)vf.mIndices;
  1201. __m128i offset = _mm_set1_epi16(index_offset);
  1202. U32 end = num_indices / 8;
  1203. for (U32 i = 0; i < end; ++i)
  1204. {
  1205. __m128i res = _mm_add_epi16(src[i], offset);
  1206. _mm_storeu_si128((__m128i*)dst++, res);
  1207. }
  1208. U16* idx = (U16*)dst;
  1209. for (U32 i = end * 8; i < num_indices; ++i)
  1210. {
  1211. *idx++ = vf.mIndices[i] + index_offset;
  1212. }
  1213. #endif
  1214. }
  1215. F32 cos_ang = 0.f, sin_ang = 0.f;
  1216. constexpr S32 XFORM_BLINNPHONG_COLOR = 1;
  1217. constexpr S32 XFORM_BLINNPHONG_NORMAL = 1 << 1;
  1218. constexpr S32 XFORM_BLINNPHONG_SPECULAR = 1 << 2;
  1219. S32 xforms = 0;
  1220. // For PBR material, transforms will be applied later
  1221. if (rebuild_tcoord && tep && !gltfp)
  1222. {
  1223. cos_ang = cosf(r);
  1224. sin_ang = sinf(r);
  1225. if (cos_ang != 1.f || sin_ang != 0.f || os != 0.f || ot != 0.f ||
  1226. ms != 1.f || mt != 1.f)
  1227. {
  1228. xforms = XFORM_BLINNPHONG_COLOR;
  1229. }
  1230. if (matp && !gUsePBRShaders)
  1231. {
  1232. F32 os_norm = 0.f;
  1233. F32 ot_norm = 0.f;
  1234. matp->getNormalOffset(os_norm, ot_norm);
  1235. if (os_norm != 0.f || ot_norm != 0.f)
  1236. {
  1237. xforms |= XFORM_BLINNPHONG_NORMAL;
  1238. }
  1239. else
  1240. {
  1241. F32 ms_norm = 0.f;
  1242. F32 mt_norm = 0.f;
  1243. matp->getNormalRepeat(ms_norm, mt_norm);
  1244. if (ms_norm != 1.f || mt_norm != 1.f)
  1245. {
  1246. xforms |= XFORM_BLINNPHONG_NORMAL;
  1247. }
  1248. else
  1249. {
  1250. F32 r_norm = matp->getNormalRotation();
  1251. if (cosf(r_norm) != 1.f || sinf(r_norm) != 0.f)
  1252. {
  1253. xforms |= XFORM_BLINNPHONG_NORMAL;
  1254. }
  1255. }
  1256. }
  1257. F32 os_spec = 0.f;
  1258. F32 ot_spec = 0.f;
  1259. matp->getSpecularOffset(os_spec, ot_spec);
  1260. if (os_spec != 0.f || ot_spec != 0.f)
  1261. {
  1262. xforms |= XFORM_BLINNPHONG_SPECULAR;
  1263. }
  1264. else
  1265. {
  1266. F32 ms_spec = 0.f;
  1267. F32 mt_spec = 0.f;
  1268. matp->getSpecularRepeat(ms_spec, mt_spec);
  1269. if (ms_spec != 1.f || mt_spec != 1.f)
  1270. {
  1271. xforms |= XFORM_BLINNPHONG_SPECULAR;
  1272. }
  1273. else
  1274. {
  1275. F32 r_spec = matp->getSpecularRotation();
  1276. if (cosf(r_spec) != 1.f || sinf(r_spec) != 0.f)
  1277. {
  1278. xforms |= XFORM_BLINNPHONG_SPECULAR;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. }
  1284. const LLMeshSkinInfo* skinp = rigged ? mSkinInfo.get() : NULL;
  1285. LLMatrix4a mat_vert;
  1286. if (rebuild_pos)
  1287. {
  1288. if (skinp)
  1289. {
  1290. // Override with bind shape matrix if rigged
  1291. mat_vert.loadu(skinp->mBindShapeMatrix);
  1292. }
  1293. else
  1294. {
  1295. mat_vert.loadu(mat_vert_in);
  1296. }
  1297. }
  1298. LLMatrix4a mat_normal;
  1299. if (rebuild_normal || rebuild_tangent)
  1300. {
  1301. if (skinp)
  1302. {
  1303. mat_normal.loadu(skinp->mBindShapeMatrix);
  1304. mat_normal.invert();
  1305. mat_normal.transpose();
  1306. }
  1307. else
  1308. {
  1309. mat_normal.loadu(mat_norm_in);
  1310. }
  1311. }
  1312. if (rebuild_tcoord)
  1313. {
  1314. LL_FAST_TIMER(FTM_FACE_GEOM_TEXTURE);
  1315. // Bump setup
  1316. LLVector4a binormal_dir(-sin_ang, cos_ang, 0.f);
  1317. LLVector4a bump_s_prim_light_ray(0.f, 0.f, 0.f);
  1318. LLVector4a bump_t_prim_light_ray(0.f, 0.f, 0.f);
  1319. LLQuaternion bump_quat;
  1320. if (mDrawablep->isActive())
  1321. {
  1322. bump_quat = LLQuaternion(mDrawablep->getRenderMatrix());
  1323. }
  1324. if (bump_code)
  1325. {
  1326. mVObjp->getVolume()->genTangents(f);
  1327. F32 offset_multiple = 1.f / 256.f;
  1328. switch (bump_code)
  1329. {
  1330. case BE_NO_BUMP:
  1331. offset_multiple = 0.f;
  1332. break;
  1333. case BE_BRIGHTNESS:
  1334. case BE_DARKNESS:
  1335. {
  1336. LLViewerTexture* tex =
  1337. mTexture[LLRender::DIFFUSE_MAP].get();
  1338. if (tex && tex->hasGLTexture())
  1339. {
  1340. // Offset by approximately one texel
  1341. S32 cur_discard = tex->getDiscardLevel();
  1342. S32 max_size = llmax(tex->getWidth(),
  1343. tex->getHeight());
  1344. max_size <<= cur_discard;
  1345. constexpr F32 ARTIFICIAL_OFFSET = 2.f;
  1346. offset_multiple = ARTIFICIAL_OFFSET / (F32)max_size;
  1347. }
  1348. break;
  1349. }
  1350. default: // Standard bumpmap texture assumed to be 256x256
  1351. break;
  1352. }
  1353. F32 s_scale = 1.f;
  1354. F32 t_scale = 1.f;
  1355. if (tep)
  1356. {
  1357. tep->getScale(&s_scale, &t_scale);
  1358. }
  1359. // Use the nudged south when coming from above Sun angle, such
  1360. // that emboss mapping always shows up on the upward faces of cubes
  1361. // when it is noon (since a lot of builders build with the Sun
  1362. // forced to noon).
  1363. const LLVector3& sun_ray = gSky.mVOSkyp->mBumpSunDir;
  1364. LLVector3 primary_light_ray;
  1365. if (sun_ray.mV[VZ] > 0.f)
  1366. {
  1367. primary_light_ray = sun_ray;
  1368. }
  1369. else
  1370. {
  1371. primary_light_ray = gSky.getMoonDirection();
  1372. }
  1373. bump_s_prim_light_ray.load3((offset_multiple * s_scale *
  1374. primary_light_ray).mV);
  1375. bump_t_prim_light_ray.load3((offset_multiple * t_scale *
  1376. primary_light_ray).mV);
  1377. }
  1378. const LLTextureEntry* te2p = getTextureEntry();
  1379. U8 texgen = te2p ? te2p->getTexGen()
  1380. : LLTextureEntry::TEX_GEN_DEFAULT;
  1381. if (rebuild_tcoord && texgen != LLTextureEntry::TEX_GEN_DEFAULT)
  1382. {
  1383. // Planar texgen needs binormals
  1384. mVObjp->getVolume()->genTangents(f);
  1385. }
  1386. LLVOVolume* vobj = (LLVOVolume*)((LLViewerObject*)mVObjp);
  1387. U8 tex_mode = vobj->mTexAnimMode;
  1388. // When texture animation is in play, override specular and normal map
  1389. // tex coords with diffuse texcoords.
  1390. bool tex_anim = vobj->mTextureAnimp != NULL;
  1391. if (isState(TEXTURE_ANIM))
  1392. {
  1393. if (!tex_mode)
  1394. {
  1395. clearState(TEXTURE_ANIM);
  1396. }
  1397. else
  1398. {
  1399. os = ot = r = sin_ang = 0.f;
  1400. cos_ang = ms = mt = 1.f;
  1401. xforms = 0;
  1402. }
  1403. #if 0 // Performance viewer change (removal of isState(RIGGED) test)
  1404. if (getVirtualSize() >= MIN_TEX_ANIM_SIZE || isState(RIGGED))
  1405. #else
  1406. if (getVirtualSize() >= MIN_TEX_ANIM_SIZE)
  1407. #endif
  1408. {
  1409. // Do not override texture transform during tc bake
  1410. tex_mode = 0;
  1411. }
  1412. }
  1413. LLVector4a scalea;
  1414. scalea.load3(scale.mV);
  1415. bool vb_has_tc1 =
  1416. mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_TEXCOORD1);
  1417. bool do_bump = bump_code && vb_has_tc1;
  1418. if ((matp || gltfp) && !do_bump)
  1419. {
  1420. do_bump = vb_has_tc1 ||
  1421. mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_TEXCOORD2);
  1422. }
  1423. bool do_tex_mat = tex_mode && mTextureMatrix;
  1424. if (!do_bump)
  1425. {
  1426. // Not bump mapped, might be able to do a cheap update
  1427. result = mVertexBuffer->getTexCoord0Strider(tex_coords0,
  1428. mGeomIndex,
  1429. mGeomCount);
  1430. if (!result)
  1431. {
  1432. llwarns << "getTexCoord0Strider() failed !" << llendl;
  1433. return false;
  1434. }
  1435. if (texgen != LLTextureEntry::TEX_GEN_PLANAR)
  1436. {
  1437. if (!do_tex_mat)
  1438. {
  1439. if (!xforms)
  1440. {
  1441. S32 tc_size = (num_vertices * 2 * sizeof(F32) + 0xF) &
  1442. ~0xF;
  1443. LLVector4a::memcpyNonAliased16((F32*)tex_coords0.get(),
  1444. (F32*)vf.mTexCoords,
  1445. tc_size);
  1446. }
  1447. else
  1448. {
  1449. F32* dst = (F32*)tex_coords0.get();
  1450. LLVector4a* src = (LLVector4a*)vf.mTexCoords;
  1451. LLVector4a trans;
  1452. trans.splat(-0.5f);
  1453. LLVector4a rot0;
  1454. rot0.set(cos_ang, -sin_ang, cos_ang, -sin_ang);
  1455. LLVector4a rot1;
  1456. rot1.set(sin_ang, cos_ang, sin_ang, cos_ang);
  1457. LLVector4a scale;
  1458. scale.set(ms, mt, ms, mt);
  1459. LLVector4a offset;
  1460. offset.set(os + 0.5f, ot + 0.5f, os + 0.5f, ot + 0.5f);
  1461. LLVector4Logical mask;
  1462. mask.clear();
  1463. mask.setElement<2>();
  1464. mask.setElement<3>();
  1465. U32 count = num_vertices / 2 + num_vertices % 2;
  1466. for (U32 i = 0; i < count; ++i)
  1467. {
  1468. LLVector4a res = *src++;
  1469. xform4a(res, trans, mask, rot0, rot1, offset,
  1470. scale);
  1471. res.store4a(dst);
  1472. dst += 4;
  1473. }
  1474. }
  1475. }
  1476. else
  1477. {
  1478. // Do tex mat, no texgen, no bump
  1479. for (U32 i = 0; i < num_vertices; ++i)
  1480. {
  1481. LLVector2 tc(vf.mTexCoords[i]);
  1482. LLVector3 tmp(tc.mV[0], tc.mV[1], 0.f);
  1483. tmp = tmp * *mTextureMatrix;
  1484. tc.mV[0] = tmp.mV[0];
  1485. tc.mV[1] = tmp.mV[1];
  1486. *tex_coords0++ = tc;
  1487. }
  1488. }
  1489. }
  1490. // No bump, tex gen planar
  1491. else if (do_tex_mat)
  1492. {
  1493. for (U32 i = 0; i < num_vertices; ++i)
  1494. {
  1495. LLVector2 tc(vf.mTexCoords[i]);
  1496. LLVector4a& norm = vf.mNormals[i];
  1497. LLVector4a& center = *(vf.mCenter);
  1498. LLVector4a vec = vf.mPositions[i];
  1499. vec.mul(scalea);
  1500. planarProjection(tc, norm, center, vec);
  1501. LLVector3 tmp(tc.mV[0], tc.mV[1], 0.f);
  1502. tmp = tmp * *mTextureMatrix;
  1503. tc.mV[0] = tmp.mV[0];
  1504. tc.mV[1] = tmp.mV[1];
  1505. *tex_coords0++ = tc;
  1506. }
  1507. }
  1508. else if (xforms || !gUsePBRShaders)
  1509. {
  1510. for (U32 i = 0; i < num_vertices; ++i)
  1511. {
  1512. LLVector2 tc(vf.mTexCoords[i]);
  1513. LLVector4a& norm = vf.mNormals[i];
  1514. LLVector4a& center = *(vf.mCenter);
  1515. LLVector4a vec = vf.mPositions[i];
  1516. vec.mul(scalea);
  1517. planarProjection(tc, norm, center, vec);
  1518. xform(tc, cos_ang, sin_ang, os, ot, ms, mt);
  1519. *tex_coords0++ = tc;
  1520. }
  1521. }
  1522. else // PBR mode, no xforms
  1523. {
  1524. for (U32 i = 0; i < num_vertices; ++i)
  1525. {
  1526. LLVector2 tc(vf.mTexCoords[i]);
  1527. LLVector4a& norm = vf.mNormals[i];
  1528. LLVector4a& center = *(vf.mCenter);
  1529. LLVector4a vec = vf.mPositions[i];
  1530. vec.mul(scalea);
  1531. planarProjection(tc, norm, center, vec);
  1532. *tex_coords0++ = tc;
  1533. }
  1534. }
  1535. }
  1536. else
  1537. {
  1538. // Bump mapped or has material, just do the whole expensive loop
  1539. static std::vector<LLVector2> bump_tc;
  1540. bump_tc.clear();
  1541. bump_tc.reserve(num_vertices);
  1542. if (matp && matp->getNormalID().notNull())
  1543. {
  1544. // Writing out normal and specular texture coordinates, not
  1545. // bump offsets
  1546. do_bump = false;
  1547. }
  1548. LLStrider<LLVector2> dst;
  1549. for (U32 ch = 0; ch < 3; ++ch)
  1550. {
  1551. S32 xform_channel = 0;
  1552. switch (ch)
  1553. {
  1554. case 0:
  1555. {
  1556. result =
  1557. mVertexBuffer->getTexCoord0Strider(dst, mGeomIndex,
  1558. mGeomCount);
  1559. if (!result)
  1560. {
  1561. llwarns << "getTexCoord0Strider() failed !"
  1562. << llendl;
  1563. return false;
  1564. }
  1565. xform_channel = XFORM_BLINNPHONG_COLOR;
  1566. break;
  1567. }
  1568. case 1:
  1569. {
  1570. if (!vb_has_tc1)
  1571. {
  1572. continue;
  1573. }
  1574. result =
  1575. mVertexBuffer->getTexCoord1Strider(dst, mGeomIndex,
  1576. mGeomCount);
  1577. if (!result)
  1578. {
  1579. llwarns << "getTexCoord1Strider() failed !"
  1580. << llendl;
  1581. return false;
  1582. }
  1583. if (matp && !tex_anim)
  1584. {
  1585. r = matp->getNormalRotation();
  1586. matp->getNormalOffset(os, ot);
  1587. matp->getNormalRepeat(ms, mt);
  1588. cos_ang = cosf(r);
  1589. sin_ang = sinf(r);
  1590. }
  1591. xform_channel = XFORM_BLINNPHONG_NORMAL;
  1592. break;
  1593. }
  1594. case 2:
  1595. {
  1596. if (!mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_TEXCOORD2))
  1597. {
  1598. continue;
  1599. }
  1600. result =
  1601. mVertexBuffer->getTexCoord2Strider(dst, mGeomIndex,
  1602. mGeomCount);
  1603. if (!result)
  1604. {
  1605. llwarns << "getTexCoord2Strider() failed !"
  1606. << llendl;
  1607. return false;
  1608. }
  1609. if (matp && !tex_anim)
  1610. {
  1611. r = matp->getSpecularRotation();
  1612. matp->getSpecularOffset(os, ot);
  1613. matp->getSpecularRepeat(ms, mt);
  1614. cos_ang = cosf(r);
  1615. sin_ang = sinf(r);
  1616. }
  1617. xform_channel = XFORM_BLINNPHONG_SPECULAR;
  1618. }
  1619. }
  1620. bool do_xform = (xforms & xform_channel) != 0 ||
  1621. gUsePBRShaders;
  1622. if (texgen == LLTextureEntry::TEX_GEN_PLANAR &&
  1623. !(tex_mode && mTextureMatrix))
  1624. {
  1625. U32 i = 0;
  1626. #ifdef __AVX2__
  1627. if (num_vertices >= 8)
  1628. {
  1629. __m256 cos_vec = _mm256_set1_ps(cos_ang);
  1630. __m256 sin_vec = _mm256_set1_ps(sin_ang);
  1631. __m256 off = _mm256_set1_ps(-0.5f);
  1632. __m256 osoff = _mm256_set1_ps(os + 0.5f);
  1633. __m256 otoff = _mm256_set1_ps(ot + 0.5f);
  1634. __m256 ms_vec = _mm256_set1_ps(ms);
  1635. __m256 mt_vec = _mm256_set1_ps(mt);
  1636. F32 sv[8], tv[8];
  1637. LLVector4a& center = *(vf.mCenter);
  1638. do
  1639. {
  1640. for (S32 j = 0; j < 8; ++j, ++i)
  1641. {
  1642. LLVector2 tcv(vf.mTexCoords[i]);
  1643. LLVector4a vec = vf.mPositions[i];
  1644. vec.mul(scalea);
  1645. planarProjection(tcv, vf.mNormals[i], center,
  1646. vec);
  1647. sv[j] = tcv.mV[0];
  1648. tv[j] = tcv.mV[1];
  1649. }
  1650. __m256 svv = _mm256_loadu_ps(sv);
  1651. __m256 tvv = _mm256_loadu_ps(tv);
  1652. // Texture transforms are done about the center of
  1653. // the face
  1654. svv = _mm256_add_ps(svv, off);
  1655. tvv = _mm256_add_ps(tvv, off);
  1656. // Transform the texture coordinates for this face.
  1657. __m256 coss = _mm256_mul_ps(svv, cos_vec);
  1658. __m256 sins = _mm256_mul_ps(svv, sin_vec);
  1659. svv = _mm256_fmadd_ps(tvv, sin_vec, coss);
  1660. tvv = _mm256_fmsub_ps(tvv, cos_vec, sins);
  1661. // Then scale and offset
  1662. svv = _mm256_fmadd_ps(svv, ms_vec, osoff);
  1663. tvv = _mm256_fmadd_ps(tvv, mt_vec, otoff);
  1664. _mm256_storeu_ps(sv, svv);
  1665. _mm256_storeu_ps(tv, tvv);
  1666. for (S32 j = 0; j < 8; ++j)
  1667. {
  1668. LLVector2 tc(sv[j], tv[j]);
  1669. *dst++ = tc;
  1670. if (!matp && do_bump)
  1671. {
  1672. bump_tc.emplace_back(tc);
  1673. }
  1674. }
  1675. }
  1676. while (i + 8 <= num_vertices);
  1677. }
  1678. #endif
  1679. // SSE2 version
  1680. if (i + 4 <= num_vertices)
  1681. {
  1682. __m128 cos_vec = _mm_set1_ps(cos_ang);
  1683. __m128 sin_vec = _mm_set1_ps(sin_ang);
  1684. __m128 off = _mm_set1_ps(-0.5f);
  1685. __m128 osoff = _mm_set1_ps(os + 0.5f);
  1686. __m128 otoff = _mm_set1_ps(ot + 0.5f);
  1687. __m128 ms_vec = _mm_set1_ps(ms);
  1688. __m128 mt_vec = _mm_set1_ps(mt);
  1689. F32 sv[4], tv[4];
  1690. LLVector4a& center = *(vf.mCenter);
  1691. do
  1692. {
  1693. for (S32 j = 0; j < 4; ++j, ++i)
  1694. {
  1695. LLVector2 tcv(vf.mTexCoords[i]);
  1696. LLVector4a vec = vf.mPositions[i];
  1697. vec.mul(scalea);
  1698. planarProjection(tcv, vf.mNormals[i], center,
  1699. vec);
  1700. sv[j] = tcv.mV[0];
  1701. tv[j] = tcv.mV[1];
  1702. }
  1703. __m128 svv = _mm_loadu_ps(sv);
  1704. __m128 tvv = _mm_loadu_ps(tv);
  1705. // Texture transforms are done about the center of
  1706. // the face
  1707. svv = _mm_add_ps(svv, off);
  1708. tvv = _mm_add_ps(tvv, off);
  1709. // Transform the texture coordinates for this face.
  1710. __m128 coss = _mm_mul_ps(svv, cos_vec);
  1711. __m128 sins = _mm_mul_ps(svv, sin_vec);
  1712. // No fmadd/fmsub in SSE2: two steps needed...
  1713. svv = _mm_add_ps(_mm_mul_ps(tvv, sin_vec), coss);
  1714. tvv = _mm_sub_ps(_mm_mul_ps(tvv, cos_vec), sins);
  1715. // Then scale and offset
  1716. svv = _mm_add_ps(_mm_mul_ps(svv, ms_vec), osoff);
  1717. tvv = _mm_add_ps(_mm_mul_ps(tvv, mt_vec), otoff);
  1718. _mm_storeu_ps(sv, svv);
  1719. _mm_storeu_ps(tv, tvv);
  1720. for (S32 j = 0; j < 4; ++j)
  1721. {
  1722. LLVector2 tc(sv[j], tv[j]);
  1723. *dst++ = tc;
  1724. if (!matp && do_bump)
  1725. {
  1726. bump_tc.emplace_back(tc);
  1727. }
  1728. }
  1729. }
  1730. while (i + 4 <= num_vertices);
  1731. }
  1732. while (i < num_vertices)
  1733. {
  1734. LLVector2 tc(vf.mTexCoords[i]);
  1735. LLVector4a& norm = vf.mNormals[i];
  1736. LLVector4a& center = *(vf.mCenter);
  1737. LLVector4a vec = vf.mPositions[i++];
  1738. vec.mul(scalea);
  1739. planarProjection(tc, norm, center, vec);
  1740. // Texture transforms are done about the center of the face.
  1741. F32 s = tc.mV[0] - 0.5f;
  1742. F32 t = tc.mV[1] - 0.5f;
  1743. // Handle rotation
  1744. F32 temp = s;
  1745. s = s * cos_ang + t * sin_ang;
  1746. t = -temp * sin_ang + t * cos_ang;
  1747. // Then scale
  1748. s *= ms;
  1749. t *= mt;
  1750. // Then offset
  1751. s += os + 0.5f;
  1752. t += ot + 0.5f;
  1753. tc.mV[0] = s;
  1754. tc.mV[1] = t;
  1755. *dst++ = tc;
  1756. if (!matp && do_bump)
  1757. {
  1758. bump_tc.emplace_back(tc);
  1759. }
  1760. }
  1761. }
  1762. else if (tex_mode && mTextureMatrix)
  1763. {
  1764. for (U32 i = 0; i < num_vertices; ++i)
  1765. {
  1766. LLVector2 tc(vf.mTexCoords[i]);
  1767. if (texgen == LLTextureEntry::TEX_GEN_PLANAR)
  1768. {
  1769. LLVector4a& norm = vf.mNormals[i];
  1770. LLVector4a& center = *(vf.mCenter);
  1771. LLVector4a vec = vf.mPositions[i];
  1772. vec.mul(scalea);
  1773. planarProjection(tc, norm, center, vec);
  1774. }
  1775. LLVector3 tmp(tc.mV[0], tc.mV[1], 0.f);
  1776. tmp = tmp * *mTextureMatrix;
  1777. tc.mV[0] = tmp.mV[0];
  1778. tc.mV[1] = tmp.mV[1];
  1779. *dst++ = tc;
  1780. if (!matp && do_bump)
  1781. {
  1782. bump_tc.emplace_back(tc);
  1783. }
  1784. }
  1785. }
  1786. else if (do_xform) // Always true in EE rendering mode. HB
  1787. {
  1788. U32 i = 0;
  1789. #ifdef __AVX2__
  1790. if (num_vertices >= 8)
  1791. {
  1792. __m256 cos_vec = _mm256_set1_ps(cos_ang);
  1793. __m256 sin_vec = _mm256_set1_ps(sin_ang);
  1794. __m256 off = _mm256_set1_ps(-0.5f);
  1795. __m256 osoff = _mm256_set1_ps(os + 0.5f);
  1796. __m256 otoff = _mm256_set1_ps(ot + 0.5f);
  1797. __m256 ms_vec = _mm256_set1_ps(ms);
  1798. __m256 mt_vec = _mm256_set1_ps(mt);
  1799. F32 sv[8], tv[8];
  1800. do
  1801. {
  1802. sv[0] = vf.mTexCoords[i].mV[0];
  1803. tv[0] = vf.mTexCoords[i++].mV[1];
  1804. sv[1] = vf.mTexCoords[i].mV[0];
  1805. tv[1] = vf.mTexCoords[i++].mV[1];
  1806. sv[2] = vf.mTexCoords[i].mV[0];
  1807. tv[2] = vf.mTexCoords[i++].mV[1];
  1808. sv[3] = vf.mTexCoords[i].mV[0];
  1809. tv[3] = vf.mTexCoords[i++].mV[1];
  1810. sv[4] = vf.mTexCoords[i].mV[0];
  1811. tv[4] = vf.mTexCoords[i++].mV[1];
  1812. sv[5] = vf.mTexCoords[i].mV[0];
  1813. tv[5] = vf.mTexCoords[i++].mV[1];
  1814. sv[6] = vf.mTexCoords[i].mV[0];
  1815. tv[6] = vf.mTexCoords[i++].mV[1];
  1816. sv[7] = vf.mTexCoords[i].mV[0];
  1817. tv[7] = vf.mTexCoords[i++].mV[1];
  1818. __m256 svv = _mm256_loadu_ps(sv);
  1819. __m256 tvv = _mm256_loadu_ps(tv);
  1820. // Texture transforms are done about the center of
  1821. // the face
  1822. svv = _mm256_add_ps(svv, off);
  1823. tvv = _mm256_add_ps(tvv, off);
  1824. // Transform the texture coordinates for this face.
  1825. __m256 coss = _mm256_mul_ps(svv, cos_vec);
  1826. __m256 sins = _mm256_mul_ps(svv, sin_vec);
  1827. svv = _mm256_fmadd_ps(tvv, sin_vec, coss);
  1828. tvv = _mm256_fmsub_ps(tvv, cos_vec, sins);
  1829. // Then scale and offset
  1830. svv = _mm256_fmadd_ps(svv, ms_vec, osoff);
  1831. tvv = _mm256_fmadd_ps(tvv, mt_vec, otoff);
  1832. _mm256_storeu_ps(sv, svv);
  1833. _mm256_storeu_ps(tv, tvv);
  1834. for (U32 j = 0; j < 8; ++j)
  1835. {
  1836. LLVector2 tc(sv[j], tv[j]);
  1837. *dst++ = tc;
  1838. if (!matp && do_bump)
  1839. {
  1840. bump_tc.emplace_back(tc);
  1841. }
  1842. }
  1843. }
  1844. while (i + 8 <= num_vertices);
  1845. }
  1846. #endif
  1847. // SSE2 version
  1848. if (i + 4 <= num_vertices)
  1849. {
  1850. __m128 cos_vec = _mm_set1_ps(cos_ang);
  1851. __m128 sin_vec = _mm_set1_ps(sin_ang);
  1852. __m128 off = _mm_set1_ps(-0.5f);
  1853. __m128 osoff = _mm_set1_ps(os + 0.5f);
  1854. __m128 otoff = _mm_set1_ps(ot + 0.5f);
  1855. __m128 ms_vec = _mm_set1_ps(ms);
  1856. __m128 mt_vec = _mm_set1_ps(mt);
  1857. F32 sv[4], tv[4];
  1858. do
  1859. {
  1860. sv[0] = vf.mTexCoords[i].mV[0];
  1861. tv[0] = vf.mTexCoords[i++].mV[1];
  1862. sv[1] = vf.mTexCoords[i].mV[0];
  1863. tv[1] = vf.mTexCoords[i++].mV[1];
  1864. sv[2] = vf.mTexCoords[i].mV[0];
  1865. tv[2] = vf.mTexCoords[i++].mV[1];
  1866. sv[3] = vf.mTexCoords[i].mV[0];
  1867. tv[3] = vf.mTexCoords[i++].mV[1];
  1868. __m128 svv = _mm_loadu_ps(sv);
  1869. __m128 tvv = _mm_loadu_ps(tv);
  1870. // Texture transforms are done about the center of
  1871. // the face
  1872. svv = _mm_add_ps(svv, off);
  1873. tvv = _mm_add_ps(tvv, off);
  1874. // Transform the texture coordinates for this face.
  1875. __m128 coss = _mm_mul_ps(svv, cos_vec);
  1876. __m128 sins = _mm_mul_ps(svv, sin_vec);
  1877. // No fmadd/fmsub in SSE2: two steps needed...
  1878. svv = _mm_add_ps(_mm_mul_ps(tvv, sin_vec), coss);
  1879. tvv = _mm_sub_ps(_mm_mul_ps(tvv, cos_vec), sins);
  1880. // Then scale and offset
  1881. svv = _mm_add_ps(_mm_mul_ps(svv, ms_vec), osoff);
  1882. tvv = _mm_add_ps(_mm_mul_ps(tvv, mt_vec), otoff);
  1883. _mm_storeu_ps(sv, svv);
  1884. _mm_storeu_ps(tv, tvv);
  1885. for (U32 j = 0; j < 4; ++j)
  1886. {
  1887. LLVector2 tc(sv[j], tv[j]);
  1888. *dst++ = tc;
  1889. if (!matp && do_bump)
  1890. {
  1891. bump_tc.emplace_back(tc);
  1892. }
  1893. }
  1894. }
  1895. while (i + 4 <= num_vertices);
  1896. }
  1897. while (i < num_vertices)
  1898. {
  1899. LLVector2 tc(vf.mTexCoords[i++]);
  1900. xform(tc, cos_ang, sin_ang, os, ot, ms, mt);
  1901. *dst++ = tc;
  1902. if (!matp && do_bump)
  1903. {
  1904. bump_tc.emplace_back(tc);
  1905. }
  1906. }
  1907. }
  1908. else // PBR rendering mode, no xforms. HB
  1909. {
  1910. U32 i = 0;
  1911. while (i < num_vertices)
  1912. {
  1913. LLVector2 tc(vf.mTexCoords[i++]);
  1914. *dst++ = tc;
  1915. if (do_bump)
  1916. {
  1917. bump_tc.emplace_back(tc);
  1918. }
  1919. }
  1920. }
  1921. }
  1922. if (!matp && !gltfp && do_bump)
  1923. {
  1924. result = mVertexBuffer->getTexCoord1Strider(tex_coords1,
  1925. mGeomIndex,
  1926. mGeomCount);
  1927. if (!result)
  1928. {
  1929. llwarns << "getTexCoord1Strider() failed !" << llendl;
  1930. return false;
  1931. }
  1932. LLMatrix4a tangent_to_object;
  1933. LLVector4a tangent, binorm, t, binormal;
  1934. LLVector3 t2;
  1935. for (U32 i = 0; i < num_vertices; ++i)
  1936. {
  1937. tangent = vf.mTangents[i];
  1938. binorm.setCross3(vf.mNormals[i], tangent);
  1939. binorm.mul(tangent.getF32ptr()[3]);
  1940. tangent_to_object.setRows(tangent, binorm, vf.mNormals[i]);
  1941. tangent_to_object.rotate(binormal_dir, t);
  1942. mat_normal.rotate(t, binormal);
  1943. // VECTORIZE THIS
  1944. if (mDrawablep->isActive())
  1945. {
  1946. t2.set(binormal.getF32ptr());
  1947. t2 *= bump_quat;
  1948. binormal.load3(t2.mV);
  1949. }
  1950. binormal.normalize3fast();
  1951. *tex_coords1++ = bump_tc[i] +
  1952. LLVector2(bump_s_prim_light_ray.dot3(tangent).getF32(),
  1953. bump_t_prim_light_ray.dot3(binormal).getF32());
  1954. }
  1955. }
  1956. }
  1957. }
  1958. if (rebuild_pos)
  1959. {
  1960. LL_FAST_TIMER(FTM_FACE_GEOM_POSITION);
  1961. llassert(num_vertices > 0);
  1962. result = mVertexBuffer->getVertexStrider(vert, mGeomIndex, mGeomCount);
  1963. if (!result)
  1964. {
  1965. llwarns << "getVertexStrider() failed !" << llendl;
  1966. return false;
  1967. }
  1968. LLVector4a* src = vf.mPositions;
  1969. LLVector4a* end = src + num_vertices;
  1970. S32 index =
  1971. mTextureIndex < FACE_DO_NOT_BATCH_TEXTURES ? mTextureIndex : 0;
  1972. F32 val = 0.f;
  1973. S32* vp = (S32*)&val;
  1974. *vp = index;
  1975. LLVector4a tex_idx(0.f, 0.f, 0.f, val);
  1976. LLVector4Logical mask;
  1977. mask.clear();
  1978. mask.setElement<3>();
  1979. F32* dst = (F32*)vert.get();
  1980. F32* end_f32 = dst + mGeomCount * 4;
  1981. LLVector4a res0, tmp;
  1982. while (src < end)
  1983. {
  1984. mat_vert.affineTransform(*src++, res0);
  1985. tmp.setSelectWithMask(mask, tex_idx, res0);
  1986. tmp.store4a((F32*)dst);
  1987. dst += 4;
  1988. }
  1989. while (dst < end_f32)
  1990. {
  1991. res0.store4a((F32*)dst);
  1992. dst += 4;
  1993. }
  1994. }
  1995. if (rebuild_normal)
  1996. {
  1997. LL_FAST_TIMER(FTM_FACE_GEOM_NORMAL);
  1998. result = mVertexBuffer->getNormalStrider(norm, mGeomIndex, mGeomCount);
  1999. if (!result)
  2000. {
  2001. llwarns << "getNormalStrider() failed !" << llendl;
  2002. return false;
  2003. }
  2004. F32* normals = (F32*)norm.get();
  2005. LLVector4a* src = vf.mNormals;
  2006. LLVector4a* end = src + num_vertices;
  2007. LLVector4a normal;
  2008. while (src < end)
  2009. {
  2010. mat_normal.rotate(*src++, normal);
  2011. normal.store4a(normals);
  2012. normals += 4;
  2013. }
  2014. }
  2015. if (rebuild_tangent)
  2016. {
  2017. LL_FAST_TIMER(FTM_FACE_GEOM_TANGENT);
  2018. result = mVertexBuffer->getTangentStrider(tangent, mGeomIndex,
  2019. mGeomCount);
  2020. if (!result)
  2021. {
  2022. llwarns << "getTangentStrider() failed !" << llendl;
  2023. return false;
  2024. }
  2025. F32* tangents = (F32*)tangent.get();
  2026. mVObjp->getVolume()->genTangents(f);
  2027. LLVector4Logical mask;
  2028. mask.clear();
  2029. mask.setElement<3>();
  2030. LLVector4a* src = vf.mTangents;
  2031. LLVector4a* end = vf.mTangents + num_vertices;
  2032. LLVector4a tangent_out;
  2033. while (src < end)
  2034. {
  2035. mat_normal.rotate(*src, tangent_out);
  2036. #if 1 // Note: removed from LL's PBR code. Is it safe ? Kept for now. HB
  2037. tangent_out.normalize3fast();
  2038. #endif
  2039. tangent_out.setSelectWithMask(mask, *src++, tangent_out);
  2040. tangent_out.store4a(tangents);
  2041. tangents += 4;
  2042. }
  2043. }
  2044. if (rebuild_weights && vf.mWeights)
  2045. {
  2046. LL_FAST_TIMER(FTM_FACE_GEOM_WEIGHTS);
  2047. result = mVertexBuffer->getWeight4Strider(wght, mGeomIndex,
  2048. mGeomCount);
  2049. if (!result)
  2050. {
  2051. llwarns << "getWeight4Strider() failed !" << llendl;
  2052. return false;
  2053. }
  2054. F32* weights = (F32*)wght.get();
  2055. LLVector4a::memcpyNonAliased16(weights, (F32*)vf.mWeights,
  2056. num_vertices * 4 * sizeof(F32));
  2057. }
  2058. if (rebuild_color &&
  2059. mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_COLOR))
  2060. {
  2061. LL_FAST_TIMER(FTM_FACE_GEOM_COLOR);
  2062. result = mVertexBuffer->getColorStrider(colors, mGeomIndex,
  2063. mGeomCount);
  2064. if (!result)
  2065. {
  2066. llwarns << "getColorStrider() failed !" << llendl;
  2067. return false;
  2068. }
  2069. U32 vec[4];
  2070. vec[0] = vec[1] = vec[2] = vec[3] = color.asRGBA();
  2071. LLVector4a src;
  2072. src.loadua((F32*)vec);
  2073. F32* dst = (F32*)colors.get();
  2074. U32 num_vecs = (num_vertices + 3) / 4; // Rounded up
  2075. for (U32 i = 0; i < num_vecs; ++i)
  2076. {
  2077. src.store4a(dst);
  2078. dst += 4;
  2079. }
  2080. }
  2081. if (rebuild_emissive)
  2082. {
  2083. LL_FAST_TIMER(FTM_FACE_GEOM_EMISSIVE);
  2084. LLStrider<LLColor4U> emissive;
  2085. result = mVertexBuffer->getEmissiveStrider(emissive, mGeomIndex,
  2086. mGeomCount);
  2087. if (!result)
  2088. {
  2089. llwarns << "getEmissiveStrider() failed !" << llendl;
  2090. return false;
  2091. }
  2092. F32 glowf = llmax(0.f, getTextureEntry()->getGlow());
  2093. U8 glow = (U8)llmin((S32)(glowf * 255.f), 255);
  2094. LLColor4U glow4u = LLColor4U(0, 0, 0, glow);
  2095. U32 glow32 = glow4u.asRGBA();
  2096. U32 vec[4];
  2097. vec[0] = vec[1] = vec[2] = vec[3] = glow32;
  2098. LLVector4a src;
  2099. src.loadua((F32*)vec);
  2100. F32* dst = (F32*)emissive.get();
  2101. U32 num_vecs = (num_vertices + 3) / 4; // Rounded up
  2102. for (U32 i = 0; i < num_vecs; ++i)
  2103. {
  2104. src.store4a(dst);
  2105. dst += 4;
  2106. }
  2107. }
  2108. if (rebuild_tcoord)
  2109. {
  2110. mTexExtents[0].set(0.f, 0.f);
  2111. mTexExtents[1].set(1.f, 1.f);
  2112. xform(mTexExtents[0], cos_ang, sin_ang, os, ot, ms, mt);
  2113. xform(mTexExtents[1], cos_ang, sin_ang, os, ot, ms, mt);
  2114. F32 es = vf.mTexCoordExtents[1].mV[0] - vf.mTexCoordExtents[0].mV[0];
  2115. F32 et = vf.mTexCoordExtents[1].mV[1] - vf.mTexCoordExtents[0].mV[1];
  2116. mTexExtents[0][0] *= es;
  2117. mTexExtents[1][0] *= es;
  2118. mTexExtents[0][1] *= et;
  2119. mTexExtents[1][1] *= et;
  2120. }
  2121. return true;
  2122. }
  2123. // Check if the face has a media
  2124. bool LLFace::hasMedia() const
  2125. {
  2126. if (mHasMedia)
  2127. {
  2128. return true;
  2129. }
  2130. LLViewerTexture* tex = mTexture[LLRender::DIFFUSE_MAP].get();
  2131. if (tex)
  2132. {
  2133. return tex->hasParcelMedia();
  2134. }
  2135. return false; // No media.
  2136. }
  2137. constexpr F32 LEAST_IMPORTANCE = 0.05f;
  2138. constexpr F32 LEAST_IMPORTANCE_FOR_LARGE_IMAGE = 0.3f;
  2139. void LLFace::resetVirtualSize()
  2140. {
  2141. setVirtualSize(0.f);
  2142. mImportanceToCamera = 0.f;
  2143. }
  2144. F32 LLFace::getTextureVirtualSize()
  2145. {
  2146. F32 radius;
  2147. F32 cos_angle_to_view_dir;
  2148. bool in_frustum = calcPixelArea(cos_angle_to_view_dir, radius);
  2149. if (mPixelArea < F_ALMOST_ZERO || !in_frustum)
  2150. {
  2151. setVirtualSize(0.f);
  2152. return 0.f;
  2153. }
  2154. // Get area of circle in texture space
  2155. LLVector2 tdim = mTexExtents[1] - mTexExtents[0];
  2156. F32 texel_area = (tdim * 0.5f).lengthSquared() * F_PI;
  2157. if (texel_area <= 0)
  2158. {
  2159. // If animated, use minimum (1/64) to avoid blur. HB
  2160. if (isState(TEXTURE_ANIM))
  2161. {
  2162. texel_area = 0.015625f;
  2163. }
  2164. else
  2165. {
  2166. // Take into account the (diffuse) texture scaling to avoid blur,
  2167. // (e.g. on displays with scripted changing text), or on the
  2168. // contrary, to load at an excessive resolution textures with a
  2169. // high repeat per face. HB
  2170. const LLTextureEntry* tep = getTextureEntry();
  2171. if (tep)
  2172. {
  2173. texel_area = tep->getScaleS() * tep->getScaleT();
  2174. }
  2175. else
  2176. {
  2177. texel_area = 1.f; // Use default.
  2178. }
  2179. }
  2180. }
  2181. F32 face_area;
  2182. if (mVObjp->isSculpted() && texel_area > 1.f)
  2183. {
  2184. // Sculpts can break assumptions about texel area
  2185. face_area = mPixelArea;
  2186. }
  2187. else
  2188. {
  2189. // Apply texel area to face area to get accurate ratio
  2190. // face_area /= llclamp(texel_area, 1.f/64.f, 16.f);
  2191. face_area = mPixelArea / llclamp(texel_area, 0.015625f, 128.f);
  2192. }
  2193. face_area = adjustPixelArea(mImportanceToCamera, face_area);
  2194. // If it is a large image, shrink face_area by considering the partial
  2195. // overlapping:
  2196. if (mImportanceToCamera < 1.f &&
  2197. face_area > LLViewerTexture::sMinLargeImageSize &&
  2198. mImportanceToCamera > LEAST_IMPORTANCE_FOR_LARGE_IMAGE)
  2199. {
  2200. LLViewerTexture* tex = mTexture[LLRender::DIFFUSE_MAP].get();
  2201. if (tex && tex->isLargeImage())
  2202. {
  2203. face_area *= adjustPartialOverlapPixelArea(cos_angle_to_view_dir,
  2204. radius);
  2205. }
  2206. }
  2207. setVirtualSize(face_area);
  2208. return face_area;
  2209. }
  2210. bool LLFace::calcPixelArea(F32& cos_angle_to_view_dir, F32& radius)
  2211. {
  2212. // VECTORIZE THIS
  2213. // Get area of circle around face
  2214. LLVector4a center;
  2215. LLVector4a size;
  2216. if (isState(LLFace::RIGGED))
  2217. {
  2218. // Override with avatar bounding box
  2219. LLVOAvatar* avatarp = mVObjp->getAvatar();
  2220. if (!avatarp || avatarp->isDead() || !avatarp->mDrawable)
  2221. {
  2222. return false;
  2223. }
  2224. center.load3(avatarp->getPositionAgent().mV);
  2225. const LLVector4a* exts = avatarp->mDrawable->getSpatialExtents();
  2226. size.setSub(exts[1], exts[0]);
  2227. }
  2228. else
  2229. {
  2230. center.load3(getPositionAgent().mV);
  2231. size.setSub(mExtents[1], mExtents[0]);
  2232. }
  2233. size.mul(0.5f);
  2234. F32 size_squared = size.dot3(size).getF32();
  2235. LLVector4a t;
  2236. t.load3(gViewerCamera.getOrigin().mV);
  2237. LLVector4a look_at;
  2238. look_at.setSub(center, t);
  2239. F32 dist = look_at.getLength3().getF32();
  2240. dist = llmax(dist - size.getLength3().getF32(), 0.001f);
  2241. // Ramp down distance for nearby objects
  2242. if (dist < 16.f)
  2243. {
  2244. dist *= 0.0625f; // /= 16.f;
  2245. dist *= dist;
  2246. dist *= 16.f;
  2247. }
  2248. look_at.normalize3fast();
  2249. // Get area of circle around node
  2250. F32 app_angle = atanf(sqrtf(size_squared) / dist);
  2251. radius = app_angle * LLDrawable::sCurPixelAngle;
  2252. mPixelArea = radius * radius * F_PI;
  2253. LLVector4a x_axis;
  2254. x_axis.load3(gViewerCamera.getXAxis().mV);
  2255. cos_angle_to_view_dir = look_at.dot3(x_axis).getF32();
  2256. // If face has media, check if the face is out of the view frustum.
  2257. if (hasMedia())
  2258. {
  2259. if (!gViewerCamera.AABBInFrustum(center, size))
  2260. {
  2261. mImportanceToCamera = 0.f;
  2262. return false;
  2263. }
  2264. if (cos_angle_to_view_dir > gViewerCamera.getCosHalfFov())
  2265. {
  2266. // The center is within the view frustum
  2267. cos_angle_to_view_dir = 1.f;
  2268. }
  2269. else
  2270. {
  2271. LLVector4a d;
  2272. d.setSub(look_at, x_axis);
  2273. if (dist * dist * d.dot3(d) < size_squared)
  2274. {
  2275. cos_angle_to_view_dir = 1.f;
  2276. }
  2277. }
  2278. }
  2279. if (dist < mBoundingSphereRadius) // Camera is very close
  2280. {
  2281. cos_angle_to_view_dir = 1.f;
  2282. mImportanceToCamera = 1.f;
  2283. }
  2284. else
  2285. {
  2286. mImportanceToCamera = calcImportanceToCamera(cos_angle_to_view_dir,
  2287. dist);
  2288. }
  2289. return true;
  2290. }
  2291. // The projection of the face partially overlaps with the screen
  2292. F32 LLFace::adjustPartialOverlapPixelArea(F32 cos_angle_to_view_dir,
  2293. F32 radius)
  2294. {
  2295. F32 screen_radius = (F32)llmax(gViewerWindowp->getWindowDisplayWidth(),
  2296. gViewerWindowp->getWindowDisplayHeight());
  2297. F32 center_angle = acosf(cos_angle_to_view_dir);
  2298. F32 d = center_angle * LLDrawable::sCurPixelAngle;
  2299. if (d + radius <= screen_radius + 5.f)
  2300. {
  2301. return 1.f;
  2302. }
  2303. #if 0 // Calculating the intersection area of two circles is too expensive
  2304. F32 radius_square = radius * radius;
  2305. F32 d_square = d * d;
  2306. F32 screen_radius_square = screen_radius * screen_radius;
  2307. face_area = radius_square *
  2308. acosf((d_square + radius_square - screen_radius_square) /
  2309. (2.f * d * radius)) +
  2310. screen_radius_square *
  2311. acosf((d_square + screen_radius_square - radius_square) /
  2312. (2.f * d * screen_radius)) -
  2313. 0.5f * sqrtf((-d + radius + screen_radius) *
  2314. (d + radius - screen_radius) * (d - radius + screen_radius) *
  2315. (d + radius + screen_radius));
  2316. return face_area;
  2317. #else // This is a good estimation: bounding box of the bounding sphere:
  2318. F32 alpha = llclamp(0.5f * (radius + screen_radius - d) / radius,
  2319. 0.f, 1.f);
  2320. return alpha * alpha;
  2321. #endif
  2322. }
  2323. constexpr S8 FACE_IMPORTANCE_LEVEL = 4;
  2324. // { distance, importance_weight }
  2325. static const F32 FACE_IMPORTANCE_TO_CAMERA_OVER_DISTANCE[FACE_IMPORTANCE_LEVEL][2] =
  2326. {
  2327. { 16.1f, 1.f },
  2328. { 32.1f, 0.5f },
  2329. { 48.1f, 0.2f },
  2330. { 96.1f, 0.05f }
  2331. };
  2332. // { cosf(angle), importance_weight }
  2333. static const F32 FACE_IMPORTANCE_TO_CAMERA_OVER_ANGLE[FACE_IMPORTANCE_LEVEL][2] =
  2334. {
  2335. { 0.985f /*cosf(10 degrees)*/, 1.f },
  2336. { 0.94f /*cosf(20 degrees)*/, 0.8f },
  2337. { 0.866f /*cosf(30 degrees)*/, 0.64f },
  2338. { 0.f, 0.36f }
  2339. };
  2340. //static
  2341. F32 LLFace::calcImportanceToCamera(F32 cos_angle_to_view_dir, F32 dist)
  2342. {
  2343. if (cos_angle_to_view_dir <= gViewerCamera.getCosHalfFov() ||
  2344. dist >= FACE_IMPORTANCE_TO_CAMERA_OVER_DISTANCE[FACE_IMPORTANCE_LEVEL - 1][0])
  2345. {
  2346. return 0.f;
  2347. }
  2348. if (gViewerCamera.getAverageSpeed() > 10.f ||
  2349. gViewerCamera.getAverageAngularSpeed() > 1.f)
  2350. {
  2351. // If camera moves or rotates too fast, ignore the importance factor
  2352. return 0.f;
  2353. }
  2354. S32 i = 0;
  2355. while (i < FACE_IMPORTANCE_LEVEL &&
  2356. dist > FACE_IMPORTANCE_TO_CAMERA_OVER_DISTANCE[i][0])
  2357. {
  2358. ++i;
  2359. }
  2360. i = llmin(i, FACE_IMPORTANCE_LEVEL - 1);
  2361. F32 dist_factor = FACE_IMPORTANCE_TO_CAMERA_OVER_DISTANCE[i][1];
  2362. i = 0;
  2363. while (i < FACE_IMPORTANCE_LEVEL &&
  2364. cos_angle_to_view_dir < FACE_IMPORTANCE_TO_CAMERA_OVER_ANGLE[i][0])
  2365. {
  2366. ++i;
  2367. }
  2368. i = llmin(i, FACE_IMPORTANCE_LEVEL - 1);
  2369. return dist_factor * FACE_IMPORTANCE_TO_CAMERA_OVER_ANGLE[i][1];
  2370. }
  2371. //static
  2372. F32 LLFace::adjustPixelArea(F32 importance, F32 pixel_area)
  2373. {
  2374. if (pixel_area > LLViewerTexture::sMaxSmallImageSize)
  2375. {
  2376. if (importance < LEAST_IMPORTANCE)
  2377. {
  2378. // If the face is not important, do not load hi-res.
  2379. constexpr F32 MAX_LEAST_IMPORTANCE_IMAGE_SIZE = 128.f * 128.f;
  2380. pixel_area = llmin(pixel_area * 0.5f,
  2381. MAX_LEAST_IMPORTANCE_IMAGE_SIZE);
  2382. }
  2383. else if (pixel_area > LLViewerTexture::sMinLargeImageSize)
  2384. {
  2385. // If is large image, shrink face_area by considering the partial
  2386. // overlapping.
  2387. if (importance < LEAST_IMPORTANCE_FOR_LARGE_IMAGE)
  2388. {
  2389. // If the face is not important, do not load hi-res.
  2390. pixel_area = LLViewerTexture::sMinLargeImageSize;
  2391. }
  2392. }
  2393. }
  2394. return pixel_area;
  2395. }
  2396. bool LLFace::verify(const U32* indices_array) const
  2397. {
  2398. bool ok = true;
  2399. if (mVertexBuffer.isNull())
  2400. {
  2401. // No vertex buffer, face is implicitly valid
  2402. return true;
  2403. }
  2404. // First, check whether the face data fits within the pool's range.
  2405. if (mGeomIndex + mGeomCount > mVertexBuffer->getNumVerts())
  2406. {
  2407. ok = false;
  2408. llinfos << "Face references invalid vertices !" << llendl;
  2409. }
  2410. U32 indices_count = getIndicesCount();
  2411. if (!indices_count)
  2412. {
  2413. return true;
  2414. }
  2415. if (indices_count > LL_MAX_INDICES_COUNT)
  2416. {
  2417. ok = false;
  2418. llinfos << "Face has bogus indices count" << llendl;
  2419. }
  2420. if (mIndicesIndex + mIndicesCount > mVertexBuffer->getNumIndices())
  2421. {
  2422. ok = false;
  2423. llinfos << "Face references invalid indices !" << llendl;
  2424. }
  2425. #if 0
  2426. U32 geom_start = getGeomStart();
  2427. U32 geom_count = mGeomCount;
  2428. const U32* indicesp = indices_array ? indices_array + mIndicesIndex
  2429. : getRawIndices();
  2430. for (U32 i = 0; i < indices_count; ++i)
  2431. {
  2432. U32 delta = indicesp[i] - geom_start;
  2433. if (0 > delta)
  2434. {
  2435. llwarns << "Face index too low !" << llendl;
  2436. llinfos << "i:" << i << " Index:" << indicesp[i] << " GStart: "
  2437. << geom_start << llendl;
  2438. ok = false;
  2439. }
  2440. else if (delta >= geom_count)
  2441. {
  2442. llwarns << "Face index too high !" << llendl;
  2443. llinfos << "i:" << i << " Index:" << indicesp[i] << " GEnd: "
  2444. << geom_start + geom_count << llendl;
  2445. ok = false;
  2446. }
  2447. }
  2448. #endif
  2449. if (!ok)
  2450. {
  2451. printDebugInfo();
  2452. }
  2453. return ok;
  2454. }
  2455. const LLColor4& LLFace::getRenderColor() const
  2456. {
  2457. if (isState(USE_FACE_COLOR))
  2458. {
  2459. return mFaceColor; // Face Color
  2460. }
  2461. const LLTextureEntry* tep = getTextureEntry();
  2462. return tep ? tep->getColor() : LLColor4::white;
  2463. }
  2464. bool LLFace::canBatchTexture() const
  2465. {
  2466. const LLTextureEntry* tep = getTextureEntry();
  2467. if (!tep || tep->getBumpmap() || tep->getMaterialParams().notNull())
  2468. {
  2469. // Bump maps and materials do not work with texture batching yet
  2470. return false;
  2471. }
  2472. if (gUsePBRShaders && tep->getGLTFRenderMaterial())
  2473. {
  2474. // PBR materials break indexed texture batching
  2475. return false;
  2476. }
  2477. // Optionally disable glow batching (trying to determine if glow batching
  2478. // would be causing some render glitches, such as bogus glow flickering on
  2479. // no-glow faces). HB
  2480. static LLCachedControl<bool> batch_glow(gSavedSettings, "RenderBatchGlow");
  2481. if (tep->hasGlow() && !batch_glow)
  2482. {
  2483. return false;
  2484. }
  2485. const LLViewerTexture* texp = getTexture();
  2486. if (texp && texp->getPrimaryFormat() == GL_ALPHA)
  2487. {
  2488. // Cannot batch invisiprims
  2489. return false;
  2490. }
  2491. if (isState(TEXTURE_ANIM) && getVirtualSize() >= MIN_TEX_ANIM_SIZE)
  2492. {
  2493. // Texture animation breaks batches
  2494. return false;
  2495. }
  2496. return true;
  2497. }
  2498. const LLMatrix4& LLFace::getRenderMatrix() const
  2499. {
  2500. return mDrawablep->getRenderMatrix();
  2501. }
  2502. void LLFace::renderIndexed(U32 mask)
  2503. {
  2504. if (mVertexBuffer.isNull())
  2505. {
  2506. return;
  2507. }
  2508. if (gUsePBRShaders)
  2509. {
  2510. mVertexBuffer->setBuffer();
  2511. }
  2512. else if (!mDrawPoolp)
  2513. {
  2514. return;
  2515. }
  2516. else
  2517. {
  2518. if (!mask)
  2519. {
  2520. mask = mDrawPoolp->getVertexDataMask();
  2521. }
  2522. mVertexBuffer->setBuffer(mask);
  2523. }
  2524. mVertexBuffer->drawRange(LLRender::TRIANGLES, mGeomIndex,
  2525. mGeomIndex + mGeomCount - 1, mIndicesCount,
  2526. mIndicesIndex);
  2527. }
  2528. S32 LLFace::getColors(LLStrider<LLColor4U>& colors)
  2529. {
  2530. if (!mGeomCount)
  2531. {
  2532. return -1;
  2533. }
  2534. mVertexBuffer->getColorStrider(colors, mGeomIndex, mGeomCount);
  2535. return mGeomIndex;
  2536. }
  2537. S32 LLFace::getIndices(LLStrider<U16>& indicesp)
  2538. {
  2539. mVertexBuffer->getIndexStrider(indicesp, mIndicesIndex, mIndicesCount);
  2540. llassert(indicesp[0] != indicesp[1]);
  2541. return mIndicesIndex;
  2542. }
  2543. LLVector3 LLFace::getPositionAgent() const
  2544. {
  2545. if (mDrawablep.isNull() || mDrawablep->isStatic())
  2546. {
  2547. return mCenterAgent;
  2548. }
  2549. return mCenterLocal * getRenderMatrix();
  2550. }
  2551. LLViewerTexture* LLFace::getTexture(U32 ch) const
  2552. {
  2553. if (ch < LLRender::NUM_TEXTURE_CHANNELS)
  2554. {
  2555. return mTexture[ch];
  2556. }
  2557. llassert (false);
  2558. return NULL;
  2559. }
  2560. void LLFace::setVertexBuffer(LLVertexBuffer* buffer)
  2561. {
  2562. mVertexBuffer = buffer;
  2563. llassert(verify());
  2564. }
  2565. void LLFace::clearVertexBuffer()
  2566. {
  2567. if (mVertexBuffer.notNull())
  2568. {
  2569. mVertexBuffer = NULL;
  2570. }
  2571. }
  2572. S32 LLFace::getRiggedIndex(U32 type) const
  2573. {
  2574. if (mRiggedIndex.empty())
  2575. {
  2576. return -1;
  2577. }
  2578. llassert(type < mRiggedIndex.size());
  2579. return mRiggedIndex[type];
  2580. }
  2581. U64 LLFace::getSkinHash() const
  2582. {
  2583. return mSkinInfo.notNull() ? mSkinInfo->mHash : 0;
  2584. }
  2585. bool LLFace::isInAlphaPool() const
  2586. {
  2587. if (mPoolType == LLDrawPool::POOL_ALPHA)
  2588. {
  2589. return true;
  2590. }
  2591. if (gUsePBRShaders &&
  2592. (mPoolType == LLDrawPool::POOL_ALPHA_PRE_WATER ||
  2593. mPoolType == LLDrawPool::POOL_ALPHA_POST_WATER))
  2594. {
  2595. return true;
  2596. }
  2597. return false;
  2598. }