2
0

shadowmaps.cpp 107 KB

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