shadowmaps.cpp 104 KB

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