shadowmaps.cpp 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097
  1. /*
  2. * Copyright 2013-2014 Dario Manesku. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include "common.h"
  9. #include "bgfx_utils.h"
  10. #include <bgfx/bgfx.h>
  11. #include <bx/timer.h>
  12. #include <bx/math.h>
  13. #include <bx/file.h>
  14. #include "entry/entry.h"
  15. #include "camera.h"
  16. #include "bgfx_utils.h"
  17. #include "imgui/imgui.h"
  18. namespace bgfx
  19. {
  20. int32_t read(bx::ReaderI* _reader, bgfx::VertexLayout& _layout, bx::Error* _err = NULL);
  21. }
  22. namespace
  23. {
  24. #define RENDERVIEW_SHADOWMAP_0_ID 1
  25. #define RENDERVIEW_SHADOWMAP_1_ID 2
  26. #define RENDERVIEW_SHADOWMAP_2_ID 3
  27. #define RENDERVIEW_SHADOWMAP_3_ID 4
  28. #define RENDERVIEW_SHADOWMAP_4_ID 5
  29. #define RENDERVIEW_VBLUR_0_ID 6
  30. #define RENDERVIEW_HBLUR_0_ID 7
  31. #define RENDERVIEW_VBLUR_1_ID 8
  32. #define RENDERVIEW_HBLUR_1_ID 9
  33. #define RENDERVIEW_VBLUR_2_ID 10
  34. #define RENDERVIEW_HBLUR_2_ID 11
  35. #define RENDERVIEW_VBLUR_3_ID 12
  36. #define RENDERVIEW_HBLUR_3_ID 13
  37. #define RENDERVIEW_DRAWSCENE_0_ID 14
  38. #define RENDERVIEW_DRAWSCENE_1_ID 15
  39. #define RENDERVIEW_DRAWDEPTH_0_ID 16
  40. #define RENDERVIEW_DRAWDEPTH_1_ID 17
  41. #define RENDERVIEW_DRAWDEPTH_2_ID 18
  42. #define RENDERVIEW_DRAWDEPTH_3_ID 19
  43. struct LightType
  44. {
  45. enum Enum
  46. {
  47. SpotLight,
  48. PointLight,
  49. DirectionalLight,
  50. Count
  51. };
  52. };
  53. struct DepthImpl
  54. {
  55. enum Enum
  56. {
  57. InvZ,
  58. Linear,
  59. Count
  60. };
  61. };
  62. struct PackDepth
  63. {
  64. enum Enum
  65. {
  66. RGBA,
  67. VSM,
  68. Count
  69. };
  70. };
  71. struct SmImpl
  72. {
  73. enum Enum
  74. {
  75. Hard,
  76. PCF,
  77. VSM,
  78. ESM,
  79. Count
  80. };
  81. };
  82. struct SmType
  83. {
  84. enum Enum
  85. {
  86. Single,
  87. Omni,
  88. Cascade,
  89. Count
  90. };
  91. };
  92. struct TetrahedronFaces
  93. {
  94. enum Enum
  95. {
  96. Green,
  97. Yellow,
  98. Blue,
  99. Red,
  100. Count
  101. };
  102. };
  103. struct ProjType
  104. {
  105. enum Enum
  106. {
  107. Horizontal,
  108. Vertical,
  109. Count
  110. };
  111. };
  112. struct ShadowMapRenderTargets
  113. {
  114. enum Enum
  115. {
  116. First,
  117. Second,
  118. Third,
  119. Fourth,
  120. Count
  121. };
  122. };
  123. struct PosNormalTexcoordVertex
  124. {
  125. float m_x;
  126. float m_y;
  127. float m_z;
  128. uint32_t m_normal;
  129. float m_u;
  130. float m_v;
  131. };
  132. static const float s_texcoord = 5.0f;
  133. static PosNormalTexcoordVertex s_hplaneVertices[] =
  134. {
  135. { -1.0f, 0.0f, 1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), s_texcoord, s_texcoord },
  136. { 1.0f, 0.0f, 1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), s_texcoord, 0.0f },
  137. { -1.0f, 0.0f, -1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), 0.0f, s_texcoord },
  138. { 1.0f, 0.0f, -1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f), 0.0f, 0.0f },
  139. };
  140. static PosNormalTexcoordVertex s_vplaneVertices[] =
  141. {
  142. { -1.0f, 1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 1.0f, 1.0f },
  143. { 1.0f, 1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 1.0f, 0.0f },
  144. { -1.0f, -1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 0.0f, 1.0f },
  145. { 1.0f, -1.0f, 0.0f, encodeNormalRgba8(0.0f, 0.0f, -1.0f), 0.0f, 0.0f },
  146. };
  147. static const uint16_t s_planeIndices[] =
  148. {
  149. 0, 1, 2,
  150. 1, 3, 2,
  151. };
  152. static bool s_originBottomLeft = false;
  153. static bgfx::UniformHandle s_texColor;
  154. static bgfx::UniformHandle s_shadowMap[ShadowMapRenderTargets::Count];
  155. static bgfx::FrameBufferHandle s_rtShadowMap[ShadowMapRenderTargets::Count];
  156. static bgfx::FrameBufferHandle s_rtBlur;
  157. void mtxBillboard(
  158. float* _result
  159. , const float* _view
  160. , const float* _pos
  161. , const float* _scale
  162. )
  163. {
  164. _result[ 0] = _view[0] * _scale[0];
  165. _result[ 1] = _view[4] * _scale[0];
  166. _result[ 2] = _view[8] * _scale[0];
  167. _result[ 3] = 0.0f;
  168. _result[ 4] = _view[1] * _scale[1];
  169. _result[ 5] = _view[5] * _scale[1];
  170. _result[ 6] = _view[9] * _scale[1];
  171. _result[ 7] = 0.0f;
  172. _result[ 8] = _view[2] * _scale[2];
  173. _result[ 9] = _view[6] * _scale[2];
  174. _result[10] = _view[10] * _scale[2];
  175. _result[11] = 0.0f;
  176. _result[12] = _pos[0];
  177. _result[13] = _pos[1];
  178. _result[14] = _pos[2];
  179. _result[15] = 1.0f;
  180. }
  181. void mtxYawPitchRoll(float* _result
  182. , float _yaw
  183. , float _pitch
  184. , float _roll
  185. )
  186. {
  187. float sroll = bx::sin(_roll);
  188. float croll = bx::cos(_roll);
  189. float spitch = bx::sin(_pitch);
  190. float cpitch = bx::cos(_pitch);
  191. float syaw = bx::sin(_yaw);
  192. float cyaw = bx::cos(_yaw);
  193. _result[ 0] = sroll * spitch * syaw + croll * cyaw;
  194. _result[ 1] = sroll * cpitch;
  195. _result[ 2] = sroll * spitch * cyaw - croll * syaw;
  196. _result[ 3] = 0.0f;
  197. _result[ 4] = croll * spitch * syaw - sroll * cyaw;
  198. _result[ 5] = croll * cpitch;
  199. _result[ 6] = croll * spitch * cyaw + sroll * syaw;
  200. _result[ 7] = 0.0f;
  201. _result[ 8] = cpitch * syaw;
  202. _result[ 9] = -spitch;
  203. _result[10] = cpitch * cyaw;
  204. _result[11] = 0.0f;
  205. _result[12] = 0.0f;
  206. _result[13] = 0.0f;
  207. _result[14] = 0.0f;
  208. _result[15] = 1.0f;
  209. }
  210. struct Material
  211. {
  212. union Ambient
  213. {
  214. struct
  215. {
  216. float m_r;
  217. float m_g;
  218. float m_b;
  219. float m_unused;
  220. };
  221. float m_v[4];
  222. };
  223. union Diffuse
  224. {
  225. struct
  226. {
  227. float m_r;
  228. float m_g;
  229. float m_b;
  230. float m_unused;
  231. };
  232. float m_v[4];
  233. };
  234. union Specular
  235. {
  236. struct
  237. {
  238. float m_r;
  239. float m_g;
  240. float m_b;
  241. float m_ns;
  242. };
  243. float m_v[4];
  244. };
  245. Ambient m_ka;
  246. Diffuse m_kd;
  247. Specular m_ks;
  248. };
  249. struct Light
  250. {
  251. union Position
  252. {
  253. struct
  254. {
  255. float m_x;
  256. float m_y;
  257. float m_z;
  258. float m_w;
  259. };
  260. float m_v[4];
  261. };
  262. union LightRgbPower
  263. {
  264. struct
  265. {
  266. float m_r;
  267. float m_g;
  268. float m_b;
  269. float m_power;
  270. };
  271. float m_v[4];
  272. };
  273. union SpotDirectionInner
  274. {
  275. struct
  276. {
  277. float m_x;
  278. float m_y;
  279. float m_z;
  280. float m_inner;
  281. };
  282. float m_v[4];
  283. };
  284. union AttenuationSpotOuter
  285. {
  286. struct
  287. {
  288. float m_attnConst;
  289. float m_attnLinear;
  290. float m_attnQuadrantic;
  291. float m_outer;
  292. };
  293. float m_v[4];
  294. };
  295. void computeViewSpaceComponents(float* _viewMtx)
  296. {
  297. bx::vec4MulMtx(m_position_viewSpace, m_position.m_v, _viewMtx);
  298. float tmp[] =
  299. {
  300. m_spotDirectionInner.m_x
  301. , m_spotDirectionInner.m_y
  302. , m_spotDirectionInner.m_z
  303. , 0.0f
  304. };
  305. bx::vec4MulMtx(m_spotDirectionInner_viewSpace, tmp, _viewMtx);
  306. m_spotDirectionInner_viewSpace[3] = m_spotDirectionInner.m_v[3];
  307. }
  308. Position m_position;
  309. float m_position_viewSpace[4];
  310. LightRgbPower m_ambientPower;
  311. LightRgbPower m_diffusePower;
  312. LightRgbPower m_specularPower;
  313. SpotDirectionInner m_spotDirectionInner;
  314. float m_spotDirectionInner_viewSpace[4];
  315. AttenuationSpotOuter m_attenuationSpotOuter;
  316. };
  317. struct Uniforms
  318. {
  319. void init()
  320. {
  321. m_ambientPass = 1.0f;
  322. m_lightingPass = 1.0f;
  323. m_shadowMapBias = 0.003f;
  324. m_shadowMapOffset = 0.0f;
  325. m_shadowMapParam0 = 0.5;
  326. m_shadowMapParam1 = 1.0;
  327. m_depthValuePow = 1.0f;
  328. m_showSmCoverage = 1.0f;
  329. m_shadowMapTexelSize = 1.0f/512.0f;
  330. m_csmFarDistances[0] = 30.0f;
  331. m_csmFarDistances[1] = 90.0f;
  332. m_csmFarDistances[2] = 180.0f;
  333. m_csmFarDistances[3] = 1000.0f;
  334. m_tetraNormalGreen[0] = 0.0f;
  335. m_tetraNormalGreen[1] = -0.57735026f;
  336. m_tetraNormalGreen[2] = 0.81649661f;
  337. m_tetraNormalYellow[0] = 0.0f;
  338. m_tetraNormalYellow[1] = -0.57735026f;
  339. m_tetraNormalYellow[2] = -0.81649661f;
  340. m_tetraNormalBlue[0] = -0.81649661f;
  341. m_tetraNormalBlue[1] = 0.57735026f;
  342. m_tetraNormalBlue[2] = 0.0f;
  343. m_tetraNormalRed[0] = 0.81649661f;
  344. m_tetraNormalRed[1] = 0.57735026f;
  345. m_tetraNormalRed[2] = 0.0f;
  346. m_XNum = 2.0f;
  347. m_YNum = 2.0f;
  348. m_XOffset = 10.0f/512.0f;
  349. m_YOffset = 10.0f/512.0f;
  350. u_params0 = bgfx::createUniform("u_params0", bgfx::UniformType::Vec4);
  351. u_params1 = bgfx::createUniform("u_params1", bgfx::UniformType::Vec4);
  352. u_params2 = bgfx::createUniform("u_params2", bgfx::UniformType::Vec4);
  353. u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
  354. u_smSamplingParams = bgfx::createUniform("u_smSamplingParams", bgfx::UniformType::Vec4);
  355. u_csmFarDistances = bgfx::createUniform("u_csmFarDistances", bgfx::UniformType::Vec4);
  356. u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4);
  357. u_tetraNormalGreen = bgfx::createUniform("u_tetraNormalGreen", bgfx::UniformType::Vec4);
  358. u_tetraNormalYellow = bgfx::createUniform("u_tetraNormalYellow", bgfx::UniformType::Vec4);
  359. u_tetraNormalBlue = bgfx::createUniform("u_tetraNormalBlue", bgfx::UniformType::Vec4);
  360. u_tetraNormalRed = bgfx::createUniform("u_tetraNormalRed", bgfx::UniformType::Vec4);
  361. u_shadowMapMtx0 = bgfx::createUniform("u_shadowMapMtx0", bgfx::UniformType::Mat4);
  362. u_shadowMapMtx1 = bgfx::createUniform("u_shadowMapMtx1", bgfx::UniformType::Mat4);
  363. u_shadowMapMtx2 = bgfx::createUniform("u_shadowMapMtx2", bgfx::UniformType::Mat4);
  364. u_shadowMapMtx3 = bgfx::createUniform("u_shadowMapMtx3", bgfx::UniformType::Mat4);
  365. u_lightPosition = bgfx::createUniform("u_lightPosition", bgfx::UniformType::Vec4);
  366. u_lightAmbientPower = bgfx::createUniform("u_lightAmbientPower", bgfx::UniformType::Vec4);
  367. u_lightDiffusePower = bgfx::createUniform("u_lightDiffusePower", bgfx::UniformType::Vec4);
  368. u_lightSpecularPower = bgfx::createUniform("u_lightSpecularPower", bgfx::UniformType::Vec4);
  369. u_lightSpotDirectionInner = bgfx::createUniform("u_lightSpotDirectionInner", bgfx::UniformType::Vec4);
  370. u_lightAttenuationSpotOuter = bgfx::createUniform("u_lightAttenuationSpotOuter", bgfx::UniformType::Vec4);
  371. u_materialKa = bgfx::createUniform("u_materialKa", bgfx::UniformType::Vec4);
  372. u_materialKd = bgfx::createUniform("u_materialKd", bgfx::UniformType::Vec4);
  373. u_materialKs = bgfx::createUniform("u_materialKs", bgfx::UniformType::Vec4);
  374. }
  375. void setPtrs(Material* _materialPtr, Light* _lightPtr, float* _colorPtr, float* _lightMtxPtr, float* _shadowMapMtx0, float* _shadowMapMtx1, float* _shadowMapMtx2, float* _shadowMapMtx3)
  376. {
  377. m_lightMtxPtr = _lightMtxPtr;
  378. m_colorPtr = _colorPtr;
  379. m_materialPtr = _materialPtr;
  380. m_lightPtr = _lightPtr;
  381. m_shadowMapMtx0 = _shadowMapMtx0;
  382. m_shadowMapMtx1 = _shadowMapMtx1;
  383. m_shadowMapMtx2 = _shadowMapMtx2;
  384. m_shadowMapMtx3 = _shadowMapMtx3;
  385. }
  386. // Call this once at initialization.
  387. void submitConstUniforms()
  388. {
  389. bgfx::setUniform(u_tetraNormalGreen, m_tetraNormalGreen);
  390. bgfx::setUniform(u_tetraNormalYellow, m_tetraNormalYellow);
  391. bgfx::setUniform(u_tetraNormalBlue, m_tetraNormalBlue);
  392. bgfx::setUniform(u_tetraNormalRed, m_tetraNormalRed);
  393. }
  394. // Call this once per frame.
  395. void submitPerFrameUniforms()
  396. {
  397. bgfx::setUniform(u_params1, m_params1);
  398. bgfx::setUniform(u_params2, m_params2);
  399. bgfx::setUniform(u_smSamplingParams, m_paramsBlur);
  400. bgfx::setUniform(u_csmFarDistances, m_csmFarDistances);
  401. bgfx::setUniform(u_materialKa, &m_materialPtr->m_ka);
  402. bgfx::setUniform(u_materialKd, &m_materialPtr->m_kd);
  403. bgfx::setUniform(u_materialKs, &m_materialPtr->m_ks);
  404. bgfx::setUniform(u_lightPosition, &m_lightPtr->m_position_viewSpace);
  405. bgfx::setUniform(u_lightAmbientPower, &m_lightPtr->m_ambientPower);
  406. bgfx::setUniform(u_lightDiffusePower, &m_lightPtr->m_diffusePower);
  407. bgfx::setUniform(u_lightSpecularPower, &m_lightPtr->m_specularPower);
  408. bgfx::setUniform(u_lightSpotDirectionInner, &m_lightPtr->m_spotDirectionInner_viewSpace);
  409. bgfx::setUniform(u_lightAttenuationSpotOuter, &m_lightPtr->m_attenuationSpotOuter);
  410. }
  411. // Call this before each draw call.
  412. void submitPerDrawUniforms()
  413. {
  414. bgfx::setUniform(u_shadowMapMtx0, m_shadowMapMtx0);
  415. bgfx::setUniform(u_shadowMapMtx1, m_shadowMapMtx1);
  416. bgfx::setUniform(u_shadowMapMtx2, m_shadowMapMtx2);
  417. bgfx::setUniform(u_shadowMapMtx3, m_shadowMapMtx3);
  418. bgfx::setUniform(u_params0, m_params0);
  419. bgfx::setUniform(u_lightMtx, m_lightMtxPtr);
  420. bgfx::setUniform(u_color, m_colorPtr);
  421. }
  422. void destroy()
  423. {
  424. bgfx::destroy(u_params0);
  425. bgfx::destroy(u_params1);
  426. bgfx::destroy(u_params2);
  427. bgfx::destroy(u_color);
  428. bgfx::destroy(u_smSamplingParams);
  429. bgfx::destroy(u_csmFarDistances);
  430. bgfx::destroy(u_materialKa);
  431. bgfx::destroy(u_materialKd);
  432. bgfx::destroy(u_materialKs);
  433. bgfx::destroy(u_tetraNormalGreen);
  434. bgfx::destroy(u_tetraNormalYellow);
  435. bgfx::destroy(u_tetraNormalBlue);
  436. bgfx::destroy(u_tetraNormalRed);
  437. bgfx::destroy(u_shadowMapMtx0);
  438. bgfx::destroy(u_shadowMapMtx1);
  439. bgfx::destroy(u_shadowMapMtx2);
  440. bgfx::destroy(u_shadowMapMtx3);
  441. bgfx::destroy(u_lightMtx);
  442. bgfx::destroy(u_lightPosition);
  443. bgfx::destroy(u_lightAmbientPower);
  444. bgfx::destroy(u_lightDiffusePower);
  445. bgfx::destroy(u_lightSpecularPower);
  446. bgfx::destroy(u_lightSpotDirectionInner);
  447. bgfx::destroy(u_lightAttenuationSpotOuter);
  448. }
  449. union
  450. {
  451. struct
  452. {
  453. float m_ambientPass;
  454. float m_lightingPass;
  455. float m_unused00;
  456. float m_unused01;
  457. };
  458. float m_params0[4];
  459. };
  460. union
  461. {
  462. struct
  463. {
  464. float m_shadowMapBias;
  465. float m_shadowMapOffset;
  466. float m_shadowMapParam0;
  467. float m_shadowMapParam1;
  468. };
  469. float m_params1[4];
  470. };
  471. union
  472. {
  473. struct
  474. {
  475. float m_depthValuePow;
  476. float m_showSmCoverage;
  477. float m_shadowMapTexelSize;
  478. float m_unused23;
  479. };
  480. float m_params2[4];
  481. };
  482. union
  483. {
  484. struct
  485. {
  486. float m_XNum;
  487. float m_YNum;
  488. float m_XOffset;
  489. float m_YOffset;
  490. };
  491. float m_paramsBlur[4];
  492. };
  493. float m_tetraNormalGreen[3];
  494. float m_tetraNormalYellow[3];
  495. float m_tetraNormalBlue[3];
  496. float m_tetraNormalRed[3];
  497. float m_csmFarDistances[4];
  498. float* m_lightMtxPtr;
  499. float* m_colorPtr;
  500. Light* m_lightPtr;
  501. float* m_shadowMapMtx0;
  502. float* m_shadowMapMtx1;
  503. float* m_shadowMapMtx2;
  504. float* m_shadowMapMtx3;
  505. Material* m_materialPtr;
  506. private:
  507. bgfx::UniformHandle u_params0;
  508. bgfx::UniformHandle u_params1;
  509. bgfx::UniformHandle u_params2;
  510. bgfx::UniformHandle u_color;
  511. bgfx::UniformHandle u_smSamplingParams;
  512. bgfx::UniformHandle u_csmFarDistances;
  513. bgfx::UniformHandle u_materialKa;
  514. bgfx::UniformHandle u_materialKd;
  515. bgfx::UniformHandle u_materialKs;
  516. bgfx::UniformHandle u_tetraNormalGreen;
  517. bgfx::UniformHandle u_tetraNormalYellow;
  518. bgfx::UniformHandle u_tetraNormalBlue;
  519. bgfx::UniformHandle u_tetraNormalRed;
  520. bgfx::UniformHandle u_shadowMapMtx0;
  521. bgfx::UniformHandle u_shadowMapMtx1;
  522. bgfx::UniformHandle u_shadowMapMtx2;
  523. bgfx::UniformHandle u_shadowMapMtx3;
  524. bgfx::UniformHandle u_lightMtx;
  525. bgfx::UniformHandle u_lightPosition;
  526. bgfx::UniformHandle u_lightAmbientPower;
  527. bgfx::UniformHandle u_lightDiffusePower;
  528. bgfx::UniformHandle u_lightSpecularPower;
  529. bgfx::UniformHandle u_lightSpotDirectionInner;
  530. bgfx::UniformHandle u_lightAttenuationSpotOuter;
  531. };
  532. static Uniforms s_uniforms;
  533. struct RenderState
  534. {
  535. enum Enum
  536. {
  537. Default = 0,
  538. ShadowMap_PackDepth,
  539. ShadowMap_PackDepthHoriz,
  540. ShadowMap_PackDepthVert,
  541. Custom_BlendLightTexture,
  542. Custom_DrawPlaneBottom,
  543. Count
  544. };
  545. uint64_t m_state;
  546. uint32_t m_blendFactorRgba;
  547. uint32_t m_fstencil;
  548. uint32_t m_bstencil;
  549. };
  550. static RenderState s_renderStates[RenderState::Count] =
  551. {
  552. { // Default
  553. 0
  554. | BGFX_STATE_WRITE_RGB
  555. | BGFX_STATE_WRITE_A
  556. | BGFX_STATE_DEPTH_TEST_LESS
  557. | BGFX_STATE_WRITE_Z
  558. | BGFX_STATE_CULL_CCW
  559. | BGFX_STATE_MSAA
  560. , UINT32_MAX
  561. , BGFX_STENCIL_NONE
  562. , BGFX_STENCIL_NONE
  563. },
  564. { // ShadowMap_PackDepth
  565. 0
  566. | BGFX_STATE_WRITE_RGB
  567. | BGFX_STATE_WRITE_A
  568. | BGFX_STATE_WRITE_Z
  569. | BGFX_STATE_DEPTH_TEST_LESS
  570. | BGFX_STATE_CULL_CCW
  571. | BGFX_STATE_MSAA
  572. , UINT32_MAX
  573. , BGFX_STENCIL_NONE
  574. , BGFX_STENCIL_NONE
  575. },
  576. { // ShadowMap_PackDepthHoriz
  577. 0
  578. | BGFX_STATE_WRITE_RGB
  579. | BGFX_STATE_WRITE_A
  580. | BGFX_STATE_WRITE_Z
  581. | BGFX_STATE_DEPTH_TEST_LESS
  582. | BGFX_STATE_CULL_CCW
  583. | BGFX_STATE_MSAA
  584. , UINT32_MAX
  585. , BGFX_STENCIL_TEST_EQUAL
  586. | BGFX_STENCIL_FUNC_REF(1)
  587. | BGFX_STENCIL_FUNC_RMASK(0xff)
  588. | BGFX_STENCIL_OP_FAIL_S_KEEP
  589. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  590. | BGFX_STENCIL_OP_PASS_Z_KEEP
  591. , BGFX_STENCIL_NONE
  592. },
  593. { // ShadowMap_PackDepthVert
  594. 0
  595. | BGFX_STATE_WRITE_RGB
  596. | BGFX_STATE_WRITE_A
  597. | BGFX_STATE_WRITE_Z
  598. | BGFX_STATE_DEPTH_TEST_LESS
  599. | BGFX_STATE_CULL_CCW
  600. | BGFX_STATE_MSAA
  601. , UINT32_MAX
  602. , BGFX_STENCIL_TEST_EQUAL
  603. | BGFX_STENCIL_FUNC_REF(0)
  604. | BGFX_STENCIL_FUNC_RMASK(0xff)
  605. | BGFX_STENCIL_OP_FAIL_S_KEEP
  606. | BGFX_STENCIL_OP_FAIL_Z_KEEP
  607. | BGFX_STENCIL_OP_PASS_Z_KEEP
  608. , BGFX_STENCIL_NONE
  609. },
  610. { // Custom_BlendLightTexture
  611. BGFX_STATE_WRITE_RGB
  612. | BGFX_STATE_WRITE_A
  613. | BGFX_STATE_WRITE_Z
  614. | BGFX_STATE_DEPTH_TEST_LESS
  615. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_COLOR, BGFX_STATE_BLEND_INV_SRC_COLOR)
  616. | BGFX_STATE_CULL_CCW
  617. | BGFX_STATE_MSAA
  618. , UINT32_MAX
  619. , BGFX_STENCIL_NONE
  620. , BGFX_STENCIL_NONE
  621. },
  622. { // Custom_DrawPlaneBottom
  623. BGFX_STATE_WRITE_RGB
  624. | BGFX_STATE_CULL_CW
  625. | BGFX_STATE_MSAA
  626. , UINT32_MAX
  627. , BGFX_STENCIL_NONE
  628. , BGFX_STENCIL_NONE
  629. },
  630. };
  631. struct ViewState
  632. {
  633. ViewState(uint16_t _width = 1280, uint16_t _height = 720)
  634. : m_width(_width)
  635. , m_height(_height)
  636. {
  637. }
  638. uint16_t m_width;
  639. uint16_t m_height;
  640. float m_view[16];
  641. float m_proj[16];
  642. };
  643. struct ClearValues
  644. {
  645. ClearValues(uint32_t _clearRgba = 0x30303000
  646. , float _clearDepth = 1.0f
  647. , uint8_t _clearStencil = 0
  648. )
  649. : m_clearRgba(_clearRgba)
  650. , m_clearDepth(_clearDepth)
  651. , m_clearStencil(_clearStencil)
  652. {
  653. }
  654. uint32_t m_clearRgba;
  655. float m_clearDepth;
  656. uint8_t m_clearStencil;
  657. };
  658. struct Mesh : public ::Mesh
  659. {
  660. void load(const char* _filePath, bool _ramcopy = false)
  661. {
  662. bx::FileReaderI* reader = entry::getFileReader();
  663. if (bx::open(reader, _filePath))
  664. {
  665. ::Mesh::load(reader, _ramcopy);
  666. bx::close(reader);
  667. }
  668. }
  669. void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexLayout _layout, const uint16_t* _indices, uint32_t _numIndices)
  670. {
  671. Group group;
  672. const bgfx::Memory* mem;
  673. uint32_t size;
  674. size = _numVertices * _layout.getStride();
  675. mem = bgfx::makeRef(_vertices, size);
  676. group.m_vbh = bgfx::createVertexBuffer(mem, _layout);
  677. size = _numIndices * 2;
  678. mem = bgfx::makeRef(_indices, size);
  679. group.m_ibh = bgfx::createIndexBuffer(mem);
  680. m_groups.push_back(group);
  681. }
  682. void submit(uint8_t _viewId, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState, bool _submitShadowMaps = false)
  683. {
  684. bgfx::TextureHandle texture = BGFX_INVALID_HANDLE;
  685. submit(_viewId, _mtx, _program, _renderState, texture, _submitShadowMaps);
  686. }
  687. void submit(uint8_t _viewId, float* _mtx, bgfx::ProgramHandle _program, const RenderState& _renderState, bgfx::TextureHandle _texture, bool _submitShadowMaps = false)
  688. {
  689. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  690. {
  691. const Group& group = *it;
  692. // Set uniforms.
  693. s_uniforms.submitPerDrawUniforms();
  694. // Set model matrix for rendering.
  695. bgfx::setTransform(_mtx);
  696. bgfx::setIndexBuffer(group.m_ibh);
  697. bgfx::setVertexBuffer(0, group.m_vbh);
  698. // Set textures.
  699. if (bgfx::kInvalidHandle != _texture.idx)
  700. {
  701. bgfx::setTexture(0, s_texColor, _texture);
  702. }
  703. if (_submitShadowMaps)
  704. {
  705. for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
  706. {
  707. bgfx::setTexture(4 + ii, s_shadowMap[ii], bgfx::getTexture(s_rtShadowMap[ii]));
  708. }
  709. }
  710. // Apply render state.
  711. bgfx::setStencil(_renderState.m_fstencil, _renderState.m_bstencil);
  712. bgfx::setState(_renderState.m_state, _renderState.m_blendFactorRgba);
  713. // Submit.
  714. bgfx::submit(_viewId, _program);
  715. }
  716. }
  717. };
  718. struct PosColorTexCoord0Vertex
  719. {
  720. float m_x;
  721. float m_y;
  722. float m_z;
  723. uint32_t m_rgba;
  724. float m_u;
  725. float m_v;
  726. static void init()
  727. {
  728. ms_layout
  729. .begin()
  730. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  731. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  732. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  733. .end();
  734. }
  735. static bgfx::VertexLayout ms_layout;
  736. };
  737. bgfx::VertexLayout PosColorTexCoord0Vertex::ms_layout;
  738. void screenSpaceQuad(bool _originBottomLeft = true, float _width = 1.0f, float _height = 1.0f)
  739. {
  740. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosColorTexCoord0Vertex::ms_layout) )
  741. {
  742. bgfx::TransientVertexBuffer vb;
  743. bgfx::allocTransientVertexBuffer(&vb, 3, PosColorTexCoord0Vertex::ms_layout);
  744. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)vb.data;
  745. const float zz = 0.0f;
  746. const float minx = -_width;
  747. const float maxx = _width;
  748. const float miny = 0.0f;
  749. const float maxy = _height*2.0f;
  750. const float minu = -1.0f;
  751. const float maxu = 1.0f;
  752. float minv = 0.0f;
  753. float maxv = 2.0f;
  754. if (_originBottomLeft)
  755. {
  756. std::swap(minv, maxv);
  757. minv -= 1.0f;
  758. maxv -= 1.0f;
  759. }
  760. vertex[0].m_x = minx;
  761. vertex[0].m_y = miny;
  762. vertex[0].m_z = zz;
  763. vertex[0].m_rgba = 0xffffffff;
  764. vertex[0].m_u = minu;
  765. vertex[0].m_v = minv;
  766. vertex[1].m_x = maxx;
  767. vertex[1].m_y = miny;
  768. vertex[1].m_z = zz;
  769. vertex[1].m_rgba = 0xffffffff;
  770. vertex[1].m_u = maxu;
  771. vertex[1].m_v = minv;
  772. vertex[2].m_x = maxx;
  773. vertex[2].m_y = maxy;
  774. vertex[2].m_z = zz;
  775. vertex[2].m_rgba = 0xffffffff;
  776. vertex[2].m_u = maxu;
  777. vertex[2].m_v = maxv;
  778. bgfx::setVertexBuffer(0, &vb);
  779. }
  780. }
  781. void worldSpaceFrustumCorners(
  782. float* _corners24f
  783. , float _near
  784. , float _far
  785. , float _projWidth
  786. , float _projHeight
  787. , const float* _invViewMtx
  788. )
  789. {
  790. // Define frustum corners in view space.
  791. const float nw = _near * _projWidth;
  792. const float nh = _near * _projHeight;
  793. const float fw = _far * _projWidth;
  794. const float fh = _far * _projHeight;
  795. const uint8_t numCorners = 8;
  796. const bx::Vec3 corners[numCorners] =
  797. {
  798. { -nw, nh, _near },
  799. { nw, nh, _near },
  800. { nw, -nh, _near },
  801. { -nw, -nh, _near },
  802. { -fw, fh, _far },
  803. { fw, fh, _far },
  804. { fw, -fh, _far },
  805. { -fw, -fh, _far },
  806. };
  807. // Convert them to world space.
  808. float (*out)[3] = (float(*)[3])_corners24f;
  809. for (uint8_t ii = 0; ii < numCorners; ++ii)
  810. {
  811. bx::store(&out[ii], bx::mul(corners[ii], _invViewMtx) );
  812. }
  813. }
  814. /**
  815. * _splits = { near0, far0, near1, far1... nearN, farN }
  816. * N = _numSplits
  817. */
  818. void splitFrustum(float* _splits, uint8_t _numSplits, float _near, float _far, float _splitWeight = 0.75f)
  819. {
  820. const float l = _splitWeight;
  821. const float ratio = _far/_near;
  822. const int8_t numSlices = _numSplits*2;
  823. const float numSlicesf = float(numSlices);
  824. // First slice.
  825. _splits[0] = _near;
  826. for (uint8_t nn = 2, ff = 1; nn < numSlices; nn+=2, ff+=2)
  827. {
  828. float si = float(int8_t(ff) ) / numSlicesf;
  829. const float nearp = l*(_near*bx::pow(ratio, si) ) + (1 - l)*(_near + (_far - _near)*si);
  830. _splits[nn] = nearp; //near
  831. _splits[ff] = nearp * 1.005f; //far from previous split
  832. }
  833. // Last slice.
  834. _splits[numSlices-1] = _far;
  835. }
  836. struct Programs
  837. {
  838. void init()
  839. {
  840. // Misc.
  841. m_black = loadProgram("vs_shadowmaps_color", "fs_shadowmaps_color_black");
  842. m_texture = loadProgram("vs_shadowmaps_texture", "fs_shadowmaps_texture");
  843. m_colorTexture = loadProgram("vs_shadowmaps_color_texture", "fs_shadowmaps_color_texture");
  844. // Blur.
  845. m_vBlur[PackDepth::RGBA] = loadProgram("vs_shadowmaps_vblur", "fs_shadowmaps_vblur");
  846. m_hBlur[PackDepth::RGBA] = loadProgram("vs_shadowmaps_hblur", "fs_shadowmaps_hblur");
  847. m_vBlur[PackDepth::VSM] = loadProgram("vs_shadowmaps_vblur", "fs_shadowmaps_vblur_vsm");
  848. m_hBlur[PackDepth::VSM] = loadProgram("vs_shadowmaps_hblur", "fs_shadowmaps_hblur_vsm");
  849. // Draw depth.
  850. m_drawDepth[PackDepth::RGBA] = loadProgram("vs_shadowmaps_unpackdepth", "fs_shadowmaps_unpackdepth");
  851. m_drawDepth[PackDepth::VSM] = loadProgram("vs_shadowmaps_unpackdepth", "fs_shadowmaps_unpackdepth_vsm");
  852. // Pack depth.
  853. m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] = loadProgram("vs_shadowmaps_packdepth", "fs_shadowmaps_packdepth");
  854. m_packDepth[DepthImpl::InvZ][PackDepth::VSM] = loadProgram("vs_shadowmaps_packdepth", "fs_shadowmaps_packdepth_vsm");
  855. m_packDepth[DepthImpl::Linear][PackDepth::RGBA] = loadProgram("vs_shadowmaps_packdepth_linear", "fs_shadowmaps_packdepth_linear");
  856. m_packDepth[DepthImpl::Linear][PackDepth::VSM] = loadProgram("vs_shadowmaps_packdepth_linear", "fs_shadowmaps_packdepth_vsm_linear");
  857. // Color lighting.
  858. m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lighting", "fs_shadowmaps_color_lighting_hard");
  859. m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lighting", "fs_shadowmaps_color_lighting_pcf");
  860. m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lighting", "fs_shadowmaps_color_lighting_vsm");
  861. m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lighting", "fs_shadowmaps_color_lighting_esm");
  862. m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lighting_linear", "fs_shadowmaps_color_lighting_hard_linear");
  863. m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lighting_linear", "fs_shadowmaps_color_lighting_pcf_linear");
  864. m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lighting_linear", "fs_shadowmaps_color_lighting_vsm_linear");
  865. m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lighting_linear", "fs_shadowmaps_color_lighting_esm_linear");
  866. m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lighting_omni", "fs_shadowmaps_color_lighting_hard_omni");
  867. m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lighting_omni", "fs_shadowmaps_color_lighting_pcf_omni");
  868. m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lighting_omni", "fs_shadowmaps_color_lighting_vsm_omni");
  869. m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lighting_omni", "fs_shadowmaps_color_lighting_esm_omni");
  870. m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lighting_linear_omni", "fs_shadowmaps_color_lighting_hard_linear_omni");
  871. m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lighting_linear_omni", "fs_shadowmaps_color_lighting_pcf_linear_omni");
  872. m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lighting_linear_omni", "fs_shadowmaps_color_lighting_vsm_linear_omni");
  873. m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lighting_linear_omni", "fs_shadowmaps_color_lighting_esm_linear_omni");
  874. m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lighting_csm", "fs_shadowmaps_color_lighting_hard_csm");
  875. m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lighting_csm", "fs_shadowmaps_color_lighting_pcf_csm");
  876. m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lighting_csm", "fs_shadowmaps_color_lighting_vsm_csm");
  877. m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lighting_csm", "fs_shadowmaps_color_lighting_esm_csm");
  878. m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::Hard] = loadProgram("vs_shadowmaps_color_lighting_linear_csm", "fs_shadowmaps_color_lighting_hard_linear_csm");
  879. m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::PCF] = loadProgram("vs_shadowmaps_color_lighting_linear_csm", "fs_shadowmaps_color_lighting_pcf_linear_csm");
  880. m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::VSM] = loadProgram("vs_shadowmaps_color_lighting_linear_csm", "fs_shadowmaps_color_lighting_vsm_linear_csm");
  881. m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::ESM] = loadProgram("vs_shadowmaps_color_lighting_linear_csm", "fs_shadowmaps_color_lighting_esm_linear_csm");
  882. }
  883. void destroy()
  884. {
  885. // Color lighting.
  886. for (uint8_t ii = 0; ii < SmType::Count; ++ii)
  887. {
  888. for (uint8_t jj = 0; jj < DepthImpl::Count; ++jj)
  889. {
  890. for (uint8_t kk = 0; kk < SmImpl::Count; ++kk)
  891. {
  892. bgfx::destroy(m_colorLighting[ii][jj][kk]);
  893. }
  894. }
  895. }
  896. // Pack depth.
  897. for (uint8_t ii = 0; ii < DepthImpl::Count; ++ii)
  898. {
  899. for (uint8_t jj = 0; jj < PackDepth::Count; ++jj)
  900. {
  901. bgfx::destroy(m_packDepth[ii][jj]);
  902. }
  903. }
  904. // Draw depth.
  905. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  906. {
  907. bgfx::destroy(m_drawDepth[ii]);
  908. }
  909. // Hblur.
  910. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  911. {
  912. bgfx::destroy(m_hBlur[ii]);
  913. }
  914. // Vblur.
  915. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  916. {
  917. bgfx::destroy(m_vBlur[ii]);
  918. }
  919. // Misc.
  920. bgfx::destroy(m_colorTexture);
  921. bgfx::destroy(m_texture);
  922. bgfx::destroy(m_black);
  923. }
  924. bgfx::ProgramHandle m_black;
  925. bgfx::ProgramHandle m_texture;
  926. bgfx::ProgramHandle m_colorTexture;
  927. bgfx::ProgramHandle m_vBlur[PackDepth::Count];
  928. bgfx::ProgramHandle m_hBlur[PackDepth::Count];
  929. bgfx::ProgramHandle m_drawDepth[PackDepth::Count];
  930. bgfx::ProgramHandle m_packDepth[DepthImpl::Count][PackDepth::Count];
  931. bgfx::ProgramHandle m_colorLighting[SmType::Count][DepthImpl::Count][SmImpl::Count];
  932. };
  933. static Programs s_programs;
  934. struct ShadowMapSettings
  935. {
  936. #define IMGUI_FLOAT_PARAM(_name) float _name, _name##Min, _name##Max, _name##Step
  937. IMGUI_FLOAT_PARAM(m_sizePwrTwo);
  938. IMGUI_FLOAT_PARAM(m_depthValuePow);
  939. IMGUI_FLOAT_PARAM(m_near);
  940. IMGUI_FLOAT_PARAM(m_far);
  941. IMGUI_FLOAT_PARAM(m_bias);
  942. IMGUI_FLOAT_PARAM(m_normalOffset);
  943. IMGUI_FLOAT_PARAM(m_customParam0);
  944. IMGUI_FLOAT_PARAM(m_customParam1);
  945. IMGUI_FLOAT_PARAM(m_xNum);
  946. IMGUI_FLOAT_PARAM(m_yNum);
  947. IMGUI_FLOAT_PARAM(m_xOffset);
  948. IMGUI_FLOAT_PARAM(m_yOffset);
  949. bool m_doBlur;
  950. bgfx::ProgramHandle* m_progPack;
  951. bgfx::ProgramHandle* m_progDraw;
  952. #undef IMGUI_FLOAT_PARAM
  953. };
  954. struct SceneSettings
  955. {
  956. LightType::Enum m_lightType;
  957. DepthImpl::Enum m_depthImpl;
  958. SmImpl::Enum m_smImpl;
  959. float m_spotOuterAngle;
  960. float m_spotInnerAngle;
  961. float m_fovXAdjust;
  962. float m_fovYAdjust;
  963. float m_coverageSpotL;
  964. float m_splitDistribution;
  965. int m_numSplits;
  966. bool m_updateLights;
  967. bool m_updateScene;
  968. bool m_drawDepthBuffer;
  969. bool m_showSmCoverage;
  970. bool m_stencilPack;
  971. bool m_stabilize;
  972. };
  973. class ExampleShadowmaps : public entry::AppI
  974. {
  975. public:
  976. ExampleShadowmaps(const char* _name, const char* _description, const char* _url)
  977. : entry::AppI(_name, _description, _url)
  978. {
  979. }
  980. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  981. {
  982. Args args(_argc, _argv);
  983. m_debug = BGFX_DEBUG_NONE;
  984. m_reset = BGFX_RESET_VSYNC;
  985. m_width = _width;
  986. m_height = _height;
  987. m_viewState = ViewState(uint16_t(m_width), uint16_t(m_height));
  988. m_clearValues = ClearValues(0x00000000, 1.0f, 0);
  989. bgfx::Init init;
  990. init.type = args.m_type;
  991. init.vendorId = args.m_pciId;
  992. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  993. init.platformData.ndt = entry::getNativeDisplayHandle();
  994. init.platformData.type = entry::getNativeWindowHandleType(entry::kDefaultWindowHandle);
  995. init.resolution.width = m_viewState.m_width;
  996. init.resolution.height = m_viewState.m_height;
  997. init.resolution.reset = m_reset;
  998. bgfx::init(init);
  999. // Enable debug text.
  1000. bgfx::setDebug(m_debug);
  1001. s_originBottomLeft = bgfx::getCaps()->originBottomLeft;
  1002. // Imgui.
  1003. imguiCreate();
  1004. // Uniforms.
  1005. s_uniforms.init();
  1006. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  1007. s_shadowMap[0] = bgfx::createUniform("s_shadowMap0", bgfx::UniformType::Sampler);
  1008. s_shadowMap[1] = bgfx::createUniform("s_shadowMap1", bgfx::UniformType::Sampler);
  1009. s_shadowMap[2] = bgfx::createUniform("s_shadowMap2", bgfx::UniformType::Sampler);
  1010. s_shadowMap[3] = bgfx::createUniform("s_shadowMap3", bgfx::UniformType::Sampler);
  1011. // Programs.
  1012. s_programs.init();
  1013. // Vertex declarations.
  1014. bgfx::VertexLayout PosNormalTexcoordLayout;
  1015. PosNormalTexcoordLayout.begin()
  1016. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  1017. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  1018. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  1019. .end();
  1020. m_posLayout.begin();
  1021. m_posLayout.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  1022. m_posLayout.end();
  1023. PosColorTexCoord0Vertex::init();
  1024. // Textures.
  1025. m_texFigure = loadTexture("textures/figure-rgba.dds");
  1026. m_texFlare = loadTexture("textures/flare.dds");
  1027. m_texFieldstone = loadTexture("textures/fieldstone-rgba.dds");
  1028. // Meshes.
  1029. m_bunnyMesh.load("meshes/bunny.bin");
  1030. m_treeMesh.load("meshes/tree.bin");
  1031. m_cubeMesh.load("meshes/cube.bin");
  1032. m_hollowcubeMesh.load("meshes/hollowcube.bin");
  1033. m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordLayout, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  1034. m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordLayout, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  1035. // Materials.
  1036. m_defaultMaterial =
  1037. {
  1038. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //ambient
  1039. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //diffuse
  1040. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular, exponent
  1041. };
  1042. // Lights.
  1043. m_pointLight =
  1044. {
  1045. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //position
  1046. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1047. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //ambient
  1048. { { 1.0f, 1.0f, 1.0f, 850.0f } }, //diffuse
  1049. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular
  1050. { { 0.0f,-0.4f,-0.6f, 0.0f } }, //spotdirection, spotexponent
  1051. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1052. { { 1.0f, 0.0f, 1.0f, 91.0f } }, //attenuation, spotcutoff
  1053. };
  1054. m_directionalLight =
  1055. {
  1056. { { 0.5f,-1.0f, 0.1f, 0.0f } }, //position
  1057. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1058. { { 1.0f, 1.0f, 1.0f, 0.02f } }, //ambient
  1059. { { 1.0f, 1.0f, 1.0f, 0.4f } }, //diffuse
  1060. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular
  1061. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //spotdirection, spotexponent
  1062. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1063. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //attenuation, spotcutoff
  1064. };
  1065. // Setup uniforms.
  1066. m_color[0] = m_color[1] = m_color[2] = m_color[3] = 1.0f;
  1067. s_uniforms.setPtrs(&m_defaultMaterial
  1068. , &m_pointLight
  1069. , m_color
  1070. , m_lightMtx
  1071. , &m_shadowMapMtx[ShadowMapRenderTargets::First][0]
  1072. , &m_shadowMapMtx[ShadowMapRenderTargets::Second][0]
  1073. , &m_shadowMapMtx[ShadowMapRenderTargets::Third][0]
  1074. , &m_shadowMapMtx[ShadowMapRenderTargets::Fourth][0]
  1075. );
  1076. s_uniforms.submitConstUniforms();
  1077. // Settings.
  1078. ShadowMapSettings smSettings[LightType::Count][DepthImpl::Count][SmImpl::Count] =
  1079. {
  1080. { //LightType::Spot
  1081. { //DepthImpl::InvZ
  1082. { //SmImpl::Hard
  1083. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1084. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1085. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1086. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1087. , 0.0035f, 0.0f, 0.01f, 0.00001f // m_bias
  1088. , 0.0012f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1089. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1090. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1091. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1092. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1093. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1094. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1095. , true // m_doBlur
  1096. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1097. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1098. },
  1099. { //SmImpl::PCF
  1100. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1101. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1102. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1103. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1104. , 0.007f, 0.0f, 0.01f, 0.00001f // m_bias
  1105. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1106. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1107. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1108. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1109. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1110. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1111. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1112. , true // m_doBlur
  1113. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1114. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1115. },
  1116. { //SmImpl::VSM
  1117. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1118. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1119. , 8.0f, 1.0f, 10.0f, 1.0f // m_near
  1120. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1121. , 0.045f, 0.0f, 0.1f, 0.00001f // m_bias
  1122. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1123. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1124. , 450.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1125. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1126. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1127. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1128. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1129. , true // m_doBlur
  1130. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1131. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1132. },
  1133. { //SmImpl::ESM
  1134. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1135. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1136. , 3.0f, 1.0f, 10.0f, 0.01f // m_near
  1137. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1138. , 0.02f, 0.0f, 0.3f, 0.00001f // m_bias
  1139. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1140. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1141. , 9000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1142. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1143. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1144. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1145. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1146. , true // m_doBlur
  1147. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1148. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1149. }
  1150. },
  1151. { //DepthImpl::Linear
  1152. { //SmImpl::Hard
  1153. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1154. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1155. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1156. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1157. , 0.0025f, 0.0f, 0.01f, 0.00001f // m_bias
  1158. , 0.0012f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1159. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1160. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1161. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1162. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1163. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1164. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1165. , true // m_doBlur
  1166. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1167. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1168. },
  1169. { //SmImpl::PCF
  1170. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1171. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1172. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1173. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1174. , 0.0025f, 0.0f, 0.01f, 0.00001f // m_bias
  1175. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1176. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1177. , 2000.0f, 1.0f, 2000.0f, 1.0f // m_customParam1
  1178. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1179. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1180. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1181. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1182. , true // m_doBlur
  1183. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1184. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1185. },
  1186. { //SmImpl::VSM
  1187. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1188. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1189. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1190. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1191. , 0.006f, 0.0f, 0.01f, 0.00001f // m_bias
  1192. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1193. , 0.02f, 0.0f, 0.1f, 0.00001f // m_customParam0
  1194. , 300.0f, 1.0f, 1500.0f, 1.0f // m_customParam1
  1195. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1196. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1197. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1198. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1199. , true // m_doBlur
  1200. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1201. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1202. },
  1203. { //SmImpl::ESM
  1204. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1205. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1206. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1207. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1208. , 0.0055f, 0.0f, 0.01f, 0.00001f // m_bias
  1209. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1210. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1211. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1212. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1213. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1214. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1215. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1216. , true // m_doBlur
  1217. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1218. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1219. }
  1220. }
  1221. },
  1222. { //LightType::Point
  1223. { //DepthImpl::InvZ
  1224. { //SmImpl::Hard
  1225. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1226. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1227. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1228. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1229. , 0.006f, 0.0f, 0.01f, 0.00001f // m_bias
  1230. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1231. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1232. , 50.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1233. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1234. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1235. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1236. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1237. , true // m_doBlur
  1238. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1239. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1240. },
  1241. { //SmImpl::PCF
  1242. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1243. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1244. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1245. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1246. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1247. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1248. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1249. , 50.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1250. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1251. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1252. , 1.0f, 0.0f, 3.0f, 0.001f // m_xOffset
  1253. , 1.0f, 0.0f, 3.0f, 0.001f // m_yOffset
  1254. , true // m_doBlur
  1255. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1256. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1257. },
  1258. { //SmImpl::VSM
  1259. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1260. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1261. , 8.0f, 1.0f, 10.0f, 1.0f // m_near
  1262. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1263. , 0.055f, 0.0f, 0.1f, 0.00001f // m_bias
  1264. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1265. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1266. , 450.0f, 1.0f, 900.0f, 1.0f // m_customParam1
  1267. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1268. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1269. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1270. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1271. , true // m_doBlur
  1272. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1273. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1274. },
  1275. { //SmImpl::ESM
  1276. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1277. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1278. , 3.0f, 1.0f, 10.0f, 0.01f // m_near
  1279. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1280. , 0.035f, 0.0f, 0.1f, 0.00001f // m_bias
  1281. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1282. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1283. , 9000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1284. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1285. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1286. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1287. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1288. , true // m_doBlur
  1289. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1290. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1291. }
  1292. },
  1293. { //DepthImpl::Linear
  1294. { //SmImpl::Hard
  1295. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1296. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1297. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1298. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1299. , 0.003f, 0.0f, 0.01f, 0.00001f // m_bias
  1300. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1301. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1302. , 120.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1303. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1304. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1305. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1306. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1307. , true // m_doBlur
  1308. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1309. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1310. },
  1311. { //SmImpl::PCF
  1312. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1313. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1314. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1315. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1316. , 0.0035f, 0.0f, 0.01f, 0.00001f // m_bias
  1317. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1318. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1319. , 120.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1320. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1321. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1322. , 1.0f, 0.0f, 3.0f, 0.001f // m_xOffset
  1323. , 1.0f, 0.0f, 3.0f, 0.001f // m_yOffset
  1324. , true // m_doBlur
  1325. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1326. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1327. },
  1328. { //SmImpl::VSM
  1329. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1330. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1331. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1332. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1333. , 0.006f, 0.0f, 0.1f, 0.00001f // m_bias
  1334. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1335. , 0.02f, 0.0f, 0.1f, 0.00001f // m_customParam0
  1336. , 400.0f, 1.0f, 900.0f, 1.0f // m_customParam1
  1337. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1338. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1339. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1340. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1341. , true // m_doBlur
  1342. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1343. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1344. },
  1345. { //SmImpl::ESM
  1346. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1347. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1348. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1349. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1350. , 0.007f, 0.0f, 0.01f, 0.00001f // m_bias
  1351. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1352. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1353. , 8000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1354. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1355. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1356. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1357. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1358. , true // m_doBlur
  1359. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1360. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1361. }
  1362. }
  1363. },
  1364. { //LightType::Directional
  1365. { //DepthImpl::InvZ
  1366. { //SmImpl::Hard
  1367. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1368. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1369. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1370. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1371. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1372. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1373. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1374. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1375. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1376. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1377. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1378. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1379. , true // m_doBlur
  1380. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1381. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1382. },
  1383. { //SmImpl::PCF
  1384. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1385. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1386. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1387. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1388. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1389. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1390. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1391. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1392. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1393. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1394. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1395. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1396. , true // m_doBlur
  1397. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1398. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1399. },
  1400. { //SmImpl::VSM
  1401. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1402. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1403. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1404. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1405. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1406. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1407. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1408. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1409. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1410. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1411. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1412. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1413. , true // m_doBlur
  1414. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1415. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1416. },
  1417. { //SmImpl::ESM
  1418. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1419. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1420. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1421. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1422. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1423. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1424. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1425. , 9500.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1426. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1427. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1428. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1429. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1430. , true // m_doBlur
  1431. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1432. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1433. }
  1434. },
  1435. { //DepthImpl::Linear
  1436. { //SmImpl::Hard
  1437. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1438. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1439. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1440. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1441. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1442. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1443. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1444. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1445. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1446. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1447. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1448. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1449. , true // m_doBlur
  1450. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1451. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1452. },
  1453. { //SmImpl::PCF
  1454. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1455. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1456. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1457. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1458. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1459. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1460. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1461. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1462. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1463. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1464. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1465. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1466. , true // m_doBlur
  1467. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1468. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1469. },
  1470. { //SmImpl::VSM
  1471. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1472. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1473. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1474. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1475. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1476. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1477. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1478. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1479. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1480. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1481. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1482. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1483. , true // m_doBlur
  1484. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1485. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1486. },
  1487. { //SmImpl::ESM
  1488. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1489. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1490. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1491. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1492. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1493. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1494. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1495. , 9500.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1496. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1497. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1498. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1499. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1500. , true // m_doBlur
  1501. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1502. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1503. }
  1504. }
  1505. }
  1506. };
  1507. bx::memCopy(m_smSettings, smSettings, sizeof(smSettings));
  1508. m_settings.m_lightType = LightType::SpotLight;
  1509. m_settings.m_depthImpl = DepthImpl::InvZ;
  1510. m_settings.m_smImpl = SmImpl::Hard;
  1511. m_settings.m_spotOuterAngle = 45.0f;
  1512. m_settings.m_spotInnerAngle = 30.0f;
  1513. m_settings.m_fovXAdjust = 0.0f;
  1514. m_settings.m_fovYAdjust = 0.0f;
  1515. m_settings.m_coverageSpotL = 90.0f;
  1516. m_settings.m_splitDistribution = 0.6f;
  1517. m_settings.m_numSplits = 4;
  1518. m_settings.m_updateLights = true;
  1519. m_settings.m_updateScene = true;
  1520. m_settings.m_drawDepthBuffer = false;
  1521. m_settings.m_showSmCoverage = false;
  1522. m_settings.m_stencilPack = true;
  1523. m_settings.m_stabilize = true;
  1524. ShadowMapSettings* currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1525. // Render targets.
  1526. uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo);
  1527. m_currentShadowMapSize = shadowMapSize;
  1528. float currentShadowMapSizef = float(int16_t(m_currentShadowMapSize) );
  1529. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  1530. for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
  1531. {
  1532. bgfx::TextureHandle fbtextures[] =
  1533. {
  1534. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  1535. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D32F, BGFX_TEXTURE_RT),
  1536. };
  1537. s_rtShadowMap[ii] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  1538. }
  1539. s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8);
  1540. // Setup camera.
  1541. cameraCreate();
  1542. cameraSetPosition({ 0.0f, 60.0f, -105.0f });
  1543. cameraSetVerticalAngle(-0.45f);
  1544. m_timeAccumulatorLight = 0.0f;
  1545. m_timeAccumulatorScene = 0.0f;
  1546. }
  1547. virtual int shutdown() override
  1548. {
  1549. m_bunnyMesh.unload();
  1550. m_treeMesh.unload();
  1551. m_cubeMesh.unload();
  1552. m_hollowcubeMesh.unload();
  1553. m_hplaneMesh.unload();
  1554. m_vplaneMesh.unload();
  1555. bgfx::destroy(m_texFigure);
  1556. bgfx::destroy(m_texFieldstone);
  1557. bgfx::destroy(m_texFlare);
  1558. for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
  1559. {
  1560. bgfx::destroy(s_rtShadowMap[ii]);
  1561. }
  1562. bgfx::destroy(s_rtBlur);
  1563. s_programs.destroy();
  1564. bgfx::destroy(s_texColor);
  1565. bgfx::destroy(s_shadowMap[3]);
  1566. bgfx::destroy(s_shadowMap[2]);
  1567. bgfx::destroy(s_shadowMap[1]);
  1568. bgfx::destroy(s_shadowMap[0]);
  1569. s_uniforms.destroy();
  1570. cameraDestroy();
  1571. imguiDestroy();
  1572. // Shutdown bgfx.
  1573. bgfx::shutdown();
  1574. return 0;
  1575. }
  1576. bool update() override
  1577. {
  1578. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  1579. {
  1580. m_viewState.m_width = uint16_t(m_width);
  1581. m_viewState.m_height = uint16_t(m_height);
  1582. const bgfx::Caps* caps = bgfx::getCaps();
  1583. // Set view and projection matrices.
  1584. const float camFovy = 60.0f;
  1585. const float camAspect = float(int32_t(m_viewState.m_width) ) / float(int32_t(m_viewState.m_height) );
  1586. const float camNear = 0.1f;
  1587. const float camFar = 2000.0f;
  1588. const float projHeight = bx::tan(bx::toRad(camFovy)*0.5f);
  1589. const float projWidth = projHeight * camAspect;
  1590. bx::mtxProj(m_viewState.m_proj, camFovy, camAspect, camNear, camFar, caps->homogeneousDepth);
  1591. cameraGetViewMtx(m_viewState.m_view);
  1592. float currentShadowMapSizef = float(int16_t(m_currentShadowMapSize) );
  1593. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  1594. s_uniforms.submitConstUniforms();
  1595. // s_uniforms.submitConstUniforms();
  1596. // Imgui.
  1597. imguiBeginFrame(m_mouseState.m_mx
  1598. , m_mouseState.m_my
  1599. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1600. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1601. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1602. , m_mouseState.m_mz
  1603. , m_viewState.m_width
  1604. , m_viewState.m_height
  1605. );
  1606. showExampleDialog(this);
  1607. ImGui::SetNextWindowPos(
  1608. ImVec2(m_viewState.m_width - m_viewState.m_width / 5.0f - 10.0f, 10.0f)
  1609. , ImGuiCond_FirstUseEver
  1610. );
  1611. ImGui::SetNextWindowSize(
  1612. ImVec2(m_viewState.m_width / 5.0f, m_viewState.m_height - 20.0f)
  1613. , ImGuiCond_FirstUseEver
  1614. );
  1615. ImGui::Begin("Settings"
  1616. , NULL
  1617. , 0
  1618. );
  1619. #define IMGUI_FLOAT_SLIDER(_name, _val) \
  1620. ImGui::SliderFloat(_name \
  1621. , &_val \
  1622. , *( ((float*)&_val)+1) \
  1623. , *( ((float*)&_val)+2) \
  1624. )
  1625. #define IMGUI_RADIO_BUTTON(_name, _var, _val) \
  1626. if (ImGui::RadioButton(_name, _var == _val) ) \
  1627. { \
  1628. _var = _val; \
  1629. }
  1630. ImGui::Checkbox("Update lights", &m_settings.m_updateLights);
  1631. ImGui::Checkbox("Update scene", &m_settings.m_updateScene);
  1632. ImGui::Separator();
  1633. ImGui::Text("Shadow map depth:");
  1634. IMGUI_RADIO_BUTTON("InvZ", m_settings.m_depthImpl, DepthImpl::InvZ);
  1635. IMGUI_RADIO_BUTTON("Linear", m_settings.m_depthImpl, DepthImpl::Linear);
  1636. ShadowMapSettings* currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1637. ImGui::Separator();
  1638. ImGui::Checkbox("Draw depth buffer", &m_settings.m_drawDepthBuffer);
  1639. if (m_settings.m_drawDepthBuffer)
  1640. {
  1641. IMGUI_FLOAT_SLIDER("Depth value pow", currentSmSettings->m_depthValuePow);
  1642. }
  1643. ImGui::Separator();
  1644. ImGui::Text("Shadow Map implementation");
  1645. IMGUI_RADIO_BUTTON("Hard", m_settings.m_smImpl, SmImpl::Hard);
  1646. IMGUI_RADIO_BUTTON("PCF", m_settings.m_smImpl, SmImpl::PCF);
  1647. IMGUI_RADIO_BUTTON("VSM", m_settings.m_smImpl, SmImpl::VSM);
  1648. IMGUI_RADIO_BUTTON("ESM", m_settings.m_smImpl, SmImpl::ESM);
  1649. currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1650. ImGui::Separator();
  1651. IMGUI_FLOAT_SLIDER("Bias", currentSmSettings->m_bias);
  1652. IMGUI_FLOAT_SLIDER("Normal offset", currentSmSettings->m_normalOffset);
  1653. ImGui::Separator();
  1654. if (LightType::DirectionalLight != m_settings.m_lightType)
  1655. {
  1656. IMGUI_FLOAT_SLIDER("Near plane", currentSmSettings->m_near);
  1657. }
  1658. IMGUI_FLOAT_SLIDER("Far plane", currentSmSettings->m_far);
  1659. ImGui::Separator();
  1660. switch(m_settings.m_smImpl)
  1661. {
  1662. case SmImpl::Hard:
  1663. //ImGui::Text("Hard");
  1664. break;
  1665. case SmImpl::PCF:
  1666. ImGui::Text("PCF");
  1667. IMGUI_FLOAT_SLIDER("X Offset", currentSmSettings->m_xOffset);
  1668. IMGUI_FLOAT_SLIDER("Y Offset", currentSmSettings->m_yOffset);
  1669. break;
  1670. case SmImpl::VSM:
  1671. ImGui::Text("VSM");
  1672. IMGUI_FLOAT_SLIDER("Min variance", currentSmSettings->m_customParam0);
  1673. IMGUI_FLOAT_SLIDER("Depth multiplier", currentSmSettings->m_customParam1);
  1674. ImGui::Checkbox("Blur shadow map", &currentSmSettings->m_doBlur);
  1675. if (currentSmSettings->m_doBlur)
  1676. {
  1677. IMGUI_FLOAT_SLIDER("Blur X Offset", currentSmSettings->m_xOffset);
  1678. IMGUI_FLOAT_SLIDER("Blur Y Offset", currentSmSettings->m_yOffset);
  1679. }
  1680. break;
  1681. case SmImpl::ESM:
  1682. ImGui::Text("ESM");
  1683. IMGUI_FLOAT_SLIDER("ESM Hardness", currentSmSettings->m_customParam0);
  1684. IMGUI_FLOAT_SLIDER("Depth multiplier", currentSmSettings->m_customParam1);
  1685. ImGui::Checkbox("Blur shadow map", &currentSmSettings->m_doBlur);
  1686. if (currentSmSettings->m_doBlur)
  1687. {
  1688. IMGUI_FLOAT_SLIDER("Blur X Offset", currentSmSettings->m_xOffset);
  1689. IMGUI_FLOAT_SLIDER("Blur Y Offset", currentSmSettings->m_yOffset);
  1690. }
  1691. break;
  1692. default:
  1693. break;
  1694. };
  1695. ImGui::End();
  1696. #undef IMGUI_RADIO_BUTTON
  1697. ImGui::SetNextWindowPos(
  1698. ImVec2(10.0f, 260.0f)
  1699. , ImGuiCond_FirstUseEver
  1700. );
  1701. ImGui::SetNextWindowSize(
  1702. ImVec2(m_viewState.m_width / 5.0f, 350.0f)
  1703. , ImGuiCond_FirstUseEver
  1704. );
  1705. ImGui::Begin("Light"
  1706. , NULL
  1707. , 0
  1708. );
  1709. ImGui::PushItemWidth(185.0f);
  1710. bool bLtChanged = false;
  1711. if ( ImGui::RadioButton("Spot light", m_settings.m_lightType == LightType::SpotLight ))
  1712. {
  1713. m_settings.m_lightType = LightType::SpotLight; bLtChanged = true;
  1714. }
  1715. if ( ImGui::RadioButton("Point light", m_settings.m_lightType == LightType::PointLight ))
  1716. {
  1717. m_settings.m_lightType = LightType::PointLight; bLtChanged = true;
  1718. }
  1719. if ( ImGui::RadioButton("Directional light", m_settings.m_lightType == LightType::DirectionalLight ))
  1720. {
  1721. m_settings.m_lightType = LightType::DirectionalLight; bLtChanged = true;
  1722. }
  1723. ImGui::Separator();
  1724. ImGui::Checkbox("Show shadow map coverage.", &m_settings.m_showSmCoverage);
  1725. ImGui::Separator();
  1726. ImGui::Text("Shadow map resolution: %ux%u", m_currentShadowMapSize, m_currentShadowMapSize);
  1727. ImGui::SliderFloat("##SizePwrTwo", &currentSmSettings->m_sizePwrTwo,
  1728. currentSmSettings->m_sizePwrTwoMin,
  1729. currentSmSettings->m_sizePwrTwoMax, "%.0f");
  1730. ImGui::Separator();
  1731. if (LightType::SpotLight == m_settings.m_lightType)
  1732. {
  1733. ImGui::Text("Spot light");
  1734. ImGui::SliderFloat("Shadow map area", &m_settings.m_coverageSpotL, 45.0f, 120.0f);
  1735. ImGui::Separator();
  1736. ImGui::SliderFloat("Spot outer cone", &m_settings.m_spotOuterAngle, 0.0f, 91.0f);
  1737. ImGui::SliderFloat("Spot inner cone", &m_settings.m_spotInnerAngle, 0.0f, 90.0f);
  1738. }
  1739. else if (LightType::PointLight == m_settings.m_lightType)
  1740. {
  1741. ImGui::Text("Point light");
  1742. ImGui::Checkbox("Stencil pack", &m_settings.m_stencilPack);
  1743. ImGui::SliderFloat("Fov X adjust", &m_settings.m_fovXAdjust, -20.0f, 20.0f);
  1744. ImGui::SliderFloat("Fov Y adjust", &m_settings.m_fovYAdjust, -20.0f, 20.0f);
  1745. }
  1746. else if (LightType::DirectionalLight == m_settings.m_lightType)
  1747. {
  1748. ImGui::Text("Directional light");
  1749. ImGui::Checkbox("Stabilize cascades", &m_settings.m_stabilize);
  1750. ImGui::SliderInt("Cascade splits", &m_settings.m_numSplits, 1, 4);
  1751. ImGui::SliderFloat("Cascade distribution", &m_settings.m_splitDistribution, 0.0f, 1.0f);
  1752. }
  1753. #undef IMGUI_FLOAT_SLIDER
  1754. ImGui::End();
  1755. imguiEndFrame();
  1756. // Update uniforms.
  1757. s_uniforms.m_shadowMapBias = currentSmSettings->m_bias;
  1758. s_uniforms.m_shadowMapOffset = currentSmSettings->m_normalOffset;
  1759. s_uniforms.m_shadowMapParam0 = currentSmSettings->m_customParam0;
  1760. s_uniforms.m_shadowMapParam1 = currentSmSettings->m_customParam1;
  1761. s_uniforms.m_depthValuePow = currentSmSettings->m_depthValuePow;
  1762. s_uniforms.m_XNum = currentSmSettings->m_xNum;
  1763. s_uniforms.m_YNum = currentSmSettings->m_yNum;
  1764. s_uniforms.m_XOffset = currentSmSettings->m_xOffset;
  1765. s_uniforms.m_YOffset = currentSmSettings->m_yOffset;
  1766. s_uniforms.m_showSmCoverage = float(m_settings.m_showSmCoverage);
  1767. s_uniforms.m_lightPtr = (LightType::DirectionalLight == m_settings.m_lightType) ? &m_directionalLight : &m_pointLight;
  1768. if (LightType::SpotLight == m_settings.m_lightType)
  1769. {
  1770. m_pointLight.m_attenuationSpotOuter.m_outer = m_settings.m_spotOuterAngle;
  1771. m_pointLight.m_spotDirectionInner.m_inner = m_settings.m_spotInnerAngle;
  1772. }
  1773. else
  1774. {
  1775. m_pointLight.m_attenuationSpotOuter.m_outer = 91.0f; //above 90.0f means point light
  1776. }
  1777. s_uniforms.submitPerFrameUniforms();
  1778. // Time.
  1779. int64_t now = bx::getHPCounter();
  1780. static int64_t last = now;
  1781. const int64_t frameTime = now - last;
  1782. last = now;
  1783. const double freq = double(bx::getHPFrequency() );
  1784. const float deltaTime = float(frameTime/freq);
  1785. // Update camera.
  1786. cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
  1787. // Update view mtx.
  1788. cameraGetViewMtx(m_viewState.m_view);
  1789. // Update lights.
  1790. m_pointLight.computeViewSpaceComponents(m_viewState.m_view);
  1791. m_directionalLight.computeViewSpaceComponents(m_viewState.m_view);
  1792. // Update time accumulators.
  1793. if (m_settings.m_updateLights) { m_timeAccumulatorLight += deltaTime; }
  1794. if (m_settings.m_updateScene) { m_timeAccumulatorScene += deltaTime; }
  1795. // Setup lights.
  1796. m_pointLight.m_position.m_x = bx::cos(m_timeAccumulatorLight) * 20.0f;
  1797. m_pointLight.m_position.m_y = 26.0f;
  1798. m_pointLight.m_position.m_z = bx::sin(m_timeAccumulatorLight) * 20.0f;
  1799. m_pointLight.m_spotDirectionInner.m_x = -m_pointLight.m_position.m_x;
  1800. m_pointLight.m_spotDirectionInner.m_y = -m_pointLight.m_position.m_y;
  1801. m_pointLight.m_spotDirectionInner.m_z = -m_pointLight.m_position.m_z;
  1802. m_directionalLight.m_position.m_x = -bx::cos(m_timeAccumulatorLight);
  1803. m_directionalLight.m_position.m_y = -1.0f;
  1804. m_directionalLight.m_position.m_z = -bx::sin(m_timeAccumulatorLight);
  1805. // Setup instance matrices.
  1806. float mtxFloor[16];
  1807. const float floorScale = 550.0f;
  1808. bx::mtxSRT(mtxFloor
  1809. , floorScale //scaleX
  1810. , floorScale //scaleY
  1811. , floorScale //scaleZ
  1812. , 0.0f //rotX
  1813. , 0.0f //rotY
  1814. , 0.0f //rotZ
  1815. , 0.0f //translateX
  1816. , 0.0f //translateY
  1817. , 0.0f //translateZ
  1818. );
  1819. float mtxBunny[16];
  1820. bx::mtxSRT(mtxBunny
  1821. , 5.0f
  1822. , 5.0f
  1823. , 5.0f
  1824. , 0.0f
  1825. , 1.56f - m_timeAccumulatorScene
  1826. , 0.0f
  1827. , 15.0f
  1828. , 5.0f
  1829. , 0.0f
  1830. );
  1831. float mtxHollowcube[16];
  1832. bx::mtxSRT(mtxHollowcube
  1833. , 2.5f
  1834. , 2.5f
  1835. , 2.5f
  1836. , 0.0f
  1837. , 1.56f - m_timeAccumulatorScene
  1838. , 0.0f
  1839. , 0.0f
  1840. , 10.0f
  1841. , 0.0f
  1842. );
  1843. float mtxCube[16];
  1844. bx::mtxSRT(mtxCube
  1845. , 2.5f
  1846. , 2.5f
  1847. , 2.5f
  1848. , 0.0f
  1849. , 1.56f - m_timeAccumulatorScene
  1850. , 0.0f
  1851. , -15.0f
  1852. , 5.0f
  1853. , 0.0f
  1854. );
  1855. const uint8_t numTrees = 10;
  1856. float mtxTrees[numTrees][16];
  1857. for (uint8_t ii = 0; ii < numTrees; ++ii)
  1858. {
  1859. bx::mtxSRT(mtxTrees[ii]
  1860. , 2.0f
  1861. , 2.0f
  1862. , 2.0f
  1863. , 0.0f
  1864. , float(ii)
  1865. , 0.0f
  1866. , bx::sin(float(ii)*2.0f*bx::kPi/float(numTrees) ) * 60.0f
  1867. , 0.0f
  1868. , bx::cos(float(ii)*2.0f*bx::kPi/float(numTrees) ) * 60.0f
  1869. );
  1870. }
  1871. // Compute transform matrices.
  1872. const uint8_t shadowMapPasses = ShadowMapRenderTargets::Count;
  1873. float lightView[shadowMapPasses][16];
  1874. float lightProj[shadowMapPasses][16];
  1875. float mtxYpr[TetrahedronFaces::Count][16];
  1876. float screenProj[16];
  1877. float screenView[16];
  1878. bx::mtxIdentity(screenView);
  1879. bx::mtxOrtho(
  1880. screenProj
  1881. , 0.0f
  1882. , 1.0f
  1883. , 1.0f
  1884. , 0.0f
  1885. , 0.0f
  1886. , 100.0f
  1887. , 0.0f
  1888. , caps->homogeneousDepth
  1889. );
  1890. // Update render target size.
  1891. uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo);
  1892. if (bLtChanged || m_currentShadowMapSize != shadowMapSize)
  1893. {
  1894. m_currentShadowMapSize = shadowMapSize;
  1895. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  1896. {
  1897. bgfx::destroy(s_rtShadowMap[0]);
  1898. bgfx::TextureHandle fbtextures[] =
  1899. {
  1900. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  1901. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT),
  1902. };
  1903. s_rtShadowMap[0] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  1904. }
  1905. if (LightType::DirectionalLight == m_settings.m_lightType)
  1906. {
  1907. for (uint8_t ii = 1; ii < ShadowMapRenderTargets::Count; ++ii)
  1908. {
  1909. {
  1910. bgfx::destroy(s_rtShadowMap[ii]);
  1911. bgfx::TextureHandle fbtextures[] =
  1912. {
  1913. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  1914. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT),
  1915. };
  1916. s_rtShadowMap[ii] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  1917. }
  1918. }
  1919. }
  1920. bgfx::destroy(s_rtBlur);
  1921. s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8);
  1922. }
  1923. if (LightType::SpotLight == m_settings.m_lightType)
  1924. {
  1925. const float fovy = m_settings.m_coverageSpotL;
  1926. const float aspect = 1.0f;
  1927. bx::mtxProj(
  1928. lightProj[ProjType::Horizontal]
  1929. , fovy
  1930. , aspect
  1931. , currentSmSettings->m_near
  1932. , currentSmSettings->m_far
  1933. , false
  1934. );
  1935. //For linear depth, prevent depth division by variable w-component in shaders and divide here by far plane
  1936. if (DepthImpl::Linear == m_settings.m_depthImpl)
  1937. {
  1938. lightProj[ProjType::Horizontal][10] /= currentSmSettings->m_far;
  1939. lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far;
  1940. }
  1941. const bx::Vec3 at = bx::add(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(m_pointLight.m_spotDirectionInner.m_v) );
  1942. bx::mtxLookAt(lightView[TetrahedronFaces::Green], bx::load<bx::Vec3>(m_pointLight.m_position.m_v), at);
  1943. }
  1944. else if (LightType::PointLight == m_settings.m_lightType)
  1945. {
  1946. float ypr[TetrahedronFaces::Count][3] =
  1947. {
  1948. { bx::toRad( 0.0f), bx::toRad( 27.36780516f), bx::toRad(0.0f) },
  1949. { bx::toRad(180.0f), bx::toRad( 27.36780516f), bx::toRad(0.0f) },
  1950. { bx::toRad(-90.0f), bx::toRad(-27.36780516f), bx::toRad(0.0f) },
  1951. { bx::toRad( 90.0f), bx::toRad(-27.36780516f), bx::toRad(0.0f) },
  1952. };
  1953. if (m_settings.m_stencilPack)
  1954. {
  1955. const float fovx = 143.98570868f + 3.51f + m_settings.m_fovXAdjust;
  1956. const float fovy = 125.26438968f + 9.85f + m_settings.m_fovYAdjust;
  1957. const float aspect = bx::tan(bx::toRad(fovx*0.5f) )/bx::tan(bx::toRad(fovy*0.5f) );
  1958. bx::mtxProj(
  1959. lightProj[ProjType::Vertical]
  1960. , fovx
  1961. , aspect
  1962. , currentSmSettings->m_near
  1963. , currentSmSettings->m_far
  1964. , false
  1965. );
  1966. //For linear depth, prevent depth division by variable w-component in shaders and divide here by far plane
  1967. if (DepthImpl::Linear == m_settings.m_depthImpl)
  1968. {
  1969. lightProj[ProjType::Vertical][10] /= currentSmSettings->m_far;
  1970. lightProj[ProjType::Vertical][14] /= currentSmSettings->m_far;
  1971. }
  1972. ypr[TetrahedronFaces::Green ][2] = bx::toRad(180.0f);
  1973. ypr[TetrahedronFaces::Yellow][2] = bx::toRad( 0.0f);
  1974. ypr[TetrahedronFaces::Blue ][2] = bx::toRad( 90.0f);
  1975. ypr[TetrahedronFaces::Red ][2] = bx::toRad(-90.0f);
  1976. }
  1977. const float fovx = 143.98570868f + 7.8f + m_settings.m_fovXAdjust;
  1978. const float fovy = 125.26438968f + 3.0f + m_settings.m_fovYAdjust;
  1979. const float aspect = bx::tan(bx::toRad(fovx*0.5f) )/bx::tan(bx::toRad(fovy*0.5f) );
  1980. bx::mtxProj(
  1981. lightProj[ProjType::Horizontal]
  1982. , fovy
  1983. , aspect
  1984. , currentSmSettings->m_near
  1985. , currentSmSettings->m_far
  1986. , caps->homogeneousDepth
  1987. );
  1988. //For linear depth, prevent depth division by variable w component in shaders and divide here by far plane
  1989. if (DepthImpl::Linear == m_settings.m_depthImpl)
  1990. {
  1991. lightProj[ProjType::Horizontal][10] /= currentSmSettings->m_far;
  1992. lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far;
  1993. }
  1994. for (uint8_t ii = 0; ii < TetrahedronFaces::Count; ++ii)
  1995. {
  1996. float mtxTmp[16];
  1997. mtxYawPitchRoll(mtxTmp, ypr[ii][0], ypr[ii][1], ypr[ii][2]);
  1998. float tmp[3] =
  1999. {
  2000. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[0]) ),
  2001. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[4]) ),
  2002. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[8]) ),
  2003. };
  2004. bx::mtxTranspose(mtxYpr[ii], mtxTmp);
  2005. bx::memCopy(lightView[ii], mtxYpr[ii], 12*sizeof(float) );
  2006. lightView[ii][12] = tmp[0];
  2007. lightView[ii][13] = tmp[1];
  2008. lightView[ii][14] = tmp[2];
  2009. lightView[ii][15] = 1.0f;
  2010. }
  2011. }
  2012. else // LightType::DirectionalLight == settings.m_lightType
  2013. {
  2014. // Setup light view mtx.
  2015. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  2016. const bx::Vec3 eye =
  2017. {
  2018. -m_directionalLight.m_position.m_x,
  2019. -m_directionalLight.m_position.m_y,
  2020. -m_directionalLight.m_position.m_z,
  2021. };
  2022. bx::mtxLookAt(lightView[0], eye, at);
  2023. // Compute camera inverse view mtx.
  2024. float mtxViewInv[16];
  2025. bx::mtxInverse(mtxViewInv, m_viewState.m_view);
  2026. // Compute split distances.
  2027. const uint8_t maxNumSplits = 4;
  2028. BX_ASSERT(maxNumSplits >= m_settings.m_numSplits, "Error! Max num splits.");
  2029. float splitSlices[maxNumSplits*2];
  2030. splitFrustum(splitSlices
  2031. , uint8_t(m_settings.m_numSplits)
  2032. , currentSmSettings->m_near
  2033. , currentSmSettings->m_far
  2034. , m_settings.m_splitDistribution
  2035. );
  2036. // Update uniforms.
  2037. for (uint8_t ii = 0, ff = 1; ii < m_settings.m_numSplits; ++ii, ff+=2)
  2038. {
  2039. // This lags for 1 frame, but it's not a problem.
  2040. s_uniforms.m_csmFarDistances[ii] = splitSlices[ff];
  2041. }
  2042. float mtxProj[16];
  2043. bx::mtxOrtho(
  2044. mtxProj
  2045. , 1.0f
  2046. , -1.0f
  2047. , 1.0f
  2048. , -1.0f
  2049. , -currentSmSettings->m_far
  2050. , currentSmSettings->m_far
  2051. , 0.0f
  2052. , caps->homogeneousDepth
  2053. );
  2054. const uint8_t numCorners = 8;
  2055. float frustumCorners[maxNumSplits][numCorners][3];
  2056. for (uint8_t ii = 0, nn = 0, ff = 1; ii < m_settings.m_numSplits; ++ii, nn+=2, ff+=2)
  2057. {
  2058. // Compute frustum corners for one split in world space.
  2059. worldSpaceFrustumCorners( (float*)frustumCorners[ii], splitSlices[nn], splitSlices[ff], projWidth, projHeight, mtxViewInv);
  2060. bx::Vec3 min = { 9000.0f, 9000.0f, 9000.0f };
  2061. bx::Vec3 max = { -9000.0f, -9000.0f, -9000.0f };
  2062. for (uint8_t jj = 0; jj < numCorners; ++jj)
  2063. {
  2064. // Transform to light space.
  2065. const bx::Vec3 xyz = bx::mul(bx::load<bx::Vec3>(frustumCorners[ii][jj]), lightView[0]);
  2066. // Update bounding box.
  2067. min = bx::min(min, xyz);
  2068. max = bx::max(max, xyz);
  2069. }
  2070. const bx::Vec3 minproj = bx::mulH(min, mtxProj);
  2071. const bx::Vec3 maxproj = bx::mulH(max, mtxProj);
  2072. float scalex = 2.0f / (maxproj.x - minproj.x);
  2073. float scaley = 2.0f / (maxproj.y - minproj.y);
  2074. if (m_settings.m_stabilize)
  2075. {
  2076. const float quantizer = 64.0f;
  2077. scalex = quantizer / bx::ceil(quantizer / scalex);
  2078. scaley = quantizer / bx::ceil(quantizer / scaley);
  2079. }
  2080. float offsetx = 0.5f * (maxproj.x + minproj.x) * scalex;
  2081. float offsety = 0.5f * (maxproj.y + minproj.y) * scaley;
  2082. if (m_settings.m_stabilize)
  2083. {
  2084. const float halfSize = currentShadowMapSizef * 0.5f;
  2085. offsetx = bx::ceil(offsetx * halfSize) / halfSize;
  2086. offsety = bx::ceil(offsety * halfSize) / halfSize;
  2087. }
  2088. float mtxCrop[16];
  2089. bx::mtxIdentity(mtxCrop);
  2090. mtxCrop[ 0] = scalex;
  2091. mtxCrop[ 5] = scaley;
  2092. mtxCrop[12] = offsetx;
  2093. mtxCrop[13] = offsety;
  2094. bx::mtxMul(lightProj[ii], mtxCrop, mtxProj);
  2095. }
  2096. }
  2097. // Reset render targets.
  2098. const bgfx::FrameBufferHandle invalidRt = BGFX_INVALID_HANDLE;
  2099. for (uint8_t ii = 0; ii < RENDERVIEW_DRAWDEPTH_3_ID+1; ++ii)
  2100. {
  2101. bgfx::setViewFrameBuffer(ii, invalidRt);
  2102. bgfx::setViewRect(ii, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2103. }
  2104. // Determine on-screen rectangle size where depth buffer will be drawn.
  2105. uint16_t depthRectHeight = uint16_t(float(m_viewState.m_height) / 2.5f);
  2106. uint16_t depthRectWidth = depthRectHeight;
  2107. uint16_t depthRectX = 0;
  2108. uint16_t depthRectY = m_viewState.m_height - depthRectHeight;
  2109. // Setup views and render targets.
  2110. bgfx::setViewRect(0, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2111. bgfx::setViewTransform(0, m_viewState.m_view, m_viewState.m_proj);
  2112. if (LightType::SpotLight == m_settings.m_lightType)
  2113. {
  2114. /**
  2115. * RENDERVIEW_SHADOWMAP_0_ID - Clear shadow map. (used as convenience, otherwise render_pass_1 could be cleared)
  2116. * RENDERVIEW_SHADOWMAP_1_ID - Craft shadow map.
  2117. * RENDERVIEW_VBLUR_0_ID - Vertical blur.
  2118. * RENDERVIEW_HBLUR_0_ID - Horizontal blur.
  2119. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2120. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2121. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer.
  2122. */
  2123. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2124. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2125. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2126. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2127. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2128. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2129. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX, depthRectY, depthRectWidth, depthRectHeight);
  2130. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_0_ID, screenView, screenProj);
  2131. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[0], lightProj[ProjType::Horizontal]);
  2132. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2133. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2134. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2135. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2136. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2137. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_0_ID, s_rtShadowMap[0]);
  2138. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2139. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur);
  2140. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]);
  2141. }
  2142. else if (LightType::PointLight == m_settings.m_lightType)
  2143. {
  2144. /**
  2145. * RENDERVIEW_SHADOWMAP_0_ID - Clear entire shadow map.
  2146. * RENDERVIEW_SHADOWMAP_1_ID - Craft green tetrahedron shadow face.
  2147. * RENDERVIEW_SHADOWMAP_2_ID - Craft yellow tetrahedron shadow face.
  2148. * RENDERVIEW_SHADOWMAP_3_ID - Craft blue tetrahedron shadow face.
  2149. * RENDERVIEW_SHADOWMAP_4_ID - Craft red tetrahedron shadow face.
  2150. * RENDERVIEW_VBLUR_0_ID - Vertical blur.
  2151. * RENDERVIEW_HBLUR_0_ID - Horizontal blur.
  2152. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2153. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2154. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer.
  2155. */
  2156. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2157. if (m_settings.m_stencilPack)
  2158. {
  2159. const uint16_t f = m_currentShadowMapSize; //full size
  2160. const uint16_t h = m_currentShadowMapSize/2; //half size
  2161. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, f, h);
  2162. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, 0, h, f, h);
  2163. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, 0, h, f);
  2164. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, h, 0, h, f);
  2165. }
  2166. else
  2167. {
  2168. const uint16_t h = m_currentShadowMapSize/2; //half size
  2169. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, h, h);
  2170. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, h, 0, h, h);
  2171. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, h, h, h);
  2172. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, h, h, h, h);
  2173. }
  2174. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2175. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2176. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2177. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2178. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX, depthRectY, depthRectWidth, depthRectHeight);
  2179. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_0_ID, screenView, screenProj);
  2180. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[TetrahedronFaces::Green], lightProj[ProjType::Horizontal]);
  2181. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_2_ID, lightView[TetrahedronFaces::Yellow], lightProj[ProjType::Horizontal]);
  2182. if(m_settings.m_stencilPack)
  2183. {
  2184. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[TetrahedronFaces::Blue], lightProj[ProjType::Vertical]);
  2185. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[TetrahedronFaces::Red], lightProj[ProjType::Vertical]);
  2186. }
  2187. else
  2188. {
  2189. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[TetrahedronFaces::Blue], lightProj[ProjType::Horizontal]);
  2190. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[TetrahedronFaces::Red], lightProj[ProjType::Horizontal]);
  2191. }
  2192. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2193. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2194. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2195. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2196. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2197. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_0_ID, s_rtShadowMap[0]);
  2198. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2199. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_2_ID, s_rtShadowMap[0]);
  2200. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_3_ID, s_rtShadowMap[0]);
  2201. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_4_ID, s_rtShadowMap[0]);
  2202. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur);
  2203. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]);
  2204. }
  2205. else // LightType::DirectionalLight == settings.m_lightType
  2206. {
  2207. /**
  2208. * RENDERVIEW_SHADOWMAP_1_ID - Craft shadow map for first split.
  2209. * RENDERVIEW_SHADOWMAP_2_ID - Craft shadow map for second split.
  2210. * RENDERVIEW_SHADOWMAP_3_ID - Craft shadow map for third split.
  2211. * RENDERVIEW_SHADOWMAP_4_ID - Craft shadow map for fourth split.
  2212. * RENDERVIEW_VBLUR_0_ID - Vertical blur for first split.
  2213. * RENDERVIEW_HBLUR_0_ID - Horizontal blur for first split.
  2214. * RENDERVIEW_VBLUR_1_ID - Vertical blur for second split.
  2215. * RENDERVIEW_HBLUR_1_ID - Horizontal blur for second split.
  2216. * RENDERVIEW_VBLUR_2_ID - Vertical blur for third split.
  2217. * RENDERVIEW_HBLUR_2_ID - Horizontal blur for third split.
  2218. * RENDERVIEW_VBLUR_3_ID - Vertical blur for fourth split.
  2219. * RENDERVIEW_HBLUR_3_ID - Horizontal blur for fourth split.
  2220. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2221. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2222. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer for first split.
  2223. * RENDERVIEW_DRAWDEPTH_1_ID - Draw depth buffer for second split.
  2224. * RENDERVIEW_DRAWDEPTH_2_ID - Draw depth buffer for third split.
  2225. * RENDERVIEW_DRAWDEPTH_3_ID - Draw depth buffer for fourth split.
  2226. */
  2227. depthRectHeight = m_viewState.m_height / 3;
  2228. depthRectWidth = depthRectHeight;
  2229. depthRectX = 0;
  2230. depthRectY = m_viewState.m_height - depthRectHeight;
  2231. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2232. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2233. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2234. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2235. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2236. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2237. bgfx::setViewRect(RENDERVIEW_VBLUR_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2238. bgfx::setViewRect(RENDERVIEW_HBLUR_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2239. bgfx::setViewRect(RENDERVIEW_VBLUR_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2240. bgfx::setViewRect(RENDERVIEW_HBLUR_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2241. bgfx::setViewRect(RENDERVIEW_VBLUR_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2242. bgfx::setViewRect(RENDERVIEW_HBLUR_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2243. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2244. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2245. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX+(0*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2246. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_1_ID, depthRectX+(1*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2247. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_2_ID, depthRectX+(2*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2248. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_3_ID, depthRectX+(3*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2249. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[0], lightProj[0]);
  2250. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_2_ID, lightView[0], lightProj[1]);
  2251. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[0], lightProj[2]);
  2252. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[0], lightProj[3]);
  2253. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2254. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2255. bgfx::setViewTransform(RENDERVIEW_VBLUR_1_ID, screenView, screenProj);
  2256. bgfx::setViewTransform(RENDERVIEW_HBLUR_1_ID, screenView, screenProj);
  2257. bgfx::setViewTransform(RENDERVIEW_VBLUR_2_ID, screenView, screenProj);
  2258. bgfx::setViewTransform(RENDERVIEW_HBLUR_2_ID, screenView, screenProj);
  2259. bgfx::setViewTransform(RENDERVIEW_VBLUR_3_ID, screenView, screenProj);
  2260. bgfx::setViewTransform(RENDERVIEW_HBLUR_3_ID, screenView, screenProj);
  2261. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2262. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2263. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2264. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_1_ID, screenView, screenProj);
  2265. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_2_ID, screenView, screenProj);
  2266. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_3_ID, screenView, screenProj);
  2267. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2268. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_2_ID, s_rtShadowMap[1]);
  2269. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_3_ID, s_rtShadowMap[2]);
  2270. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_4_ID, s_rtShadowMap[3]);
  2271. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur); //vblur
  2272. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]); //hblur
  2273. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_1_ID, s_rtBlur); //vblur
  2274. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_1_ID, s_rtShadowMap[1]); //hblur
  2275. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_2_ID, s_rtBlur); //vblur
  2276. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_2_ID, s_rtShadowMap[2]); //hblur
  2277. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_3_ID, s_rtBlur); //vblur
  2278. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_3_ID, s_rtShadowMap[3]); //hblur
  2279. }
  2280. // Clear backbuffer at beginning.
  2281. bgfx::setViewClear(0
  2282. , BGFX_CLEAR_COLOR
  2283. | BGFX_CLEAR_DEPTH
  2284. , m_clearValues.m_clearRgba
  2285. , m_clearValues.m_clearDepth
  2286. , m_clearValues.m_clearStencil
  2287. );
  2288. bgfx::touch(0);
  2289. // Clear shadowmap rendertarget at beginning.
  2290. const uint8_t flags0 = (LightType::DirectionalLight == m_settings.m_lightType)
  2291. ? 0
  2292. : BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL
  2293. ;
  2294. bgfx::setViewClear(RENDERVIEW_SHADOWMAP_0_ID
  2295. , flags0
  2296. , 0xfefefefe //blur fails on completely white regions
  2297. , m_clearValues.m_clearDepth
  2298. , m_clearValues.m_clearStencil
  2299. );
  2300. bgfx::touch(RENDERVIEW_SHADOWMAP_0_ID);
  2301. const uint8_t flags1 = (LightType::DirectionalLight == m_settings.m_lightType)
  2302. ? BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  2303. : 0
  2304. ;
  2305. for (uint8_t ii = 0; ii < 4; ++ii)
  2306. {
  2307. bgfx::setViewClear(RENDERVIEW_SHADOWMAP_1_ID+ii
  2308. , flags1
  2309. , 0xfefefefe //blur fails on completely white regions
  2310. , m_clearValues.m_clearDepth
  2311. , m_clearValues.m_clearStencil
  2312. );
  2313. bgfx::touch(RENDERVIEW_SHADOWMAP_1_ID+ii);
  2314. }
  2315. // Render.
  2316. // Craft shadow map.
  2317. {
  2318. // Craft stencil mask for point light shadow map packing.
  2319. if(LightType::PointLight == m_settings.m_lightType && m_settings.m_stencilPack)
  2320. {
  2321. if (6 == bgfx::getAvailTransientVertexBuffer(6, m_posLayout) )
  2322. {
  2323. struct Pos
  2324. {
  2325. float m_x, m_y, m_z;
  2326. };
  2327. bgfx::TransientVertexBuffer vb;
  2328. bgfx::allocTransientVertexBuffer(&vb, 6, m_posLayout);
  2329. Pos* vertex = (Pos*)vb.data;
  2330. const float min = 0.0f;
  2331. const float max = 1.0f;
  2332. const float center = 0.5f;
  2333. const float zz = 0.0f;
  2334. vertex[0].m_x = min;
  2335. vertex[0].m_y = min;
  2336. vertex[0].m_z = zz;
  2337. vertex[1].m_x = max;
  2338. vertex[1].m_y = min;
  2339. vertex[1].m_z = zz;
  2340. vertex[2].m_x = center;
  2341. vertex[2].m_y = center;
  2342. vertex[2].m_z = zz;
  2343. vertex[3].m_x = center;
  2344. vertex[3].m_y = center;
  2345. vertex[3].m_z = zz;
  2346. vertex[4].m_x = max;
  2347. vertex[4].m_y = max;
  2348. vertex[4].m_z = zz;
  2349. vertex[5].m_x = min;
  2350. vertex[5].m_y = max;
  2351. vertex[5].m_z = zz;
  2352. bgfx::setState(0);
  2353. bgfx::setStencil(BGFX_STENCIL_TEST_ALWAYS
  2354. | BGFX_STENCIL_FUNC_REF(1)
  2355. | BGFX_STENCIL_FUNC_RMASK(0xff)
  2356. | BGFX_STENCIL_OP_FAIL_S_REPLACE
  2357. | BGFX_STENCIL_OP_FAIL_Z_REPLACE
  2358. | BGFX_STENCIL_OP_PASS_Z_REPLACE
  2359. );
  2360. bgfx::setVertexBuffer(0, &vb);
  2361. bgfx::submit(RENDERVIEW_SHADOWMAP_0_ID, s_programs.m_black);
  2362. }
  2363. }
  2364. // Draw scene into shadowmap.
  2365. uint8_t drawNum;
  2366. if (LightType::SpotLight == m_settings.m_lightType)
  2367. {
  2368. drawNum = 1;
  2369. }
  2370. else if (LightType::PointLight == m_settings.m_lightType)
  2371. {
  2372. drawNum = 4;
  2373. }
  2374. else //LightType::DirectionalLight == settings.m_lightType)
  2375. {
  2376. drawNum = uint8_t(m_settings.m_numSplits);
  2377. }
  2378. for (uint8_t ii = 0; ii < drawNum; ++ii)
  2379. {
  2380. const uint8_t viewId = RENDERVIEW_SHADOWMAP_1_ID + ii;
  2381. uint8_t renderStateIndex = RenderState::ShadowMap_PackDepth;
  2382. if(LightType::PointLight == m_settings.m_lightType && m_settings.m_stencilPack)
  2383. {
  2384. renderStateIndex = uint8_t( (ii < 2) ? RenderState::ShadowMap_PackDepthHoriz : RenderState::ShadowMap_PackDepthVert);
  2385. }
  2386. // Floor.
  2387. m_hplaneMesh.submit(viewId
  2388. , mtxFloor
  2389. , *currentSmSettings->m_progPack
  2390. , s_renderStates[renderStateIndex]
  2391. );
  2392. // Bunny.
  2393. m_bunnyMesh.submit(viewId
  2394. , mtxBunny
  2395. , *currentSmSettings->m_progPack
  2396. , s_renderStates[renderStateIndex]
  2397. );
  2398. // Hollow cube.
  2399. m_hollowcubeMesh.submit(viewId
  2400. , mtxHollowcube
  2401. , *currentSmSettings->m_progPack
  2402. , s_renderStates[renderStateIndex]
  2403. );
  2404. // Cube.
  2405. m_cubeMesh.submit(viewId
  2406. , mtxCube
  2407. , *currentSmSettings->m_progPack
  2408. , s_renderStates[renderStateIndex]
  2409. );
  2410. // Trees.
  2411. for (uint8_t jj = 0; jj < numTrees; ++jj)
  2412. {
  2413. m_treeMesh.submit(viewId
  2414. , mtxTrees[jj]
  2415. , *currentSmSettings->m_progPack
  2416. , s_renderStates[renderStateIndex]
  2417. );
  2418. }
  2419. }
  2420. }
  2421. PackDepth::Enum depthType = (SmImpl::VSM == m_settings.m_smImpl) ? PackDepth::VSM : PackDepth::RGBA;
  2422. bool bVsmOrEsm = (SmImpl::VSM == m_settings.m_smImpl) || (SmImpl::ESM == m_settings.m_smImpl);
  2423. // Blur shadow map.
  2424. if (bVsmOrEsm
  2425. && currentSmSettings->m_doBlur)
  2426. {
  2427. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[0]) );
  2428. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2429. screenSpaceQuad(s_originBottomLeft);
  2430. bgfx::submit(RENDERVIEW_VBLUR_0_ID, s_programs.m_vBlur[depthType]);
  2431. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtBlur) );
  2432. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2433. screenSpaceQuad(s_originBottomLeft);
  2434. bgfx::submit(RENDERVIEW_HBLUR_0_ID, s_programs.m_hBlur[depthType]);
  2435. if (LightType::DirectionalLight == m_settings.m_lightType)
  2436. {
  2437. for (uint8_t ii = 1, jj = 2; ii < m_settings.m_numSplits; ++ii, jj+=2)
  2438. {
  2439. const uint8_t viewId = RENDERVIEW_VBLUR_0_ID + jj;
  2440. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[ii]) );
  2441. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2442. screenSpaceQuad(s_originBottomLeft);
  2443. bgfx::submit(viewId, s_programs.m_vBlur[depthType]);
  2444. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtBlur) );
  2445. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2446. screenSpaceQuad(s_originBottomLeft);
  2447. bgfx::submit(viewId+1, s_programs.m_hBlur[depthType]);
  2448. }
  2449. }
  2450. }
  2451. // Draw scene.
  2452. {
  2453. // Setup shadow mtx.
  2454. float mtxShadow[16];
  2455. const float ymul = (s_originBottomLeft) ? 0.5f : -0.5f;
  2456. float zadd = (DepthImpl::Linear == m_settings.m_depthImpl) ? 0.0f : 0.5f;
  2457. const float mtxBias[16] =
  2458. {
  2459. 0.5f, 0.0f, 0.0f, 0.0f,
  2460. 0.0f, ymul, 0.0f, 0.0f,
  2461. 0.0f, 0.0f, 0.5f, 0.0f,
  2462. 0.5f, 0.5f, zadd, 1.0f,
  2463. };
  2464. if (LightType::SpotLight == m_settings.m_lightType)
  2465. {
  2466. float mtxTmp[16];
  2467. bx::mtxMul(mtxTmp, lightProj[ProjType::Horizontal], mtxBias);
  2468. bx::mtxMul(mtxShadow, lightView[0], mtxTmp); //lightViewProjBias
  2469. }
  2470. else if (LightType::PointLight == m_settings.m_lightType)
  2471. {
  2472. const float s = (s_originBottomLeft) ? 1.0f : -1.0f; //sign
  2473. zadd = (DepthImpl::Linear == m_settings.m_depthImpl) ? 0.0f : 0.5f;
  2474. const float mtxCropBias[2][TetrahedronFaces::Count][16] =
  2475. {
  2476. { // settings.m_stencilPack == false
  2477. { // D3D: Green, OGL: Blue
  2478. 0.25f, 0.0f, 0.0f, 0.0f,
  2479. 0.0f, s*0.25f, 0.0f, 0.0f,
  2480. 0.0f, 0.0f, 0.5f, 0.0f,
  2481. 0.25f, 0.25f, zadd, 1.0f,
  2482. },
  2483. { // D3D: Yellow, OGL: Red
  2484. 0.25f, 0.0f, 0.0f, 0.0f,
  2485. 0.0f, s*0.25f, 0.0f, 0.0f,
  2486. 0.0f, 0.0f, 0.5f, 0.0f,
  2487. 0.75f, 0.25f, zadd, 1.0f,
  2488. },
  2489. { // D3D: Blue, OGL: Green
  2490. 0.25f, 0.0f, 0.0f, 0.0f,
  2491. 0.0f, s*0.25f, 0.0f, 0.0f,
  2492. 0.0f, 0.0f, 0.5f, 0.0f,
  2493. 0.25f, 0.75f, zadd, 1.0f,
  2494. },
  2495. { // D3D: Red, OGL: Yellow
  2496. 0.25f, 0.0f, 0.0f, 0.0f,
  2497. 0.0f, s*0.25f, 0.0f, 0.0f,
  2498. 0.0f, 0.0f, 0.5f, 0.0f,
  2499. 0.75f, 0.75f, zadd, 1.0f,
  2500. },
  2501. },
  2502. { // settings.m_stencilPack == true
  2503. { // D3D: Red, OGL: Blue
  2504. 0.25f, 0.0f, 0.0f, 0.0f,
  2505. 0.0f, s*0.5f, 0.0f, 0.0f,
  2506. 0.0f, 0.0f, 0.5f, 0.0f,
  2507. 0.25f, 0.5f, zadd, 1.0f,
  2508. },
  2509. { // D3D: Blue, OGL: Red
  2510. 0.25f, 0.0f, 0.0f, 0.0f,
  2511. 0.0f, s*0.5f, 0.0f, 0.0f,
  2512. 0.0f, 0.0f, 0.5f, 0.0f,
  2513. 0.75f, 0.5f, zadd, 1.0f,
  2514. },
  2515. { // D3D: Green, OGL: Green
  2516. 0.5f, 0.0f, 0.0f, 0.0f,
  2517. 0.0f, s*0.25f, 0.0f, 0.0f,
  2518. 0.0f, 0.0f, 0.5f, 0.0f,
  2519. 0.5f, 0.75f, zadd, 1.0f,
  2520. },
  2521. { // D3D: Yellow, OGL: Yellow
  2522. 0.5f, 0.0f, 0.0f, 0.0f,
  2523. 0.0f, s*0.25f, 0.0f, 0.0f,
  2524. 0.0f, 0.0f, 0.5f, 0.0f,
  2525. 0.5f, 0.25f, zadd, 1.0f,
  2526. },
  2527. }
  2528. };
  2529. //Use as: [stencilPack][flipV][tetrahedronFace]
  2530. static const uint8_t cropBiasIndices[2][2][4] =
  2531. {
  2532. { // settings.m_stencilPack == false
  2533. { 0, 1, 2, 3 }, //flipV == false
  2534. { 2, 3, 0, 1 }, //flipV == true
  2535. },
  2536. { // settings.m_stencilPack == true
  2537. { 3, 2, 0, 1 }, //flipV == false
  2538. { 2, 3, 0, 1 }, //flipV == true
  2539. },
  2540. };
  2541. for (uint8_t ii = 0; ii < TetrahedronFaces::Count; ++ii)
  2542. {
  2543. ProjType::Enum projType = (m_settings.m_stencilPack) ? ProjType::Enum(ii>1) : ProjType::Horizontal;
  2544. uint8_t biasIndex = cropBiasIndices[m_settings.m_stencilPack][uint8_t(s_originBottomLeft)][ii];
  2545. float mtxTmp[16];
  2546. bx::mtxMul(mtxTmp, mtxYpr[ii], lightProj[projType]);
  2547. bx::mtxMul(m_shadowMapMtx[ii], mtxTmp, mtxCropBias[m_settings.m_stencilPack][biasIndex]); //mtxYprProjBias
  2548. }
  2549. bx::mtxTranslate(mtxShadow //lightInvTranslate
  2550. , -m_pointLight.m_position.m_v[0]
  2551. , -m_pointLight.m_position.m_v[1]
  2552. , -m_pointLight.m_position.m_v[2]
  2553. );
  2554. }
  2555. else //LightType::DirectionalLight == settings.m_lightType
  2556. {
  2557. for (uint8_t ii = 0; ii < m_settings.m_numSplits; ++ii)
  2558. {
  2559. float mtxTmp[16];
  2560. bx::mtxMul(mtxTmp, lightProj[ii], mtxBias);
  2561. bx::mtxMul(m_shadowMapMtx[ii], lightView[0], mtxTmp); //lViewProjCropBias
  2562. }
  2563. }
  2564. // Floor.
  2565. if (LightType::DirectionalLight != m_settings.m_lightType)
  2566. {
  2567. bx::mtxMul(m_lightMtx, mtxFloor, mtxShadow); //not needed for directional light
  2568. }
  2569. m_hplaneMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2570. , mtxFloor
  2571. , *currentSmSettings->m_progDraw
  2572. , s_renderStates[RenderState::Default]
  2573. , true
  2574. );
  2575. // Bunny.
  2576. if (LightType::DirectionalLight != m_settings.m_lightType)
  2577. {
  2578. bx::mtxMul(m_lightMtx, mtxBunny, mtxShadow);
  2579. }
  2580. m_bunnyMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2581. , mtxBunny
  2582. , *currentSmSettings->m_progDraw
  2583. , s_renderStates[RenderState::Default]
  2584. , true
  2585. );
  2586. // Hollow cube.
  2587. if (LightType::DirectionalLight != m_settings.m_lightType)
  2588. {
  2589. bx::mtxMul(m_lightMtx, mtxHollowcube, mtxShadow);
  2590. }
  2591. m_hollowcubeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2592. , mtxHollowcube
  2593. , *currentSmSettings->m_progDraw
  2594. , s_renderStates[RenderState::Default]
  2595. , true
  2596. );
  2597. // Cube.
  2598. if (LightType::DirectionalLight != m_settings.m_lightType)
  2599. {
  2600. bx::mtxMul(m_lightMtx, mtxCube, mtxShadow);
  2601. }
  2602. m_cubeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2603. , mtxCube
  2604. , *currentSmSettings->m_progDraw
  2605. , s_renderStates[RenderState::Default]
  2606. , true
  2607. );
  2608. // Trees.
  2609. for (uint8_t ii = 0; ii < numTrees; ++ii)
  2610. {
  2611. if (LightType::DirectionalLight != m_settings.m_lightType)
  2612. {
  2613. bx::mtxMul(m_lightMtx, mtxTrees[ii], mtxShadow);
  2614. }
  2615. m_treeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2616. , mtxTrees[ii]
  2617. , *currentSmSettings->m_progDraw
  2618. , s_renderStates[RenderState::Default]
  2619. , true
  2620. );
  2621. }
  2622. // Lights.
  2623. if (LightType::SpotLight == m_settings.m_lightType || LightType::PointLight == m_settings.m_lightType)
  2624. {
  2625. const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
  2626. float mtx[16];
  2627. mtxBillboard(mtx, m_viewState.m_view, m_pointLight.m_position.m_v, lightScale);
  2628. m_vplaneMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2629. , mtx
  2630. , s_programs.m_colorTexture
  2631. , s_renderStates[RenderState::Custom_BlendLightTexture]
  2632. , m_texFlare
  2633. );
  2634. }
  2635. // Draw floor bottom.
  2636. float floorBottomMtx[16];
  2637. bx::mtxSRT(floorBottomMtx
  2638. , floorScale //scaleX
  2639. , floorScale //scaleY
  2640. , floorScale //scaleZ
  2641. , 0.0f //rotX
  2642. , 0.0f //rotY
  2643. , 0.0f //rotZ
  2644. , 0.0f //translateX
  2645. , -0.1f //translateY
  2646. , 0.0f //translateZ
  2647. );
  2648. m_hplaneMesh.submit(RENDERVIEW_DRAWSCENE_1_ID
  2649. , floorBottomMtx
  2650. , s_programs.m_texture
  2651. , s_renderStates[RenderState::Custom_DrawPlaneBottom]
  2652. , m_texFigure
  2653. );
  2654. }
  2655. // Draw depth rect.
  2656. if (m_settings.m_drawDepthBuffer)
  2657. {
  2658. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[0]) );
  2659. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2660. screenSpaceQuad(s_originBottomLeft);
  2661. bgfx::submit(RENDERVIEW_DRAWDEPTH_0_ID, s_programs.m_drawDepth[depthType]);
  2662. if (LightType::DirectionalLight == m_settings.m_lightType)
  2663. {
  2664. for (uint8_t ii = 1; ii < m_settings.m_numSplits; ++ii)
  2665. {
  2666. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[ii]) );
  2667. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2668. screenSpaceQuad(s_originBottomLeft);
  2669. bgfx::submit(RENDERVIEW_DRAWDEPTH_0_ID+ii, s_programs.m_drawDepth[depthType]);
  2670. }
  2671. }
  2672. }
  2673. // Advance to next frame. Rendering thread will be kicked to
  2674. // process submitted rendering primitives.
  2675. bgfx::frame();
  2676. return true;
  2677. }
  2678. return false;
  2679. }
  2680. entry::MouseState m_mouseState;
  2681. uint32_t m_width;
  2682. uint32_t m_height;
  2683. uint32_t m_debug;
  2684. uint32_t m_reset;
  2685. ViewState m_viewState;
  2686. ClearValues m_clearValues;
  2687. bgfx::VertexLayout m_posLayout;
  2688. bgfx::TextureHandle m_texFigure;
  2689. bgfx::TextureHandle m_texFlare;
  2690. bgfx::TextureHandle m_texFieldstone;
  2691. Mesh m_bunnyMesh;
  2692. Mesh m_treeMesh;
  2693. Mesh m_cubeMesh;
  2694. Mesh m_hollowcubeMesh;
  2695. Mesh m_hplaneMesh;
  2696. Mesh m_vplaneMesh;
  2697. float m_color[4];
  2698. Material m_defaultMaterial;
  2699. Light m_pointLight;
  2700. Light m_directionalLight;
  2701. float m_lightMtx[16];
  2702. float m_shadowMapMtx[ShadowMapRenderTargets::Count][16];
  2703. ShadowMapSettings m_smSettings[LightType::Count][DepthImpl::Count][SmImpl::Count];
  2704. SceneSettings m_settings;
  2705. uint16_t m_currentShadowMapSize;
  2706. float m_timeAccumulatorLight;
  2707. float m_timeAccumulatorScene;
  2708. };
  2709. } // namespace
  2710. ENTRY_IMPLEMENT_MAIN(
  2711. ExampleShadowmaps
  2712. , "16-shadowmaps"
  2713. , "Shadow maps example."
  2714. , "https://bkaradzic.github.io/bgfx/examples.html#shadowmaps"
  2715. );