shadowmaps.cpp 106 KB

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