shadowmaps.cpp 105 KB

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