shadowmaps.cpp 108 KB

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