shadowmaps.cpp 106 KB

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