shadowmaps.cpp 107 KB

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