shadowmaps.cpp 107 KB

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