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