shadowmaps.cpp 104 KB

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