shadowmaps.cpp 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262
  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::sin(_roll);
  186. float croll = bx::cos(_roll);
  187. float spitch = bx::sin(_pitch);
  188. float cpitch = bx::cos(_pitch);
  189. float syaw = bx::sin(_yaw);
  190. float cyaw = bx::cos(_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::destroy(u_params0);
  423. bgfx::destroy(u_params1);
  424. bgfx::destroy(u_params2);
  425. bgfx::destroy(u_color);
  426. bgfx::destroy(u_smSamplingParams);
  427. bgfx::destroy(u_csmFarDistances);
  428. bgfx::destroy(u_materialKa);
  429. bgfx::destroy(u_materialKd);
  430. bgfx::destroy(u_materialKs);
  431. bgfx::destroy(u_tetraNormalGreen);
  432. bgfx::destroy(u_tetraNormalYellow);
  433. bgfx::destroy(u_tetraNormalBlue);
  434. bgfx::destroy(u_tetraNormalRed);
  435. bgfx::destroy(u_shadowMapMtx0);
  436. bgfx::destroy(u_shadowMapMtx1);
  437. bgfx::destroy(u_shadowMapMtx2);
  438. bgfx::destroy(u_shadowMapMtx3);
  439. bgfx::destroy(u_lightMtx);
  440. bgfx::destroy(u_lightPosition);
  441. bgfx::destroy(u_lightAmbientPower);
  442. bgfx::destroy(u_lightDiffusePower);
  443. bgfx::destroy(u_lightSpecularPower);
  444. bgfx::destroy(u_lightSpotDirectionInner);
  445. bgfx::destroy(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_WRITE_RGB
  553. | BGFX_STATE_WRITE_A
  554. | BGFX_STATE_DEPTH_TEST_LESS
  555. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  565. | BGFX_STATE_WRITE_A
  566. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  577. | BGFX_STATE_WRITE_A
  578. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  594. | BGFX_STATE_WRITE_A
  595. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  610. | BGFX_STATE_WRITE_A
  611. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  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::destroy(group.m_vbh);
  793. if (bgfx::kInvalidHandle != group.m_ibh.idx)
  794. {
  795. bgfx::destroy(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::pow(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::destroy(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::destroy(m_packDepth[ii][jj]);
  1024. }
  1025. }
  1026. // Draw depth.
  1027. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  1028. {
  1029. bgfx::destroy(m_drawDepth[ii]);
  1030. }
  1031. // Hblur.
  1032. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  1033. {
  1034. bgfx::destroy(m_hBlur[ii]);
  1035. }
  1036. // Vblur.
  1037. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  1038. {
  1039. bgfx::destroy(m_vBlur[ii]);
  1040. }
  1041. // Misc.
  1042. bgfx::destroy(m_colorTexture);
  1043. bgfx::destroy(m_texture);
  1044. bgfx::destroy(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 init;
  1112. init.type = args.m_type;
  1113. init.vendorId = args.m_pciId;
  1114. init.resolution.width = m_viewState.m_width;
  1115. init.resolution.height = m_viewState.m_height;
  1116. init.resolution.reset = m_reset;
  1117. bgfx::init(init);
  1118. // Enable debug text.
  1119. bgfx::setDebug(m_debug);
  1120. // Setup root path for binary shaders. Shader binaries are different
  1121. // for each renderer.
  1122. switch (bgfx::getRendererType() )
  1123. {
  1124. case bgfx::RendererType::Direct3D9:
  1125. s_texelHalf = 0.5f;
  1126. break;
  1127. case bgfx::RendererType::OpenGL:
  1128. case bgfx::RendererType::OpenGLES:
  1129. s_flipV = true;
  1130. break;
  1131. default:
  1132. break;
  1133. }
  1134. // Imgui.
  1135. imguiCreate();
  1136. // Uniforms.
  1137. s_uniforms.init();
  1138. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  1139. s_shadowMap[0] = bgfx::createUniform("s_shadowMap0", bgfx::UniformType::Int1);
  1140. s_shadowMap[1] = bgfx::createUniform("s_shadowMap1", bgfx::UniformType::Int1);
  1141. s_shadowMap[2] = bgfx::createUniform("s_shadowMap2", bgfx::UniformType::Int1);
  1142. s_shadowMap[3] = bgfx::createUniform("s_shadowMap3", bgfx::UniformType::Int1);
  1143. // Programs.
  1144. s_programs.init();
  1145. // Vertex declarations.
  1146. bgfx::VertexDecl PosNormalTexcoordDecl;
  1147. PosNormalTexcoordDecl.begin()
  1148. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  1149. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  1150. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  1151. .end();
  1152. m_posDecl.begin();
  1153. m_posDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  1154. m_posDecl.end();
  1155. PosColorTexCoord0Vertex::init();
  1156. // Textures.
  1157. m_texFigure = loadTexture("textures/figure-rgba.dds");
  1158. m_texFlare = loadTexture("textures/flare.dds");
  1159. m_texFieldstone = loadTexture("textures/fieldstone-rgba.dds");
  1160. // Meshes.
  1161. m_bunnyMesh.load("meshes/bunny.bin");
  1162. m_treeMesh.load("meshes/tree.bin");
  1163. m_cubeMesh.load("meshes/cube.bin");
  1164. m_hollowcubeMesh.load("meshes/hollowcube.bin");
  1165. m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordDecl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  1166. m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordDecl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  1167. // Materials.
  1168. m_defaultMaterial =
  1169. {
  1170. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //ambient
  1171. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //diffuse
  1172. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular, exponent
  1173. };
  1174. // Lights.
  1175. m_pointLight =
  1176. {
  1177. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //position
  1178. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1179. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //ambient
  1180. { { 1.0f, 1.0f, 1.0f, 850.0f } }, //diffuse
  1181. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular
  1182. { { 0.0f,-0.4f,-0.6f, 0.0f } }, //spotdirection, spotexponent
  1183. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1184. { { 1.0f, 0.0f, 1.0f, 91.0f } }, //attenuation, spotcutoff
  1185. };
  1186. m_directionalLight =
  1187. {
  1188. { { 0.5f,-1.0f, 0.1f, 0.0f } }, //position
  1189. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1190. { { 1.0f, 1.0f, 1.0f, 0.02f } }, //ambient
  1191. { { 1.0f, 1.0f, 1.0f, 0.4f } }, //diffuse
  1192. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular
  1193. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //spotdirection, spotexponent
  1194. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1195. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //attenuation, spotcutoff
  1196. };
  1197. // Setup uniforms.
  1198. m_color[0] = m_color[1] = m_color[2] = m_color[3] = 1.0f;
  1199. s_uniforms.setPtrs(&m_defaultMaterial
  1200. , &m_pointLight
  1201. , m_color
  1202. , m_lightMtx
  1203. , &m_shadowMapMtx[ShadowMapRenderTargets::First][0]
  1204. , &m_shadowMapMtx[ShadowMapRenderTargets::Second][0]
  1205. , &m_shadowMapMtx[ShadowMapRenderTargets::Third][0]
  1206. , &m_shadowMapMtx[ShadowMapRenderTargets::Fourth][0]
  1207. );
  1208. s_uniforms.submitConstUniforms();
  1209. // Settings.
  1210. ShadowMapSettings smSettings[LightType::Count][DepthImpl::Count][SmImpl::Count] =
  1211. {
  1212. { //LightType::Spot
  1213. { //DepthImpl::InvZ
  1214. { //SmImpl::Hard
  1215. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1216. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1217. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1218. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1219. , 0.0035f, 0.0f, 0.01f, 0.00001f // m_bias
  1220. , 0.0012f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1221. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1222. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1223. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1224. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1225. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1226. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1227. , true // m_doBlur
  1228. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1229. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1230. },
  1231. { //SmImpl::PCF
  1232. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1233. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1234. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1235. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1236. , 0.007f, 0.0f, 0.01f, 0.00001f // m_bias
  1237. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1238. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1239. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1240. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1241. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1242. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1243. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1244. , true // m_doBlur
  1245. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1246. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1247. },
  1248. { //SmImpl::VSM
  1249. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1250. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1251. , 8.0f, 1.0f, 10.0f, 1.0f // m_near
  1252. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1253. , 0.045f, 0.0f, 0.1f, 0.00001f // m_bias
  1254. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1255. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1256. , 450.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1257. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1258. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1259. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1260. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1261. , true // m_doBlur
  1262. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1263. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1264. },
  1265. { //SmImpl::ESM
  1266. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1267. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1268. , 3.0f, 1.0f, 10.0f, 0.01f // m_near
  1269. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1270. , 0.02f, 0.0f, 0.3f, 0.00001f // m_bias
  1271. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1272. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1273. , 9000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1274. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1275. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1276. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1277. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1278. , true // m_doBlur
  1279. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1280. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1281. }
  1282. },
  1283. { //DepthImpl::Linear
  1284. { //SmImpl::Hard
  1285. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1286. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1287. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1288. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1289. , 0.0025f, 0.0f, 0.01f, 0.00001f // m_bias
  1290. , 0.0012f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1291. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1292. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1293. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1294. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1295. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1296. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1297. , true // m_doBlur
  1298. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1299. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1300. },
  1301. { //SmImpl::PCF
  1302. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1303. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1304. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1305. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1306. , 0.0025f, 0.0f, 0.01f, 0.00001f // m_bias
  1307. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1308. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1309. , 2000.0f, 1.0f, 2000.0f, 1.0f // m_customParam1
  1310. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1311. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1312. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1313. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1314. , true // m_doBlur
  1315. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1316. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1317. },
  1318. { //SmImpl::VSM
  1319. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1320. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1321. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1322. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1323. , 0.006f, 0.0f, 0.01f, 0.00001f // m_bias
  1324. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1325. , 0.02f, 0.0f, 0.1f, 0.00001f // m_customParam0
  1326. , 300.0f, 1.0f, 1500.0f, 1.0f // m_customParam1
  1327. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1328. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1329. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1330. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1331. , true // m_doBlur
  1332. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1333. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1334. },
  1335. { //SmImpl::ESM
  1336. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1337. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1338. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1339. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1340. , 0.0055f, 0.0f, 0.01f, 0.00001f // m_bias
  1341. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1342. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1343. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1344. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1345. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1346. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1347. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1348. , true // m_doBlur
  1349. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1350. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1351. }
  1352. }
  1353. },
  1354. { //LightType::Point
  1355. { //DepthImpl::InvZ
  1356. { //SmImpl::Hard
  1357. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1358. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1359. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1360. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1361. , 0.006f, 0.0f, 0.01f, 0.00001f // m_bias
  1362. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1363. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1364. , 50.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1365. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1366. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1367. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1368. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1369. , true // m_doBlur
  1370. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1371. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1372. },
  1373. { //SmImpl::PCF
  1374. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1375. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1376. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1377. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1378. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1379. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1380. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1381. , 50.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1382. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1383. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1384. , 1.0f, 0.0f, 3.0f, 0.001f // m_xOffset
  1385. , 1.0f, 0.0f, 3.0f, 0.001f // m_yOffset
  1386. , true // m_doBlur
  1387. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1388. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1389. },
  1390. { //SmImpl::VSM
  1391. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1392. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1393. , 8.0f, 1.0f, 10.0f, 1.0f // m_near
  1394. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1395. , 0.055f, 0.0f, 0.1f, 0.00001f // m_bias
  1396. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1397. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1398. , 450.0f, 1.0f, 900.0f, 1.0f // m_customParam1
  1399. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1400. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1401. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1402. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1403. , true // m_doBlur
  1404. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1405. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1406. },
  1407. { //SmImpl::ESM
  1408. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1409. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1410. , 3.0f, 1.0f, 10.0f, 0.01f // m_near
  1411. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1412. , 0.035f, 0.0f, 0.1f, 0.00001f // m_bias
  1413. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1414. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1415. , 9000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1416. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1417. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1418. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1419. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1420. , true // m_doBlur
  1421. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1422. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1423. }
  1424. },
  1425. { //DepthImpl::Linear
  1426. { //SmImpl::Hard
  1427. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1428. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1429. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1430. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1431. , 0.003f, 0.0f, 0.01f, 0.00001f // m_bias
  1432. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1433. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1434. , 120.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1435. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1436. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1437. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1438. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1439. , true // m_doBlur
  1440. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1441. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1442. },
  1443. { //SmImpl::PCF
  1444. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1445. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1446. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1447. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1448. , 0.0035f, 0.0f, 0.01f, 0.00001f // m_bias
  1449. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1450. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1451. , 120.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1452. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1453. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1454. , 1.0f, 0.0f, 3.0f, 0.001f // m_xOffset
  1455. , 1.0f, 0.0f, 3.0f, 0.001f // m_yOffset
  1456. , true // m_doBlur
  1457. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1458. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1459. },
  1460. { //SmImpl::VSM
  1461. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1462. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1463. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1464. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1465. , 0.006f, 0.0f, 0.1f, 0.00001f // m_bias
  1466. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1467. , 0.02f, 0.0f, 0.1f, 0.00001f // m_customParam0
  1468. , 400.0f, 1.0f, 900.0f, 1.0f // m_customParam1
  1469. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1470. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1471. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1472. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1473. , true // m_doBlur
  1474. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1475. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1476. },
  1477. { //SmImpl::ESM
  1478. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1479. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1480. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1481. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1482. , 0.007f, 0.0f, 0.01f, 0.00001f // m_bias
  1483. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1484. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1485. , 8000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1486. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1487. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1488. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1489. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1490. , true // m_doBlur
  1491. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1492. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1493. }
  1494. }
  1495. },
  1496. { //LightType::Directional
  1497. { //DepthImpl::InvZ
  1498. { //SmImpl::Hard
  1499. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1500. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1501. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1502. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1503. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1504. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1505. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1506. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1507. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1508. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1509. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1510. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1511. , true // m_doBlur
  1512. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1513. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1514. },
  1515. { //SmImpl::PCF
  1516. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1517. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1518. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1519. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1520. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1521. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1522. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1523. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1524. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1525. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1526. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1527. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1528. , true // m_doBlur
  1529. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1530. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1531. },
  1532. { //SmImpl::VSM
  1533. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1534. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1535. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1536. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1537. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1538. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1539. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1540. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1541. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1542. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1543. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1544. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1545. , true // m_doBlur
  1546. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1547. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1548. },
  1549. { //SmImpl::ESM
  1550. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1551. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1552. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1553. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1554. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1555. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1556. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1557. , 9500.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1558. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1559. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1560. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1561. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1562. , true // m_doBlur
  1563. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1564. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1565. }
  1566. },
  1567. { //DepthImpl::Linear
  1568. { //SmImpl::Hard
  1569. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1570. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1571. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1572. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1573. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1574. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1575. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1576. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1577. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1578. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1579. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1580. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1581. , true // m_doBlur
  1582. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1583. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1584. },
  1585. { //SmImpl::PCF
  1586. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1587. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1588. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1589. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1590. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1591. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1592. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1593. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1594. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1595. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1596. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1597. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1598. , true // m_doBlur
  1599. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1600. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1601. },
  1602. { //SmImpl::VSM
  1603. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1604. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1605. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1606. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1607. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1608. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1609. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1610. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1611. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1612. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1613. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1614. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1615. , true // m_doBlur
  1616. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1617. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1618. },
  1619. { //SmImpl::ESM
  1620. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1621. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1622. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1623. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1624. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1625. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1626. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1627. , 9500.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1628. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1629. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1630. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1631. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1632. , true // m_doBlur
  1633. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1634. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1635. }
  1636. }
  1637. }
  1638. };
  1639. bx::memCopy(m_smSettings, smSettings, sizeof(smSettings));
  1640. m_settings.m_lightType = LightType::SpotLight;
  1641. m_settings.m_depthImpl = DepthImpl::InvZ;
  1642. m_settings.m_smImpl = SmImpl::Hard;
  1643. m_settings.m_spotOuterAngle = 45.0f;
  1644. m_settings.m_spotInnerAngle = 30.0f;
  1645. m_settings.m_fovXAdjust = 0.0f;
  1646. m_settings.m_fovYAdjust = 0.0f;
  1647. m_settings.m_coverageSpotL = 90.0f;
  1648. m_settings.m_splitDistribution = 0.6f;
  1649. m_settings.m_numSplits = 4;
  1650. m_settings.m_updateLights = true;
  1651. m_settings.m_updateScene = true;
  1652. m_settings.m_drawDepthBuffer = false;
  1653. m_settings.m_showSmCoverage = false;
  1654. m_settings.m_stencilPack = true;
  1655. m_settings.m_stabilize = true;
  1656. ShadowMapSettings* currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1657. // Render targets.
  1658. uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo);
  1659. m_currentShadowMapSize = shadowMapSize;
  1660. float currentShadowMapSizef = float(int16_t(m_currentShadowMapSize) );
  1661. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  1662. for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
  1663. {
  1664. bgfx::TextureHandle fbtextures[] =
  1665. {
  1666. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  1667. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT),
  1668. };
  1669. s_rtShadowMap[ii] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  1670. }
  1671. s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8);
  1672. // Setup camera.
  1673. cameraCreate();
  1674. cameraSetPosition({ 0.0f, 60.0f, -105.0f });
  1675. cameraSetVerticalAngle(-0.45f);
  1676. m_timeAccumulatorLight = 0.0f;
  1677. m_timeAccumulatorScene = 0.0f;
  1678. }
  1679. virtual int shutdown() override
  1680. {
  1681. m_bunnyMesh.unload();
  1682. m_treeMesh.unload();
  1683. m_cubeMesh.unload();
  1684. m_hollowcubeMesh.unload();
  1685. m_hplaneMesh.unload();
  1686. m_vplaneMesh.unload();
  1687. bgfx::destroy(m_texFigure);
  1688. bgfx::destroy(m_texFieldstone);
  1689. bgfx::destroy(m_texFlare);
  1690. for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
  1691. {
  1692. bgfx::destroy(s_rtShadowMap[ii]);
  1693. }
  1694. bgfx::destroy(s_rtBlur);
  1695. s_programs.destroy();
  1696. bgfx::destroy(s_texColor);
  1697. bgfx::destroy(s_shadowMap[3]);
  1698. bgfx::destroy(s_shadowMap[2]);
  1699. bgfx::destroy(s_shadowMap[1]);
  1700. bgfx::destroy(s_shadowMap[0]);
  1701. s_uniforms.destroy();
  1702. cameraDestroy();
  1703. imguiDestroy();
  1704. // Shutdown bgfx.
  1705. bgfx::shutdown();
  1706. return 0;
  1707. }
  1708. bool update() override
  1709. {
  1710. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  1711. {
  1712. m_viewState.m_width = uint16_t(m_width);
  1713. m_viewState.m_height = uint16_t(m_height);
  1714. const bgfx::Caps* caps = bgfx::getCaps();
  1715. // Set view and projection matrices.
  1716. const float camFovy = 60.0f;
  1717. const float camAspect = float(int32_t(m_viewState.m_width) ) / float(int32_t(m_viewState.m_height) );
  1718. const float camNear = 0.1f;
  1719. const float camFar = 2000.0f;
  1720. const float projHeight = bx::tan(bx::toRad(camFovy)*0.5f);
  1721. const float projWidth = projHeight * camAspect;
  1722. bx::mtxProj(m_viewState.m_proj, camFovy, camAspect, camNear, camFar, caps->homogeneousDepth);
  1723. cameraGetViewMtx(m_viewState.m_view);
  1724. float currentShadowMapSizef = float(int16_t(m_currentShadowMapSize) );
  1725. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  1726. s_uniforms.submitConstUniforms();
  1727. // s_uniforms.submitConstUniforms();
  1728. // Imgui.
  1729. imguiBeginFrame(m_mouseState.m_mx
  1730. , m_mouseState.m_my
  1731. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1732. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1733. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1734. , m_mouseState.m_mz
  1735. , m_viewState.m_width
  1736. , m_viewState.m_height
  1737. );
  1738. showExampleDialog(this);
  1739. ImGui::SetNextWindowPos(
  1740. ImVec2(m_viewState.m_width - m_viewState.m_width / 5.0f - 10.0f, 10.0f)
  1741. , ImGuiCond_FirstUseEver
  1742. );
  1743. ImGui::SetNextWindowSize(
  1744. ImVec2(m_viewState.m_width / 5.0f, m_viewState.m_height - 20.0f)
  1745. , ImGuiCond_FirstUseEver
  1746. );
  1747. ImGui::Begin("Settings"
  1748. , NULL
  1749. , 0
  1750. );
  1751. #define IMGUI_FLOAT_SLIDER(_name, _val) \
  1752. ImGui::SliderFloat(_name \
  1753. , &_val \
  1754. , *( ((float*)&_val)+1) \
  1755. , *( ((float*)&_val)+2) \
  1756. )
  1757. #define IMGUI_RADIO_BUTTON(_name, _var, _val) \
  1758. if (ImGui::RadioButton(_name, _var == _val) ) \
  1759. { \
  1760. _var = _val; \
  1761. }
  1762. ImGui::Checkbox("Update lights", &m_settings.m_updateLights);
  1763. ImGui::Checkbox("Update scene", &m_settings.m_updateScene);
  1764. ImGui::Separator();
  1765. ImGui::Text("Shadow map depth:");
  1766. IMGUI_RADIO_BUTTON("InvZ", m_settings.m_depthImpl, DepthImpl::InvZ);
  1767. IMGUI_RADIO_BUTTON("Linear", m_settings.m_depthImpl, DepthImpl::Linear);
  1768. ShadowMapSettings* currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1769. ImGui::Separator();
  1770. ImGui::Checkbox("Draw depth buffer", &m_settings.m_drawDepthBuffer);
  1771. if (m_settings.m_drawDepthBuffer)
  1772. {
  1773. IMGUI_FLOAT_SLIDER("Depth value pow", currentSmSettings->m_depthValuePow);
  1774. }
  1775. ImGui::Separator();
  1776. ImGui::Text("Shadow Map implementation");
  1777. IMGUI_RADIO_BUTTON("Hard", m_settings.m_smImpl, SmImpl::Hard);
  1778. IMGUI_RADIO_BUTTON("PCF", m_settings.m_smImpl, SmImpl::PCF);
  1779. IMGUI_RADIO_BUTTON("VSM", m_settings.m_smImpl, SmImpl::VSM);
  1780. IMGUI_RADIO_BUTTON("ESM", m_settings.m_smImpl, SmImpl::ESM);
  1781. currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1782. ImGui::Separator();
  1783. IMGUI_FLOAT_SLIDER("Bias", currentSmSettings->m_bias);
  1784. IMGUI_FLOAT_SLIDER("Normal offset", currentSmSettings->m_normalOffset);
  1785. ImGui::Separator();
  1786. if (LightType::DirectionalLight != m_settings.m_lightType)
  1787. {
  1788. IMGUI_FLOAT_SLIDER("Near plane", currentSmSettings->m_near);
  1789. }
  1790. IMGUI_FLOAT_SLIDER("Far plane", currentSmSettings->m_far);
  1791. ImGui::Separator();
  1792. switch(m_settings.m_smImpl)
  1793. {
  1794. case SmImpl::Hard:
  1795. //ImGui::Text("Hard");
  1796. break;
  1797. case SmImpl::PCF:
  1798. ImGui::Text("PCF");
  1799. IMGUI_FLOAT_SLIDER("X Offset", currentSmSettings->m_xOffset);
  1800. IMGUI_FLOAT_SLIDER("Y Offset", currentSmSettings->m_yOffset);
  1801. break;
  1802. case SmImpl::VSM:
  1803. ImGui::Text("VSM");
  1804. IMGUI_FLOAT_SLIDER("Min variance", currentSmSettings->m_customParam0);
  1805. IMGUI_FLOAT_SLIDER("Depth multiplier", currentSmSettings->m_customParam1);
  1806. ImGui::Checkbox("Blur shadow map", &currentSmSettings->m_doBlur);
  1807. if (currentSmSettings->m_doBlur)
  1808. {
  1809. IMGUI_FLOAT_SLIDER("Blur X Offset", currentSmSettings->m_xOffset);
  1810. IMGUI_FLOAT_SLIDER("Blur Y Offset", currentSmSettings->m_yOffset);
  1811. }
  1812. break;
  1813. case SmImpl::ESM:
  1814. ImGui::Text("ESM");
  1815. IMGUI_FLOAT_SLIDER("ESM Hardness", currentSmSettings->m_customParam0);
  1816. IMGUI_FLOAT_SLIDER("Depth multiplier", currentSmSettings->m_customParam1);
  1817. ImGui::Checkbox("Blur shadow map", &currentSmSettings->m_doBlur);
  1818. if (currentSmSettings->m_doBlur)
  1819. {
  1820. IMGUI_FLOAT_SLIDER("Blur X Offset", currentSmSettings->m_xOffset);
  1821. IMGUI_FLOAT_SLIDER("Blur Y Offset", currentSmSettings->m_yOffset);
  1822. }
  1823. break;
  1824. default:
  1825. break;
  1826. };
  1827. ImGui::End();
  1828. #undef IMGUI_RADIO_BUTTON
  1829. ImGui::SetNextWindowPos(
  1830. ImVec2(10.0f, 260.0f)
  1831. , ImGuiCond_FirstUseEver
  1832. );
  1833. ImGui::SetNextWindowSize(
  1834. ImVec2(m_viewState.m_width / 5.0f, 350.0f)
  1835. , ImGuiCond_FirstUseEver
  1836. );
  1837. ImGui::Begin("Light"
  1838. , NULL
  1839. , 0
  1840. );
  1841. ImGui::PushItemWidth(185.0f);
  1842. bool bLtChanged = false;
  1843. if ( ImGui::RadioButton("Spot light", m_settings.m_lightType == LightType::SpotLight ))
  1844. {
  1845. m_settings.m_lightType = LightType::SpotLight; bLtChanged = true;
  1846. }
  1847. if ( ImGui::RadioButton("Point light", m_settings.m_lightType == LightType::PointLight ))
  1848. {
  1849. m_settings.m_lightType = LightType::PointLight; bLtChanged = true;
  1850. }
  1851. if ( ImGui::RadioButton("Directional light", m_settings.m_lightType == LightType::DirectionalLight ))
  1852. {
  1853. m_settings.m_lightType = LightType::DirectionalLight; bLtChanged = true;
  1854. }
  1855. ImGui::Separator();
  1856. ImGui::Checkbox("Show shadow map coverage.", &m_settings.m_showSmCoverage);
  1857. ImGui::Separator();
  1858. ImGui::Text("Shadow map resolution: %ux%u", m_currentShadowMapSize, m_currentShadowMapSize);
  1859. ImGui::SliderFloat("##SizePwrTwo", &currentSmSettings->m_sizePwrTwo,
  1860. currentSmSettings->m_sizePwrTwoMin,
  1861. currentSmSettings->m_sizePwrTwoMax, "%.0f");
  1862. ImGui::Separator();
  1863. if (LightType::SpotLight == m_settings.m_lightType)
  1864. {
  1865. ImGui::Text("Spot light");
  1866. ImGui::SliderFloat("Shadow map area", &m_settings.m_coverageSpotL, 45.0f, 120.0f);
  1867. ImGui::Separator();
  1868. ImGui::SliderFloat("Spot outer cone", &m_settings.m_spotOuterAngle, 0.0f, 91.0f);
  1869. ImGui::SliderFloat("Spot inner cone", &m_settings.m_spotInnerAngle, 0.0f, 90.0f);
  1870. }
  1871. else if (LightType::PointLight == m_settings.m_lightType)
  1872. {
  1873. ImGui::Text("Point light");
  1874. ImGui::Checkbox("Stencil pack", &m_settings.m_stencilPack);
  1875. ImGui::SliderFloat("Fov X adjust", &m_settings.m_fovXAdjust, -20.0f, 20.0f);
  1876. ImGui::SliderFloat("Fov Y adjust", &m_settings.m_fovYAdjust, -20.0f, 20.0f);
  1877. }
  1878. else if (LightType::DirectionalLight == m_settings.m_lightType)
  1879. {
  1880. ImGui::Text("Directional light");
  1881. ImGui::Checkbox("Stabilize cascades", &m_settings.m_stabilize);
  1882. ImGui::SliderInt("Cascade splits", &m_settings.m_numSplits, 1, 4);
  1883. ImGui::SliderFloat("Cascade distribution", &m_settings.m_splitDistribution, 0.0f, 1.0f);
  1884. }
  1885. #undef IMGUI_FLOAT_SLIDER
  1886. ImGui::End();
  1887. imguiEndFrame();
  1888. // Update uniforms.
  1889. s_uniforms.m_shadowMapBias = currentSmSettings->m_bias;
  1890. s_uniforms.m_shadowMapOffset = currentSmSettings->m_normalOffset;
  1891. s_uniforms.m_shadowMapParam0 = currentSmSettings->m_customParam0;
  1892. s_uniforms.m_shadowMapParam1 = currentSmSettings->m_customParam1;
  1893. s_uniforms.m_depthValuePow = currentSmSettings->m_depthValuePow;
  1894. s_uniforms.m_XNum = currentSmSettings->m_xNum;
  1895. s_uniforms.m_YNum = currentSmSettings->m_yNum;
  1896. s_uniforms.m_XOffset = currentSmSettings->m_xOffset;
  1897. s_uniforms.m_YOffset = currentSmSettings->m_yOffset;
  1898. s_uniforms.m_showSmCoverage = float(m_settings.m_showSmCoverage);
  1899. s_uniforms.m_lightPtr = (LightType::DirectionalLight == m_settings.m_lightType) ? &m_directionalLight : &m_pointLight;
  1900. if (LightType::SpotLight == m_settings.m_lightType)
  1901. {
  1902. m_pointLight.m_attenuationSpotOuter.m_outer = m_settings.m_spotOuterAngle;
  1903. m_pointLight.m_spotDirectionInner.m_inner = m_settings.m_spotInnerAngle;
  1904. }
  1905. else
  1906. {
  1907. m_pointLight.m_attenuationSpotOuter.m_outer = 91.0f; //above 90.0f means point light
  1908. }
  1909. s_uniforms.submitPerFrameUniforms();
  1910. // Time.
  1911. int64_t now = bx::getHPCounter();
  1912. static int64_t last = now;
  1913. const int64_t frameTime = now - last;
  1914. last = now;
  1915. const double freq = double(bx::getHPFrequency() );
  1916. const float deltaTime = float(frameTime/freq);
  1917. // Update camera.
  1918. cameraUpdate(deltaTime, m_mouseState);
  1919. // Update view mtx.
  1920. cameraGetViewMtx(m_viewState.m_view);
  1921. // Update lights.
  1922. m_pointLight.computeViewSpaceComponents(m_viewState.m_view);
  1923. m_directionalLight.computeViewSpaceComponents(m_viewState.m_view);
  1924. // Update time accumulators.
  1925. if (m_settings.m_updateLights) { m_timeAccumulatorLight += deltaTime; }
  1926. if (m_settings.m_updateScene) { m_timeAccumulatorScene += deltaTime; }
  1927. // Setup lights.
  1928. m_pointLight.m_position.m_x = bx::cos(m_timeAccumulatorLight) * 20.0f;
  1929. m_pointLight.m_position.m_y = 26.0f;
  1930. m_pointLight.m_position.m_z = bx::sin(m_timeAccumulatorLight) * 20.0f;
  1931. m_pointLight.m_spotDirectionInner.m_x = -m_pointLight.m_position.m_x;
  1932. m_pointLight.m_spotDirectionInner.m_y = -m_pointLight.m_position.m_y;
  1933. m_pointLight.m_spotDirectionInner.m_z = -m_pointLight.m_position.m_z;
  1934. m_directionalLight.m_position.m_x = -bx::cos(m_timeAccumulatorLight);
  1935. m_directionalLight.m_position.m_y = -1.0f;
  1936. m_directionalLight.m_position.m_z = -bx::sin(m_timeAccumulatorLight);
  1937. // Setup instance matrices.
  1938. float mtxFloor[16];
  1939. const float floorScale = 550.0f;
  1940. bx::mtxSRT(mtxFloor
  1941. , floorScale //scaleX
  1942. , floorScale //scaleY
  1943. , floorScale //scaleZ
  1944. , 0.0f //rotX
  1945. , 0.0f //rotY
  1946. , 0.0f //rotZ
  1947. , 0.0f //translateX
  1948. , 0.0f //translateY
  1949. , 0.0f //translateZ
  1950. );
  1951. float mtxBunny[16];
  1952. bx::mtxSRT(mtxBunny
  1953. , 5.0f
  1954. , 5.0f
  1955. , 5.0f
  1956. , 0.0f
  1957. , 1.56f - m_timeAccumulatorScene
  1958. , 0.0f
  1959. , 15.0f
  1960. , 5.0f
  1961. , 0.0f
  1962. );
  1963. float mtxHollowcube[16];
  1964. bx::mtxSRT(mtxHollowcube
  1965. , 2.5f
  1966. , 2.5f
  1967. , 2.5f
  1968. , 0.0f
  1969. , 1.56f - m_timeAccumulatorScene
  1970. , 0.0f
  1971. , 0.0f
  1972. , 10.0f
  1973. , 0.0f
  1974. );
  1975. float mtxCube[16];
  1976. bx::mtxSRT(mtxCube
  1977. , 2.5f
  1978. , 2.5f
  1979. , 2.5f
  1980. , 0.0f
  1981. , 1.56f - m_timeAccumulatorScene
  1982. , 0.0f
  1983. , -15.0f
  1984. , 5.0f
  1985. , 0.0f
  1986. );
  1987. const uint8_t numTrees = 10;
  1988. float mtxTrees[numTrees][16];
  1989. for (uint8_t ii = 0; ii < numTrees; ++ii)
  1990. {
  1991. bx::mtxSRT(mtxTrees[ii]
  1992. , 2.0f
  1993. , 2.0f
  1994. , 2.0f
  1995. , 0.0f
  1996. , float(ii)
  1997. , 0.0f
  1998. , bx::sin(float(ii)*2.0f*bx::kPi/float(numTrees) ) * 60.0f
  1999. , 0.0f
  2000. , bx::cos(float(ii)*2.0f*bx::kPi/float(numTrees) ) * 60.0f
  2001. );
  2002. }
  2003. // Compute transform matrices.
  2004. const uint8_t shadowMapPasses = ShadowMapRenderTargets::Count;
  2005. float lightView[shadowMapPasses][16];
  2006. float lightProj[shadowMapPasses][16];
  2007. float mtxYpr[TetrahedronFaces::Count][16];
  2008. float screenProj[16];
  2009. float screenView[16];
  2010. bx::mtxIdentity(screenView);
  2011. bx::mtxOrtho(
  2012. screenProj
  2013. , 0.0f
  2014. , 1.0f
  2015. , 1.0f
  2016. , 0.0f
  2017. , 0.0f
  2018. , 100.0f
  2019. , 0.0f
  2020. , caps->homogeneousDepth
  2021. );
  2022. if (LightType::SpotLight == m_settings.m_lightType)
  2023. {
  2024. const float fovy = m_settings.m_coverageSpotL;
  2025. const float aspect = 1.0f;
  2026. bx::mtxProj(
  2027. lightProj[ProjType::Horizontal]
  2028. , fovy
  2029. , aspect
  2030. , currentSmSettings->m_near
  2031. , currentSmSettings->m_far
  2032. , false
  2033. );
  2034. //For linear depth, prevent depth division by variable w-component in shaders and divide here by far plane
  2035. if (DepthImpl::Linear == m_settings.m_depthImpl)
  2036. {
  2037. lightProj[ProjType::Horizontal][10] /= currentSmSettings->m_far;
  2038. lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far;
  2039. }
  2040. const bx::Vec3 at = bx::add(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(m_pointLight.m_spotDirectionInner.m_v) );
  2041. bx::mtxLookAt(lightView[TetrahedronFaces::Green], bx::load<bx::Vec3>(m_pointLight.m_position.m_v), at);
  2042. }
  2043. else if (LightType::PointLight == m_settings.m_lightType)
  2044. {
  2045. float ypr[TetrahedronFaces::Count][3] =
  2046. {
  2047. { bx::toRad( 0.0f), bx::toRad( 27.36780516f), bx::toRad(0.0f) },
  2048. { bx::toRad(180.0f), bx::toRad( 27.36780516f), bx::toRad(0.0f) },
  2049. { bx::toRad(-90.0f), bx::toRad(-27.36780516f), bx::toRad(0.0f) },
  2050. { bx::toRad( 90.0f), bx::toRad(-27.36780516f), bx::toRad(0.0f) },
  2051. };
  2052. if (m_settings.m_stencilPack)
  2053. {
  2054. const float fovx = 143.98570868f + 3.51f + m_settings.m_fovXAdjust;
  2055. const float fovy = 125.26438968f + 9.85f + m_settings.m_fovYAdjust;
  2056. const float aspect = bx::tan(bx::toRad(fovx*0.5f) )/bx::tan(bx::toRad(fovy*0.5f) );
  2057. bx::mtxProj(
  2058. lightProj[ProjType::Vertical]
  2059. , fovx
  2060. , aspect
  2061. , currentSmSettings->m_near
  2062. , currentSmSettings->m_far
  2063. , false
  2064. );
  2065. //For linear depth, prevent depth division by variable w-component in shaders and divide here by far plane
  2066. if (DepthImpl::Linear == m_settings.m_depthImpl)
  2067. {
  2068. lightProj[ProjType::Vertical][10] /= currentSmSettings->m_far;
  2069. lightProj[ProjType::Vertical][14] /= currentSmSettings->m_far;
  2070. }
  2071. ypr[TetrahedronFaces::Green ][2] = bx::toRad(180.0f);
  2072. ypr[TetrahedronFaces::Yellow][2] = bx::toRad( 0.0f);
  2073. ypr[TetrahedronFaces::Blue ][2] = bx::toRad( 90.0f);
  2074. ypr[TetrahedronFaces::Red ][2] = bx::toRad(-90.0f);
  2075. }
  2076. const float fovx = 143.98570868f + 7.8f + m_settings.m_fovXAdjust;
  2077. const float fovy = 125.26438968f + 3.0f + m_settings.m_fovYAdjust;
  2078. const float aspect = bx::tan(bx::toRad(fovx*0.5f) )/bx::tan(bx::toRad(fovy*0.5f) );
  2079. bx::mtxProj(
  2080. lightProj[ProjType::Horizontal]
  2081. , fovy
  2082. , aspect
  2083. , currentSmSettings->m_near
  2084. , currentSmSettings->m_far
  2085. , caps->homogeneousDepth
  2086. );
  2087. //For linear depth, prevent depth division by variable w component in shaders and divide here by far plane
  2088. if (DepthImpl::Linear == m_settings.m_depthImpl)
  2089. {
  2090. lightProj[ProjType::Horizontal][10] /= currentSmSettings->m_far;
  2091. lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far;
  2092. }
  2093. for (uint8_t ii = 0; ii < TetrahedronFaces::Count; ++ii)
  2094. {
  2095. float mtxTmp[16];
  2096. mtxYawPitchRoll(mtxTmp, ypr[ii][0], ypr[ii][1], ypr[ii][2]);
  2097. float tmp[3] =
  2098. {
  2099. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[0]) ),
  2100. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[4]) ),
  2101. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[8]) ),
  2102. };
  2103. bx::mtxTranspose(mtxYpr[ii], mtxTmp);
  2104. bx::memCopy(lightView[ii], mtxYpr[ii], 12*sizeof(float) );
  2105. lightView[ii][12] = tmp[0];
  2106. lightView[ii][13] = tmp[1];
  2107. lightView[ii][14] = tmp[2];
  2108. lightView[ii][15] = 1.0f;
  2109. }
  2110. }
  2111. else // LightType::DirectionalLight == settings.m_lightType
  2112. {
  2113. // Setup light view mtx.
  2114. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  2115. const bx::Vec3 eye =
  2116. {
  2117. -m_directionalLight.m_position.m_x,
  2118. -m_directionalLight.m_position.m_y,
  2119. -m_directionalLight.m_position.m_z,
  2120. };
  2121. bx::mtxLookAt(lightView[0], eye, at);
  2122. // Compute camera inverse view mtx.
  2123. float mtxViewInv[16];
  2124. bx::mtxInverse(mtxViewInv, m_viewState.m_view);
  2125. // Compute split distances.
  2126. const uint8_t maxNumSplits = 4;
  2127. BX_CHECK(maxNumSplits >= settings.m_numSplits, "Error! Max num splits.");
  2128. float splitSlices[maxNumSplits*2];
  2129. splitFrustum(splitSlices
  2130. , uint8_t(m_settings.m_numSplits)
  2131. , currentSmSettings->m_near
  2132. , currentSmSettings->m_far
  2133. , m_settings.m_splitDistribution
  2134. );
  2135. // Update uniforms.
  2136. for (uint8_t ii = 0, ff = 1; ii < m_settings.m_numSplits; ++ii, ff+=2)
  2137. {
  2138. // This lags for 1 frame, but it's not a problem.
  2139. s_uniforms.m_csmFarDistances[ii] = splitSlices[ff];
  2140. }
  2141. float mtxProj[16];
  2142. bx::mtxOrtho(
  2143. mtxProj
  2144. , 1.0f
  2145. , -1.0f
  2146. , 1.0f
  2147. , -1.0f
  2148. , -currentSmSettings->m_far
  2149. , currentSmSettings->m_far
  2150. , 0.0f
  2151. , caps->homogeneousDepth
  2152. );
  2153. const uint8_t numCorners = 8;
  2154. float frustumCorners[maxNumSplits][numCorners][3];
  2155. for (uint8_t ii = 0, nn = 0, ff = 1; ii < m_settings.m_numSplits; ++ii, nn+=2, ff+=2)
  2156. {
  2157. // Compute frustum corners for one split in world space.
  2158. worldSpaceFrustumCorners( (float*)frustumCorners[ii], splitSlices[nn], splitSlices[ff], projWidth, projHeight, mtxViewInv);
  2159. float min[3] = { 9000.0f, 9000.0f, 9000.0f };
  2160. float max[3] = { -9000.0f, -9000.0f, -9000.0f };
  2161. for (uint8_t jj = 0; jj < numCorners; ++jj)
  2162. {
  2163. // Transform to light space.
  2164. float lightSpaceFrustumCorner[3];
  2165. bx::vec3MulMtx(lightSpaceFrustumCorner, frustumCorners[ii][jj], lightView[0]);
  2166. // Update bounding box.
  2167. min[0] = bx::min(min[0], lightSpaceFrustumCorner[0]);
  2168. max[0] = bx::max(max[0], lightSpaceFrustumCorner[0]);
  2169. min[1] = bx::min(min[1], lightSpaceFrustumCorner[1]);
  2170. max[1] = bx::max(max[1], lightSpaceFrustumCorner[1]);
  2171. min[2] = bx::min(min[2], lightSpaceFrustumCorner[2]);
  2172. max[2] = bx::max(max[2], lightSpaceFrustumCorner[2]);
  2173. }
  2174. float minproj[3];
  2175. float maxproj[3];
  2176. bx::vec3MulMtxH(minproj, min, mtxProj);
  2177. bx::vec3MulMtxH(maxproj, max, mtxProj);
  2178. float offsetx, offsety;
  2179. float scalex, scaley;
  2180. scalex = 2.0f / (maxproj[0] - minproj[0]);
  2181. scaley = 2.0f / (maxproj[1] - minproj[1]);
  2182. if (m_settings.m_stabilize)
  2183. {
  2184. const float quantizer = 64.0f;
  2185. scalex = quantizer / bx::ceil(quantizer / scalex);
  2186. scaley = quantizer / bx::ceil(quantizer / scaley);
  2187. }
  2188. offsetx = 0.5f * (maxproj[0] + minproj[0]) * scalex;
  2189. offsety = 0.5f * (maxproj[1] + minproj[1]) * scaley;
  2190. if (m_settings.m_stabilize)
  2191. {
  2192. const float halfSize = currentShadowMapSizef * 0.5f;
  2193. offsetx = bx::ceil(offsetx * halfSize) / halfSize;
  2194. offsety = bx::ceil(offsety * halfSize) / halfSize;
  2195. }
  2196. float mtxCrop[16];
  2197. bx::mtxIdentity(mtxCrop);
  2198. mtxCrop[ 0] = scalex;
  2199. mtxCrop[ 5] = scaley;
  2200. mtxCrop[12] = offsetx;
  2201. mtxCrop[13] = offsety;
  2202. bx::mtxMul(lightProj[ii], mtxCrop, mtxProj);
  2203. }
  2204. }
  2205. // Reset render targets.
  2206. const bgfx::FrameBufferHandle invalidRt = BGFX_INVALID_HANDLE;
  2207. for (uint8_t ii = 0; ii < RENDERVIEW_DRAWDEPTH_3_ID+1; ++ii)
  2208. {
  2209. bgfx::setViewFrameBuffer(ii, invalidRt);
  2210. }
  2211. // Determine on-screen rectangle size where depth buffer will be drawn.
  2212. uint16_t depthRectHeight = uint16_t(float(m_viewState.m_height) / 2.5f);
  2213. uint16_t depthRectWidth = depthRectHeight;
  2214. uint16_t depthRectX = 0;
  2215. uint16_t depthRectY = m_viewState.m_height - depthRectHeight;
  2216. // Setup views and render targets.
  2217. bgfx::setViewRect(0, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2218. bgfx::setViewTransform(0, m_viewState.m_view, m_viewState.m_proj);
  2219. if (LightType::SpotLight == m_settings.m_lightType)
  2220. {
  2221. /**
  2222. * RENDERVIEW_SHADOWMAP_0_ID - Clear shadow map. (used as convenience, otherwise render_pass_1 could be cleared)
  2223. * RENDERVIEW_SHADOWMAP_1_ID - Craft shadow map.
  2224. * RENDERVIEW_VBLUR_0_ID - Vertical blur.
  2225. * RENDERVIEW_HBLUR_0_ID - Horizontal blur.
  2226. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2227. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2228. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer.
  2229. */
  2230. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2231. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2232. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2233. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2234. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2235. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2236. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX, depthRectY, depthRectWidth, depthRectHeight);
  2237. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_0_ID, screenView, screenProj);
  2238. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[0], lightProj[ProjType::Horizontal]);
  2239. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2240. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2241. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2242. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2243. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2244. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_0_ID, s_rtShadowMap[0]);
  2245. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2246. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur);
  2247. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]);
  2248. }
  2249. else if (LightType::PointLight == m_settings.m_lightType)
  2250. {
  2251. /**
  2252. * RENDERVIEW_SHADOWMAP_0_ID - Clear entire shadow map.
  2253. * RENDERVIEW_SHADOWMAP_1_ID - Craft green tetrahedron shadow face.
  2254. * RENDERVIEW_SHADOWMAP_2_ID - Craft yellow tetrahedron shadow face.
  2255. * RENDERVIEW_SHADOWMAP_3_ID - Craft blue tetrahedron shadow face.
  2256. * RENDERVIEW_SHADOWMAP_4_ID - Craft red tetrahedron shadow face.
  2257. * RENDERVIEW_VBLUR_0_ID - Vertical blur.
  2258. * RENDERVIEW_HBLUR_0_ID - Horizontal blur.
  2259. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2260. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2261. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer.
  2262. */
  2263. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2264. if (m_settings.m_stencilPack)
  2265. {
  2266. const uint16_t f = m_currentShadowMapSize; //full size
  2267. const uint16_t h = m_currentShadowMapSize/2; //half size
  2268. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, f, h);
  2269. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, 0, h, f, h);
  2270. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, 0, h, f);
  2271. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, h, 0, h, f);
  2272. }
  2273. else
  2274. {
  2275. const uint16_t h = m_currentShadowMapSize/2; //half size
  2276. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, h, h);
  2277. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, h, 0, h, h);
  2278. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, h, h, h);
  2279. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, h, h, h, h);
  2280. }
  2281. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2282. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2283. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2284. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2285. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX, depthRectY, depthRectWidth, depthRectHeight);
  2286. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_0_ID, screenView, screenProj);
  2287. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[TetrahedronFaces::Green], lightProj[ProjType::Horizontal]);
  2288. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_2_ID, lightView[TetrahedronFaces::Yellow], lightProj[ProjType::Horizontal]);
  2289. if(m_settings.m_stencilPack)
  2290. {
  2291. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[TetrahedronFaces::Blue], lightProj[ProjType::Vertical]);
  2292. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[TetrahedronFaces::Red], lightProj[ProjType::Vertical]);
  2293. }
  2294. else
  2295. {
  2296. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[TetrahedronFaces::Blue], lightProj[ProjType::Horizontal]);
  2297. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[TetrahedronFaces::Red], lightProj[ProjType::Horizontal]);
  2298. }
  2299. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2300. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2301. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2302. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2303. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2304. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_0_ID, s_rtShadowMap[0]);
  2305. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2306. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_2_ID, s_rtShadowMap[0]);
  2307. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_3_ID, s_rtShadowMap[0]);
  2308. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_4_ID, s_rtShadowMap[0]);
  2309. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur);
  2310. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]);
  2311. }
  2312. else // LightType::DirectionalLight == settings.m_lightType
  2313. {
  2314. /**
  2315. * RENDERVIEW_SHADOWMAP_1_ID - Craft shadow map for first split.
  2316. * RENDERVIEW_SHADOWMAP_2_ID - Craft shadow map for second split.
  2317. * RENDERVIEW_SHADOWMAP_3_ID - Craft shadow map for third split.
  2318. * RENDERVIEW_SHADOWMAP_4_ID - Craft shadow map for fourth split.
  2319. * RENDERVIEW_VBLUR_0_ID - Vertical blur for first split.
  2320. * RENDERVIEW_HBLUR_0_ID - Horizontal blur for first split.
  2321. * RENDERVIEW_VBLUR_1_ID - Vertical blur for second split.
  2322. * RENDERVIEW_HBLUR_1_ID - Horizontal blur for second split.
  2323. * RENDERVIEW_VBLUR_2_ID - Vertical blur for third split.
  2324. * RENDERVIEW_HBLUR_2_ID - Horizontal blur for third split.
  2325. * RENDERVIEW_VBLUR_3_ID - Vertical blur for fourth split.
  2326. * RENDERVIEW_HBLUR_3_ID - Horizontal blur for fourth split.
  2327. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2328. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2329. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer for first split.
  2330. * RENDERVIEW_DRAWDEPTH_1_ID - Draw depth buffer for second split.
  2331. * RENDERVIEW_DRAWDEPTH_2_ID - Draw depth buffer for third split.
  2332. * RENDERVIEW_DRAWDEPTH_3_ID - Draw depth buffer for fourth split.
  2333. */
  2334. depthRectHeight = m_viewState.m_height / 3;
  2335. depthRectWidth = depthRectHeight;
  2336. depthRectX = 0;
  2337. depthRectY = m_viewState.m_height - depthRectHeight;
  2338. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2339. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2340. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2341. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2342. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2343. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2344. bgfx::setViewRect(RENDERVIEW_VBLUR_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2345. bgfx::setViewRect(RENDERVIEW_HBLUR_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2346. bgfx::setViewRect(RENDERVIEW_VBLUR_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2347. bgfx::setViewRect(RENDERVIEW_HBLUR_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2348. bgfx::setViewRect(RENDERVIEW_VBLUR_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2349. bgfx::setViewRect(RENDERVIEW_HBLUR_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2350. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2351. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2352. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX+(0*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2353. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_1_ID, depthRectX+(1*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2354. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_2_ID, depthRectX+(2*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2355. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_3_ID, depthRectX+(3*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2356. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[0], lightProj[0]);
  2357. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_2_ID, lightView[0], lightProj[1]);
  2358. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[0], lightProj[2]);
  2359. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[0], lightProj[3]);
  2360. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2361. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2362. bgfx::setViewTransform(RENDERVIEW_VBLUR_1_ID, screenView, screenProj);
  2363. bgfx::setViewTransform(RENDERVIEW_HBLUR_1_ID, screenView, screenProj);
  2364. bgfx::setViewTransform(RENDERVIEW_VBLUR_2_ID, screenView, screenProj);
  2365. bgfx::setViewTransform(RENDERVIEW_HBLUR_2_ID, screenView, screenProj);
  2366. bgfx::setViewTransform(RENDERVIEW_VBLUR_3_ID, screenView, screenProj);
  2367. bgfx::setViewTransform(RENDERVIEW_HBLUR_3_ID, screenView, screenProj);
  2368. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2369. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2370. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2371. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_1_ID, screenView, screenProj);
  2372. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_2_ID, screenView, screenProj);
  2373. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_3_ID, screenView, screenProj);
  2374. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2375. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_2_ID, s_rtShadowMap[1]);
  2376. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_3_ID, s_rtShadowMap[2]);
  2377. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_4_ID, s_rtShadowMap[3]);
  2378. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur); //vblur
  2379. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]); //hblur
  2380. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_1_ID, s_rtBlur); //vblur
  2381. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_1_ID, s_rtShadowMap[1]); //hblur
  2382. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_2_ID, s_rtBlur); //vblur
  2383. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_2_ID, s_rtShadowMap[2]); //hblur
  2384. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_3_ID, s_rtBlur); //vblur
  2385. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_3_ID, s_rtShadowMap[3]); //hblur
  2386. }
  2387. // Clear backbuffer at beginning.
  2388. bgfx::setViewClear(0
  2389. , BGFX_CLEAR_COLOR
  2390. | BGFX_CLEAR_DEPTH
  2391. , m_clearValues.m_clearRgba
  2392. , m_clearValues.m_clearDepth
  2393. , m_clearValues.m_clearStencil
  2394. );
  2395. bgfx::touch(0);
  2396. // Clear shadowmap rendertarget at beginning.
  2397. const uint8_t flags0 = (LightType::DirectionalLight == m_settings.m_lightType)
  2398. ? 0
  2399. : BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL
  2400. ;
  2401. bgfx::setViewClear(RENDERVIEW_SHADOWMAP_0_ID
  2402. , flags0
  2403. , 0xfefefefe //blur fails on completely white regions
  2404. , m_clearValues.m_clearDepth
  2405. , m_clearValues.m_clearStencil
  2406. );
  2407. bgfx::touch(RENDERVIEW_SHADOWMAP_0_ID);
  2408. const uint8_t flags1 = (LightType::DirectionalLight == m_settings.m_lightType)
  2409. ? BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  2410. : 0
  2411. ;
  2412. for (uint8_t ii = 0; ii < 4; ++ii)
  2413. {
  2414. bgfx::setViewClear(RENDERVIEW_SHADOWMAP_1_ID+ii
  2415. , flags1
  2416. , 0xfefefefe //blur fails on completely white regions
  2417. , m_clearValues.m_clearDepth
  2418. , m_clearValues.m_clearStencil
  2419. );
  2420. bgfx::touch(RENDERVIEW_SHADOWMAP_1_ID+ii);
  2421. }
  2422. // Render.
  2423. // Craft shadow map.
  2424. {
  2425. // Craft stencil mask for point light shadow map packing.
  2426. if(LightType::PointLight == m_settings.m_lightType && m_settings.m_stencilPack)
  2427. {
  2428. if (6 == bgfx::getAvailTransientVertexBuffer(6, m_posDecl) )
  2429. {
  2430. struct Pos
  2431. {
  2432. float m_x, m_y, m_z;
  2433. };
  2434. bgfx::TransientVertexBuffer vb;
  2435. bgfx::allocTransientVertexBuffer(&vb, 6, m_posDecl);
  2436. Pos* vertex = (Pos*)vb.data;
  2437. const float min = 0.0f;
  2438. const float max = 1.0f;
  2439. const float center = 0.5f;
  2440. const float zz = 0.0f;
  2441. vertex[0].m_x = min;
  2442. vertex[0].m_y = min;
  2443. vertex[0].m_z = zz;
  2444. vertex[1].m_x = max;
  2445. vertex[1].m_y = min;
  2446. vertex[1].m_z = zz;
  2447. vertex[2].m_x = center;
  2448. vertex[2].m_y = center;
  2449. vertex[2].m_z = zz;
  2450. vertex[3].m_x = center;
  2451. vertex[3].m_y = center;
  2452. vertex[3].m_z = zz;
  2453. vertex[4].m_x = max;
  2454. vertex[4].m_y = max;
  2455. vertex[4].m_z = zz;
  2456. vertex[5].m_x = min;
  2457. vertex[5].m_y = max;
  2458. vertex[5].m_z = zz;
  2459. bgfx::setState(0);
  2460. bgfx::setStencil(BGFX_STENCIL_TEST_ALWAYS
  2461. | BGFX_STENCIL_FUNC_REF(1)
  2462. | BGFX_STENCIL_FUNC_RMASK(0xff)
  2463. | BGFX_STENCIL_OP_FAIL_S_REPLACE
  2464. | BGFX_STENCIL_OP_FAIL_Z_REPLACE
  2465. | BGFX_STENCIL_OP_PASS_Z_REPLACE
  2466. );
  2467. bgfx::setVertexBuffer(0, &vb);
  2468. bgfx::submit(RENDERVIEW_SHADOWMAP_0_ID, s_programs.m_black);
  2469. }
  2470. }
  2471. // Draw scene into shadowmap.
  2472. uint8_t drawNum;
  2473. if (LightType::SpotLight == m_settings.m_lightType)
  2474. {
  2475. drawNum = 1;
  2476. }
  2477. else if (LightType::PointLight == m_settings.m_lightType)
  2478. {
  2479. drawNum = 4;
  2480. }
  2481. else //LightType::DirectionalLight == settings.m_lightType)
  2482. {
  2483. drawNum = uint8_t(m_settings.m_numSplits);
  2484. }
  2485. for (uint8_t ii = 0; ii < drawNum; ++ii)
  2486. {
  2487. const uint8_t viewId = RENDERVIEW_SHADOWMAP_1_ID + ii;
  2488. uint8_t renderStateIndex = RenderState::ShadowMap_PackDepth;
  2489. if(LightType::PointLight == m_settings.m_lightType && m_settings.m_stencilPack)
  2490. {
  2491. renderStateIndex = uint8_t( (ii < 2) ? RenderState::ShadowMap_PackDepthHoriz : RenderState::ShadowMap_PackDepthVert);
  2492. }
  2493. // Floor.
  2494. m_hplaneMesh.submit(viewId
  2495. , mtxFloor
  2496. , *currentSmSettings->m_progPack
  2497. , s_renderStates[renderStateIndex]
  2498. );
  2499. // Bunny.
  2500. m_bunnyMesh.submit(viewId
  2501. , mtxBunny
  2502. , *currentSmSettings->m_progPack
  2503. , s_renderStates[renderStateIndex]
  2504. );
  2505. // Hollow cube.
  2506. m_hollowcubeMesh.submit(viewId
  2507. , mtxHollowcube
  2508. , *currentSmSettings->m_progPack
  2509. , s_renderStates[renderStateIndex]
  2510. );
  2511. // Cube.
  2512. m_cubeMesh.submit(viewId
  2513. , mtxCube
  2514. , *currentSmSettings->m_progPack
  2515. , s_renderStates[renderStateIndex]
  2516. );
  2517. // Trees.
  2518. for (uint8_t jj = 0; jj < numTrees; ++jj)
  2519. {
  2520. m_treeMesh.submit(viewId
  2521. , mtxTrees[jj]
  2522. , *currentSmSettings->m_progPack
  2523. , s_renderStates[renderStateIndex]
  2524. );
  2525. }
  2526. }
  2527. }
  2528. PackDepth::Enum depthType = (SmImpl::VSM == m_settings.m_smImpl) ? PackDepth::VSM : PackDepth::RGBA;
  2529. bool bVsmOrEsm = (SmImpl::VSM == m_settings.m_smImpl) || (SmImpl::ESM == m_settings.m_smImpl);
  2530. // Blur shadow map.
  2531. if (bVsmOrEsm
  2532. && currentSmSettings->m_doBlur)
  2533. {
  2534. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[0]) );
  2535. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2536. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2537. bgfx::submit(RENDERVIEW_VBLUR_0_ID, s_programs.m_vBlur[depthType]);
  2538. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtBlur) );
  2539. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2540. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2541. bgfx::submit(RENDERVIEW_HBLUR_0_ID, s_programs.m_hBlur[depthType]);
  2542. if (LightType::DirectionalLight == m_settings.m_lightType)
  2543. {
  2544. for (uint8_t ii = 1, jj = 2; ii < m_settings.m_numSplits; ++ii, jj+=2)
  2545. {
  2546. const uint8_t viewId = RENDERVIEW_VBLUR_0_ID + jj;
  2547. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[ii]) );
  2548. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2549. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2550. bgfx::submit(viewId, s_programs.m_vBlur[depthType]);
  2551. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtBlur) );
  2552. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2553. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2554. bgfx::submit(viewId+1, s_programs.m_hBlur[depthType]);
  2555. }
  2556. }
  2557. }
  2558. // Draw scene.
  2559. {
  2560. // Setup shadow mtx.
  2561. float mtxShadow[16];
  2562. const float ymul = (s_flipV) ? 0.5f : -0.5f;
  2563. float zadd = (DepthImpl::Linear == m_settings.m_depthImpl) ? 0.0f : 0.5f;
  2564. const float mtxBias[16] =
  2565. {
  2566. 0.5f, 0.0f, 0.0f, 0.0f,
  2567. 0.0f, ymul, 0.0f, 0.0f,
  2568. 0.0f, 0.0f, 0.5f, 0.0f,
  2569. 0.5f, 0.5f, zadd, 1.0f,
  2570. };
  2571. if (LightType::SpotLight == m_settings.m_lightType)
  2572. {
  2573. float mtxTmp[16];
  2574. bx::mtxMul(mtxTmp, lightProj[ProjType::Horizontal], mtxBias);
  2575. bx::mtxMul(mtxShadow, lightView[0], mtxTmp); //lightViewProjBias
  2576. }
  2577. else if (LightType::PointLight == m_settings.m_lightType)
  2578. {
  2579. const float s = (s_flipV) ? 1.0f : -1.0f; //sign
  2580. zadd = (DepthImpl::Linear == m_settings.m_depthImpl) ? 0.0f : 0.5f;
  2581. const float mtxCropBias[2][TetrahedronFaces::Count][16] =
  2582. {
  2583. { // settings.m_stencilPack == false
  2584. { // D3D: Green, OGL: Blue
  2585. 0.25f, 0.0f, 0.0f, 0.0f,
  2586. 0.0f, s*0.25f, 0.0f, 0.0f,
  2587. 0.0f, 0.0f, 0.5f, 0.0f,
  2588. 0.25f, 0.25f, zadd, 1.0f,
  2589. },
  2590. { // D3D: Yellow, OGL: Red
  2591. 0.25f, 0.0f, 0.0f, 0.0f,
  2592. 0.0f, s*0.25f, 0.0f, 0.0f,
  2593. 0.0f, 0.0f, 0.5f, 0.0f,
  2594. 0.75f, 0.25f, zadd, 1.0f,
  2595. },
  2596. { // D3D: Blue, OGL: Green
  2597. 0.25f, 0.0f, 0.0f, 0.0f,
  2598. 0.0f, s*0.25f, 0.0f, 0.0f,
  2599. 0.0f, 0.0f, 0.5f, 0.0f,
  2600. 0.25f, 0.75f, zadd, 1.0f,
  2601. },
  2602. { // D3D: Red, OGL: Yellow
  2603. 0.25f, 0.0f, 0.0f, 0.0f,
  2604. 0.0f, s*0.25f, 0.0f, 0.0f,
  2605. 0.0f, 0.0f, 0.5f, 0.0f,
  2606. 0.75f, 0.75f, zadd, 1.0f,
  2607. },
  2608. },
  2609. { // settings.m_stencilPack == true
  2610. { // D3D: Red, OGL: Blue
  2611. 0.25f, 0.0f, 0.0f, 0.0f,
  2612. 0.0f, s*0.5f, 0.0f, 0.0f,
  2613. 0.0f, 0.0f, 0.5f, 0.0f,
  2614. 0.25f, 0.5f, zadd, 1.0f,
  2615. },
  2616. { // D3D: Blue, OGL: Red
  2617. 0.25f, 0.0f, 0.0f, 0.0f,
  2618. 0.0f, s*0.5f, 0.0f, 0.0f,
  2619. 0.0f, 0.0f, 0.5f, 0.0f,
  2620. 0.75f, 0.5f, zadd, 1.0f,
  2621. },
  2622. { // D3D: Green, OGL: Green
  2623. 0.5f, 0.0f, 0.0f, 0.0f,
  2624. 0.0f, s*0.25f, 0.0f, 0.0f,
  2625. 0.0f, 0.0f, 0.5f, 0.0f,
  2626. 0.5f, 0.75f, zadd, 1.0f,
  2627. },
  2628. { // D3D: Yellow, OGL: Yellow
  2629. 0.5f, 0.0f, 0.0f, 0.0f,
  2630. 0.0f, s*0.25f, 0.0f, 0.0f,
  2631. 0.0f, 0.0f, 0.5f, 0.0f,
  2632. 0.5f, 0.25f, zadd, 1.0f,
  2633. },
  2634. }
  2635. };
  2636. //Use as: [stencilPack][flipV][tetrahedronFace]
  2637. static const uint8_t cropBiasIndices[2][2][4] =
  2638. {
  2639. { // settings.m_stencilPack == false
  2640. { 0, 1, 2, 3 }, //flipV == false
  2641. { 2, 3, 0, 1 }, //flipV == true
  2642. },
  2643. { // settings.m_stencilPack == true
  2644. { 3, 2, 0, 1 }, //flipV == false
  2645. { 2, 3, 0, 1 }, //flipV == true
  2646. },
  2647. };
  2648. for (uint8_t ii = 0; ii < TetrahedronFaces::Count; ++ii)
  2649. {
  2650. ProjType::Enum projType = (m_settings.m_stencilPack) ? ProjType::Enum(ii>1) : ProjType::Horizontal;
  2651. uint8_t biasIndex = cropBiasIndices[m_settings.m_stencilPack][uint8_t(s_flipV)][ii];
  2652. float mtxTmp[16];
  2653. bx::mtxMul(mtxTmp, mtxYpr[ii], lightProj[projType]);
  2654. bx::mtxMul(m_shadowMapMtx[ii], mtxTmp, mtxCropBias[m_settings.m_stencilPack][biasIndex]); //mtxYprProjBias
  2655. }
  2656. bx::mtxTranslate(mtxShadow //lightInvTranslate
  2657. , -m_pointLight.m_position.m_v[0]
  2658. , -m_pointLight.m_position.m_v[1]
  2659. , -m_pointLight.m_position.m_v[2]
  2660. );
  2661. }
  2662. else //LightType::DirectionalLight == settings.m_lightType
  2663. {
  2664. for (uint8_t ii = 0; ii < m_settings.m_numSplits; ++ii)
  2665. {
  2666. float mtxTmp[16];
  2667. bx::mtxMul(mtxTmp, lightProj[ii], mtxBias);
  2668. bx::mtxMul(m_shadowMapMtx[ii], lightView[0], mtxTmp); //lViewProjCropBias
  2669. }
  2670. }
  2671. // Floor.
  2672. if (LightType::DirectionalLight != m_settings.m_lightType)
  2673. {
  2674. bx::mtxMul(m_lightMtx, mtxFloor, mtxShadow); //not needed for directional light
  2675. }
  2676. m_hplaneMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2677. , mtxFloor
  2678. , *currentSmSettings->m_progDraw
  2679. , s_renderStates[RenderState::Default]
  2680. , true
  2681. );
  2682. // Bunny.
  2683. if (LightType::DirectionalLight != m_settings.m_lightType)
  2684. {
  2685. bx::mtxMul(m_lightMtx, mtxBunny, mtxShadow);
  2686. }
  2687. m_bunnyMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2688. , mtxBunny
  2689. , *currentSmSettings->m_progDraw
  2690. , s_renderStates[RenderState::Default]
  2691. , true
  2692. );
  2693. // Hollow cube.
  2694. if (LightType::DirectionalLight != m_settings.m_lightType)
  2695. {
  2696. bx::mtxMul(m_lightMtx, mtxHollowcube, mtxShadow);
  2697. }
  2698. m_hollowcubeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2699. , mtxHollowcube
  2700. , *currentSmSettings->m_progDraw
  2701. , s_renderStates[RenderState::Default]
  2702. , true
  2703. );
  2704. // Cube.
  2705. if (LightType::DirectionalLight != m_settings.m_lightType)
  2706. {
  2707. bx::mtxMul(m_lightMtx, mtxCube, mtxShadow);
  2708. }
  2709. m_cubeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2710. , mtxCube
  2711. , *currentSmSettings->m_progDraw
  2712. , s_renderStates[RenderState::Default]
  2713. , true
  2714. );
  2715. // Trees.
  2716. for (uint8_t ii = 0; ii < numTrees; ++ii)
  2717. {
  2718. if (LightType::DirectionalLight != m_settings.m_lightType)
  2719. {
  2720. bx::mtxMul(m_lightMtx, mtxTrees[ii], mtxShadow);
  2721. }
  2722. m_treeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2723. , mtxTrees[ii]
  2724. , *currentSmSettings->m_progDraw
  2725. , s_renderStates[RenderState::Default]
  2726. , true
  2727. );
  2728. }
  2729. // Lights.
  2730. if (LightType::SpotLight == m_settings.m_lightType || LightType::PointLight == m_settings.m_lightType)
  2731. {
  2732. const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
  2733. float mtx[16];
  2734. mtxBillboard(mtx, m_viewState.m_view, m_pointLight.m_position.m_v, lightScale);
  2735. m_vplaneMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2736. , mtx
  2737. , s_programs.m_colorTexture
  2738. , s_renderStates[RenderState::Custom_BlendLightTexture]
  2739. , m_texFlare
  2740. );
  2741. }
  2742. // Draw floor bottom.
  2743. float floorBottomMtx[16];
  2744. bx::mtxSRT(floorBottomMtx
  2745. , floorScale //scaleX
  2746. , floorScale //scaleY
  2747. , floorScale //scaleZ
  2748. , 0.0f //rotX
  2749. , 0.0f //rotY
  2750. , 0.0f //rotZ
  2751. , 0.0f //translateX
  2752. , -0.1f //translateY
  2753. , 0.0f //translateZ
  2754. );
  2755. m_hplaneMesh.submit(RENDERVIEW_DRAWSCENE_1_ID
  2756. , floorBottomMtx
  2757. , s_programs.m_texture
  2758. , s_renderStates[RenderState::Custom_DrawPlaneBottom]
  2759. , m_texFigure
  2760. );
  2761. }
  2762. // Draw depth rect.
  2763. if (m_settings.m_drawDepthBuffer)
  2764. {
  2765. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[0]) );
  2766. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2767. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2768. bgfx::submit(RENDERVIEW_DRAWDEPTH_0_ID, s_programs.m_drawDepth[depthType]);
  2769. if (LightType::DirectionalLight == m_settings.m_lightType)
  2770. {
  2771. for (uint8_t ii = 1; ii < m_settings.m_numSplits; ++ii)
  2772. {
  2773. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[ii]) );
  2774. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2775. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2776. bgfx::submit(RENDERVIEW_DRAWDEPTH_0_ID+ii, s_programs.m_drawDepth[depthType]);
  2777. }
  2778. }
  2779. }
  2780. // Update render target size.
  2781. uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo);
  2782. if (bLtChanged || m_currentShadowMapSize != shadowMapSize)
  2783. {
  2784. m_currentShadowMapSize = shadowMapSize;
  2785. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  2786. {
  2787. bgfx::destroy(s_rtShadowMap[0]);
  2788. bgfx::TextureHandle fbtextures[] =
  2789. {
  2790. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  2791. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT),
  2792. };
  2793. s_rtShadowMap[0] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  2794. }
  2795. if (LightType::DirectionalLight == m_settings.m_lightType)
  2796. {
  2797. for (uint8_t ii = 1; ii < ShadowMapRenderTargets::Count; ++ii)
  2798. {
  2799. {
  2800. bgfx::destroy(s_rtShadowMap[ii]);
  2801. bgfx::TextureHandle fbtextures[] =
  2802. {
  2803. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  2804. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT),
  2805. };
  2806. s_rtShadowMap[ii] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  2807. }
  2808. }
  2809. }
  2810. bgfx::destroy(s_rtBlur);
  2811. s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8);
  2812. }
  2813. // Advance to next frame. Rendering thread will be kicked to
  2814. // process submitted rendering primitives.
  2815. bgfx::frame();
  2816. return true;
  2817. }
  2818. return false;
  2819. }
  2820. entry::MouseState m_mouseState;
  2821. uint32_t m_width;
  2822. uint32_t m_height;
  2823. uint32_t m_debug;
  2824. uint32_t m_reset;
  2825. ViewState m_viewState;
  2826. ClearValues m_clearValues;
  2827. bgfx::VertexDecl m_posDecl;
  2828. bgfx::TextureHandle m_texFigure;
  2829. bgfx::TextureHandle m_texFlare;
  2830. bgfx::TextureHandle m_texFieldstone;
  2831. Mesh m_bunnyMesh;
  2832. Mesh m_treeMesh;
  2833. Mesh m_cubeMesh;
  2834. Mesh m_hollowcubeMesh;
  2835. Mesh m_hplaneMesh;
  2836. Mesh m_vplaneMesh;
  2837. float m_color[4];
  2838. Material m_defaultMaterial;
  2839. Light m_pointLight;
  2840. Light m_directionalLight;
  2841. float m_lightMtx[16];
  2842. float m_shadowMapMtx[ShadowMapRenderTargets::Count][16];
  2843. ShadowMapSettings m_smSettings[LightType::Count][DepthImpl::Count][SmImpl::Count];
  2844. SceneSettings m_settings;
  2845. uint16_t m_currentShadowMapSize;
  2846. float m_timeAccumulatorLight;
  2847. float m_timeAccumulatorScene;
  2848. };
  2849. } // namespace
  2850. ENTRY_IMPLEMENT_MAIN(ExampleShadowmaps, "16-shadowmaps", "Shadow maps example.");