shadowmaps.cpp 106 KB

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