shadowmaps.cpp 107 KB

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