shadowmaps.cpp 103 KB

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