shadowmaps.cpp 107 KB

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