shadowmaps.cpp 107 KB

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