shadowmaps.cpp 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254
  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/math.h>
  13. #include <bx/file.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::sin(_roll);
  186. float croll = bx::cos(_roll);
  187. float spitch = bx::sin(_pitch);
  188. float cpitch = bx::cos(_pitch);
  189. float syaw = bx::sin(_yaw);
  190. float cyaw = bx::cos(_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::destroy(u_params0);
  423. bgfx::destroy(u_params1);
  424. bgfx::destroy(u_params2);
  425. bgfx::destroy(u_color);
  426. bgfx::destroy(u_smSamplingParams);
  427. bgfx::destroy(u_csmFarDistances);
  428. bgfx::destroy(u_materialKa);
  429. bgfx::destroy(u_materialKd);
  430. bgfx::destroy(u_materialKs);
  431. bgfx::destroy(u_tetraNormalGreen);
  432. bgfx::destroy(u_tetraNormalYellow);
  433. bgfx::destroy(u_tetraNormalBlue);
  434. bgfx::destroy(u_tetraNormalRed);
  435. bgfx::destroy(u_shadowMapMtx0);
  436. bgfx::destroy(u_shadowMapMtx1);
  437. bgfx::destroy(u_shadowMapMtx2);
  438. bgfx::destroy(u_shadowMapMtx3);
  439. bgfx::destroy(u_lightMtx);
  440. bgfx::destroy(u_lightPosition);
  441. bgfx::destroy(u_lightAmbientPower);
  442. bgfx::destroy(u_lightDiffusePower);
  443. bgfx::destroy(u_lightSpecularPower);
  444. bgfx::destroy(u_lightSpotDirectionInner);
  445. bgfx::destroy(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_WRITE_RGB
  553. | BGFX_STATE_WRITE_A
  554. | BGFX_STATE_DEPTH_TEST_LESS
  555. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  565. | BGFX_STATE_WRITE_A
  566. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  577. | BGFX_STATE_WRITE_A
  578. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  594. | BGFX_STATE_WRITE_A
  595. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  610. | BGFX_STATE_WRITE_A
  611. | BGFX_STATE_WRITE_Z
  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_WRITE_RGB
  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::destroy(group.m_vbh);
  793. if (bgfx::kInvalidHandle != group.m_ibh.idx)
  794. {
  795. bgfx::destroy(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 bx::Vec3 corners[numCorners] =
  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::store(&out[ii], bx::mul(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::pow(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::destroy(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::destroy(m_packDepth[ii][jj]);
  1024. }
  1025. }
  1026. // Draw depth.
  1027. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  1028. {
  1029. bgfx::destroy(m_drawDepth[ii]);
  1030. }
  1031. // Hblur.
  1032. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  1033. {
  1034. bgfx::destroy(m_hBlur[ii]);
  1035. }
  1036. // Vblur.
  1037. for (uint8_t ii = 0; ii < PackDepth::Count; ++ii)
  1038. {
  1039. bgfx::destroy(m_vBlur[ii]);
  1040. }
  1041. // Misc.
  1042. bgfx::destroy(m_colorTexture);
  1043. bgfx::destroy(m_texture);
  1044. bgfx::destroy(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(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  1103. {
  1104. Args args(_argc, _argv);
  1105. m_debug = BGFX_DEBUG_NONE;
  1106. m_reset = BGFX_RESET_VSYNC;
  1107. m_width = _width;
  1108. m_height = _height;
  1109. m_viewState = ViewState(uint16_t(m_width), uint16_t(m_height));
  1110. m_clearValues = ClearValues(0x00000000, 1.0f, 0);
  1111. bgfx::Init init;
  1112. init.type = args.m_type;
  1113. init.vendorId = args.m_pciId;
  1114. init.resolution.width = m_viewState.m_width;
  1115. init.resolution.height = m_viewState.m_height;
  1116. init.resolution.reset = m_reset;
  1117. bgfx::init(init);
  1118. // Enable debug text.
  1119. bgfx::setDebug(m_debug);
  1120. // Setup root path for binary shaders. Shader binaries are different
  1121. // for each renderer.
  1122. switch (bgfx::getRendererType() )
  1123. {
  1124. case bgfx::RendererType::Direct3D9:
  1125. s_texelHalf = 0.5f;
  1126. break;
  1127. case bgfx::RendererType::OpenGL:
  1128. case bgfx::RendererType::OpenGLES:
  1129. s_flipV = true;
  1130. break;
  1131. default:
  1132. break;
  1133. }
  1134. // Imgui.
  1135. imguiCreate();
  1136. // Uniforms.
  1137. s_uniforms.init();
  1138. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  1139. s_shadowMap[0] = bgfx::createUniform("s_shadowMap0", bgfx::UniformType::Sampler);
  1140. s_shadowMap[1] = bgfx::createUniform("s_shadowMap1", bgfx::UniformType::Sampler);
  1141. s_shadowMap[2] = bgfx::createUniform("s_shadowMap2", bgfx::UniformType::Sampler);
  1142. s_shadowMap[3] = bgfx::createUniform("s_shadowMap3", bgfx::UniformType::Sampler);
  1143. // Programs.
  1144. s_programs.init();
  1145. // Vertex declarations.
  1146. bgfx::VertexDecl PosNormalTexcoordDecl;
  1147. PosNormalTexcoordDecl.begin()
  1148. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  1149. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  1150. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  1151. .end();
  1152. m_posDecl.begin();
  1153. m_posDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  1154. m_posDecl.end();
  1155. PosColorTexCoord0Vertex::init();
  1156. // Textures.
  1157. m_texFigure = loadTexture("textures/figure-rgba.dds");
  1158. m_texFlare = loadTexture("textures/flare.dds");
  1159. m_texFieldstone = loadTexture("textures/fieldstone-rgba.dds");
  1160. // Meshes.
  1161. m_bunnyMesh.load("meshes/bunny.bin");
  1162. m_treeMesh.load("meshes/tree.bin");
  1163. m_cubeMesh.load("meshes/cube.bin");
  1164. m_hollowcubeMesh.load("meshes/hollowcube.bin");
  1165. m_hplaneMesh.load(s_hplaneVertices, BX_COUNTOF(s_hplaneVertices), PosNormalTexcoordDecl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  1166. m_vplaneMesh.load(s_vplaneVertices, BX_COUNTOF(s_vplaneVertices), PosNormalTexcoordDecl, s_planeIndices, BX_COUNTOF(s_planeIndices) );
  1167. // Materials.
  1168. m_defaultMaterial =
  1169. {
  1170. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //ambient
  1171. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //diffuse
  1172. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular, exponent
  1173. };
  1174. // Lights.
  1175. m_pointLight =
  1176. {
  1177. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //position
  1178. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1179. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //ambient
  1180. { { 1.0f, 1.0f, 1.0f, 850.0f } }, //diffuse
  1181. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular
  1182. { { 0.0f,-0.4f,-0.6f, 0.0f } }, //spotdirection, spotexponent
  1183. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1184. { { 1.0f, 0.0f, 1.0f, 91.0f } }, //attenuation, spotcutoff
  1185. };
  1186. m_directionalLight =
  1187. {
  1188. { { 0.5f,-1.0f, 0.1f, 0.0f } }, //position
  1189. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1190. { { 1.0f, 1.0f, 1.0f, 0.02f } }, //ambient
  1191. { { 1.0f, 1.0f, 1.0f, 0.4f } }, //diffuse
  1192. { { 1.0f, 1.0f, 1.0f, 0.0f } }, //specular
  1193. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //spotdirection, spotexponent
  1194. { 0.0f, 0.0f, 0.0f, 0.0f }, //-ignore
  1195. { { 0.0f, 0.0f, 0.0f, 1.0f } }, //attenuation, spotcutoff
  1196. };
  1197. // Setup uniforms.
  1198. m_color[0] = m_color[1] = m_color[2] = m_color[3] = 1.0f;
  1199. s_uniforms.setPtrs(&m_defaultMaterial
  1200. , &m_pointLight
  1201. , m_color
  1202. , m_lightMtx
  1203. , &m_shadowMapMtx[ShadowMapRenderTargets::First][0]
  1204. , &m_shadowMapMtx[ShadowMapRenderTargets::Second][0]
  1205. , &m_shadowMapMtx[ShadowMapRenderTargets::Third][0]
  1206. , &m_shadowMapMtx[ShadowMapRenderTargets::Fourth][0]
  1207. );
  1208. s_uniforms.submitConstUniforms();
  1209. // Settings.
  1210. ShadowMapSettings smSettings[LightType::Count][DepthImpl::Count][SmImpl::Count] =
  1211. {
  1212. { //LightType::Spot
  1213. { //DepthImpl::InvZ
  1214. { //SmImpl::Hard
  1215. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1216. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1217. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1218. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1219. , 0.0035f, 0.0f, 0.01f, 0.00001f // m_bias
  1220. , 0.0012f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1221. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1222. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1223. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1224. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1225. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1226. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1227. , true // m_doBlur
  1228. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1229. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1230. },
  1231. { //SmImpl::PCF
  1232. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1233. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1234. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1235. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1236. , 0.007f, 0.0f, 0.01f, 0.00001f // m_bias
  1237. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1238. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1239. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1240. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1241. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1242. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1243. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1244. , true // m_doBlur
  1245. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1246. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1247. },
  1248. { //SmImpl::VSM
  1249. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1250. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1251. , 8.0f, 1.0f, 10.0f, 1.0f // m_near
  1252. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1253. , 0.045f, 0.0f, 0.1f, 0.00001f // m_bias
  1254. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1255. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1256. , 450.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1257. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1258. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1259. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1260. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1261. , true // m_doBlur
  1262. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1263. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1264. },
  1265. { //SmImpl::ESM
  1266. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1267. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1268. , 3.0f, 1.0f, 10.0f, 0.01f // m_near
  1269. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1270. , 0.02f, 0.0f, 0.3f, 0.00001f // m_bias
  1271. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1272. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1273. , 9000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1274. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1275. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1276. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1277. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1278. , true // m_doBlur
  1279. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1280. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1281. }
  1282. },
  1283. { //DepthImpl::Linear
  1284. { //SmImpl::Hard
  1285. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1286. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1287. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1288. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1289. , 0.0025f, 0.0f, 0.01f, 0.00001f // m_bias
  1290. , 0.0012f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1291. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1292. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1293. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1294. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1295. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1296. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1297. , true // m_doBlur
  1298. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1299. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1300. },
  1301. { //SmImpl::PCF
  1302. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1303. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1304. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1305. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1306. , 0.0025f, 0.0f, 0.01f, 0.00001f // m_bias
  1307. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1308. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1309. , 2000.0f, 1.0f, 2000.0f, 1.0f // m_customParam1
  1310. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1311. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1312. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1313. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1314. , true // m_doBlur
  1315. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1316. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1317. },
  1318. { //SmImpl::VSM
  1319. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1320. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1321. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1322. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1323. , 0.006f, 0.0f, 0.01f, 0.00001f // m_bias
  1324. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1325. , 0.02f, 0.0f, 0.1f, 0.00001f // m_customParam0
  1326. , 300.0f, 1.0f, 1500.0f, 1.0f // m_customParam1
  1327. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1328. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1329. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1330. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1331. , true // m_doBlur
  1332. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1333. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1334. },
  1335. { //SmImpl::ESM
  1336. 10.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1337. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1338. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1339. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1340. , 0.0055f, 0.0f, 0.01f, 0.00001f // m_bias
  1341. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1342. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1343. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1344. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1345. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1346. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1347. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1348. , true // m_doBlur
  1349. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1350. , &s_programs.m_colorLighting[SmType::Single][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1351. }
  1352. }
  1353. },
  1354. { //LightType::Point
  1355. { //DepthImpl::InvZ
  1356. { //SmImpl::Hard
  1357. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1358. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1359. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1360. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1361. , 0.006f, 0.0f, 0.01f, 0.00001f // m_bias
  1362. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1363. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1364. , 50.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1365. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1366. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1367. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1368. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1369. , true // m_doBlur
  1370. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1371. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1372. },
  1373. { //SmImpl::PCF
  1374. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1375. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1376. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1377. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1378. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1379. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1380. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1381. , 50.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1382. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1383. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1384. , 1.0f, 0.0f, 3.0f, 0.001f // m_xOffset
  1385. , 1.0f, 0.0f, 3.0f, 0.001f // m_yOffset
  1386. , true // m_doBlur
  1387. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1388. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1389. },
  1390. { //SmImpl::VSM
  1391. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1392. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1393. , 8.0f, 1.0f, 10.0f, 1.0f // m_near
  1394. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1395. , 0.055f, 0.0f, 0.1f, 0.00001f // m_bias
  1396. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1397. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1398. , 450.0f, 1.0f, 900.0f, 1.0f // m_customParam1
  1399. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1400. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1401. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1402. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1403. , true // m_doBlur
  1404. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1405. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1406. },
  1407. { //SmImpl::ESM
  1408. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1409. , 10.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1410. , 3.0f, 1.0f, 10.0f, 0.01f // m_near
  1411. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1412. , 0.035f, 0.0f, 0.1f, 0.00001f // m_bias
  1413. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1414. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1415. , 9000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1416. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1417. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1418. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1419. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1420. , true // m_doBlur
  1421. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1422. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1423. }
  1424. },
  1425. { //DepthImpl::Linear
  1426. { //SmImpl::Hard
  1427. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1428. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1429. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1430. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1431. , 0.003f, 0.0f, 0.01f, 0.00001f // m_bias
  1432. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1433. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1434. , 120.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1435. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1436. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1437. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1438. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1439. , true // m_doBlur
  1440. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1441. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1442. },
  1443. { //SmImpl::PCF
  1444. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1445. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1446. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1447. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1448. , 0.0035f, 0.0f, 0.01f, 0.00001f // m_bias
  1449. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1450. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1451. , 120.0f, 1.0f, 300.0f, 1.0f // m_customParam1
  1452. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1453. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1454. , 1.0f, 0.0f, 3.0f, 0.001f // m_xOffset
  1455. , 1.0f, 0.0f, 3.0f, 0.001f // m_yOffset
  1456. , true // m_doBlur
  1457. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1458. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1459. },
  1460. { //SmImpl::VSM
  1461. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1462. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1463. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1464. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1465. , 0.006f, 0.0f, 0.1f, 0.00001f // m_bias
  1466. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1467. , 0.02f, 0.0f, 0.1f, 0.00001f // m_customParam0
  1468. , 400.0f, 1.0f, 900.0f, 1.0f // m_customParam1
  1469. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1470. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1471. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1472. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1473. , true // m_doBlur
  1474. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1475. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1476. },
  1477. { //SmImpl::ESM
  1478. 12.0f, 9.0f, 12.0f, 1.0f // m_sizePwrTwo
  1479. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1480. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1481. , 250.0f, 100.0f, 2000.0f, 50.0f // m_far
  1482. , 0.007f, 0.0f, 0.01f, 0.00001f // m_bias
  1483. , 0.001f, 0.0f, 0.05f, 0.00001f // m_normalOffset
  1484. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1485. , 8000.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1486. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1487. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1488. , 0.25f, 0.0f, 2.0f, 0.001f // m_xOffset
  1489. , 0.25f, 0.0f, 2.0f, 0.001f // m_yOffset
  1490. , true // m_doBlur
  1491. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1492. , &s_programs.m_colorLighting[SmType::Omni][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1493. }
  1494. }
  1495. },
  1496. { //LightType::Directional
  1497. { //DepthImpl::InvZ
  1498. { //SmImpl::Hard
  1499. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1500. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1501. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1502. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1503. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1504. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1505. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1506. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1507. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1508. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1509. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1510. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1511. , true // m_doBlur
  1512. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1513. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::Hard] //m_progDraw
  1514. },
  1515. { //SmImpl::PCF
  1516. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1517. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1518. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1519. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1520. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1521. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1522. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1523. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1524. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1525. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1526. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1527. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1528. , true // m_doBlur
  1529. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1530. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::PCF] //m_progDraw
  1531. },
  1532. { //SmImpl::VSM
  1533. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1534. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1535. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1536. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1537. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1538. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1539. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1540. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1541. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1542. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1543. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1544. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1545. , true // m_doBlur
  1546. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::VSM] //m_progPack
  1547. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::VSM] //m_progDraw
  1548. },
  1549. { //SmImpl::ESM
  1550. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1551. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1552. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1553. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1554. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1555. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1556. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1557. , 9500.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1558. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1559. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1560. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1561. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1562. , true // m_doBlur
  1563. , &s_programs.m_packDepth[DepthImpl::InvZ][PackDepth::RGBA] //m_progPack
  1564. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::InvZ][SmImpl::ESM] //m_progDraw
  1565. }
  1566. },
  1567. { //DepthImpl::Linear
  1568. { //SmImpl::Hard
  1569. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1570. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1571. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1572. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1573. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1574. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1575. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1576. , 500.0f, 1.0f, 1000.0f, 1.0f // m_customParam1
  1577. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1578. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1579. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1580. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1581. , true // m_doBlur
  1582. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1583. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::Hard] //m_progDraw
  1584. },
  1585. { //SmImpl::PCF
  1586. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1587. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1588. , 1.0f, 1.0f, 99.0f, 1.0f // m_near
  1589. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1590. , 0.0012f, 0.0f, 0.01f, 0.00001f // m_bias
  1591. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1592. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1593. , 200.0f, 1.0f, 400.0f, 1.0f // m_customParam1
  1594. , 2.0f, 0.0f, 8.0f, 1.0f // m_xNum
  1595. , 2.0f, 0.0f, 8.0f, 1.0f // m_yNum
  1596. , 1.0f, 0.0f, 3.0f, 0.01f // m_xOffset
  1597. , 1.0f, 0.0f, 3.0f, 0.01f // m_yOffset
  1598. , true // m_doBlur
  1599. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1600. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::PCF] //m_progDraw
  1601. },
  1602. { //SmImpl::VSM
  1603. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1604. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1605. , 1.0f, 1.0f, 10.0f, 1.0f // m_near
  1606. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1607. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1608. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1609. , 0.02f, 0.0f, 0.04f, 0.00001f // m_customParam0
  1610. , 2500.0f, 1.0f, 5000.0f, 1.0f // m_customParam1
  1611. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1612. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1613. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1614. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1615. , true // m_doBlur
  1616. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::VSM] //m_progPack
  1617. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::VSM] //m_progDraw
  1618. },
  1619. { //SmImpl::ESM
  1620. 11.0f, 7.0f, 12.0f, 1.0f // m_sizePwrTwo
  1621. , 1.0f, 1.0f, 20.0f, 1.0f // m_depthValuePow
  1622. , 1.0f, 1.0f, 10.0f, 0.01f // m_near
  1623. , 550.0f, 100.0f, 2000.0f, 50.0f // m_far
  1624. , 0.004f, 0.0f, 0.01f, 0.00001f // m_bias
  1625. , 0.001f, 0.0f, 0.04f, 0.00001f // m_normalOffset
  1626. , 0.7f, 0.0f, 1.0f, 0.01f // m_customParam0
  1627. , 9500.0f, 1.0f, 15000.0f, 1.0f // m_customParam1
  1628. , 2.0f, 0.0f, 4.0f, 1.0f // m_xNum
  1629. , 2.0f, 0.0f, 4.0f, 1.0f // m_yNum
  1630. , 0.2f, 0.0f, 1.0f, 0.01f // m_xOffset
  1631. , 0.2f, 0.0f, 1.0f, 0.01f // m_yOffset
  1632. , true // m_doBlur
  1633. , &s_programs.m_packDepth[DepthImpl::Linear][PackDepth::RGBA] //m_progPack
  1634. , &s_programs.m_colorLighting[SmType::Cascade][DepthImpl::Linear][SmImpl::ESM] //m_progDraw
  1635. }
  1636. }
  1637. }
  1638. };
  1639. bx::memCopy(m_smSettings, smSettings, sizeof(smSettings));
  1640. m_settings.m_lightType = LightType::SpotLight;
  1641. m_settings.m_depthImpl = DepthImpl::InvZ;
  1642. m_settings.m_smImpl = SmImpl::Hard;
  1643. m_settings.m_spotOuterAngle = 45.0f;
  1644. m_settings.m_spotInnerAngle = 30.0f;
  1645. m_settings.m_fovXAdjust = 0.0f;
  1646. m_settings.m_fovYAdjust = 0.0f;
  1647. m_settings.m_coverageSpotL = 90.0f;
  1648. m_settings.m_splitDistribution = 0.6f;
  1649. m_settings.m_numSplits = 4;
  1650. m_settings.m_updateLights = true;
  1651. m_settings.m_updateScene = true;
  1652. m_settings.m_drawDepthBuffer = false;
  1653. m_settings.m_showSmCoverage = false;
  1654. m_settings.m_stencilPack = true;
  1655. m_settings.m_stabilize = true;
  1656. ShadowMapSettings* currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1657. // Render targets.
  1658. uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo);
  1659. m_currentShadowMapSize = shadowMapSize;
  1660. float currentShadowMapSizef = float(int16_t(m_currentShadowMapSize) );
  1661. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  1662. for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
  1663. {
  1664. bgfx::TextureHandle fbtextures[] =
  1665. {
  1666. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  1667. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT),
  1668. };
  1669. s_rtShadowMap[ii] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  1670. }
  1671. s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8);
  1672. // Setup camera.
  1673. cameraCreate();
  1674. cameraSetPosition({ 0.0f, 60.0f, -105.0f });
  1675. cameraSetVerticalAngle(-0.45f);
  1676. m_timeAccumulatorLight = 0.0f;
  1677. m_timeAccumulatorScene = 0.0f;
  1678. }
  1679. virtual int shutdown() override
  1680. {
  1681. m_bunnyMesh.unload();
  1682. m_treeMesh.unload();
  1683. m_cubeMesh.unload();
  1684. m_hollowcubeMesh.unload();
  1685. m_hplaneMesh.unload();
  1686. m_vplaneMesh.unload();
  1687. bgfx::destroy(m_texFigure);
  1688. bgfx::destroy(m_texFieldstone);
  1689. bgfx::destroy(m_texFlare);
  1690. for (uint8_t ii = 0; ii < ShadowMapRenderTargets::Count; ++ii)
  1691. {
  1692. bgfx::destroy(s_rtShadowMap[ii]);
  1693. }
  1694. bgfx::destroy(s_rtBlur);
  1695. s_programs.destroy();
  1696. bgfx::destroy(s_texColor);
  1697. bgfx::destroy(s_shadowMap[3]);
  1698. bgfx::destroy(s_shadowMap[2]);
  1699. bgfx::destroy(s_shadowMap[1]);
  1700. bgfx::destroy(s_shadowMap[0]);
  1701. s_uniforms.destroy();
  1702. cameraDestroy();
  1703. imguiDestroy();
  1704. // Shutdown bgfx.
  1705. bgfx::shutdown();
  1706. return 0;
  1707. }
  1708. bool update() override
  1709. {
  1710. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  1711. {
  1712. m_viewState.m_width = uint16_t(m_width);
  1713. m_viewState.m_height = uint16_t(m_height);
  1714. const bgfx::Caps* caps = bgfx::getCaps();
  1715. // Set view and projection matrices.
  1716. const float camFovy = 60.0f;
  1717. const float camAspect = float(int32_t(m_viewState.m_width) ) / float(int32_t(m_viewState.m_height) );
  1718. const float camNear = 0.1f;
  1719. const float camFar = 2000.0f;
  1720. const float projHeight = bx::tan(bx::toRad(camFovy)*0.5f);
  1721. const float projWidth = projHeight * camAspect;
  1722. bx::mtxProj(m_viewState.m_proj, camFovy, camAspect, camNear, camFar, caps->homogeneousDepth);
  1723. cameraGetViewMtx(m_viewState.m_view);
  1724. float currentShadowMapSizef = float(int16_t(m_currentShadowMapSize) );
  1725. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  1726. s_uniforms.submitConstUniforms();
  1727. // s_uniforms.submitConstUniforms();
  1728. // Imgui.
  1729. imguiBeginFrame(m_mouseState.m_mx
  1730. , m_mouseState.m_my
  1731. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1732. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1733. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1734. , m_mouseState.m_mz
  1735. , m_viewState.m_width
  1736. , m_viewState.m_height
  1737. );
  1738. showExampleDialog(this);
  1739. ImGui::SetNextWindowPos(
  1740. ImVec2(m_viewState.m_width - m_viewState.m_width / 5.0f - 10.0f, 10.0f)
  1741. , ImGuiCond_FirstUseEver
  1742. );
  1743. ImGui::SetNextWindowSize(
  1744. ImVec2(m_viewState.m_width / 5.0f, m_viewState.m_height - 20.0f)
  1745. , ImGuiCond_FirstUseEver
  1746. );
  1747. ImGui::Begin("Settings"
  1748. , NULL
  1749. , 0
  1750. );
  1751. #define IMGUI_FLOAT_SLIDER(_name, _val) \
  1752. ImGui::SliderFloat(_name \
  1753. , &_val \
  1754. , *( ((float*)&_val)+1) \
  1755. , *( ((float*)&_val)+2) \
  1756. )
  1757. #define IMGUI_RADIO_BUTTON(_name, _var, _val) \
  1758. if (ImGui::RadioButton(_name, _var == _val) ) \
  1759. { \
  1760. _var = _val; \
  1761. }
  1762. ImGui::Checkbox("Update lights", &m_settings.m_updateLights);
  1763. ImGui::Checkbox("Update scene", &m_settings.m_updateScene);
  1764. ImGui::Separator();
  1765. ImGui::Text("Shadow map depth:");
  1766. IMGUI_RADIO_BUTTON("InvZ", m_settings.m_depthImpl, DepthImpl::InvZ);
  1767. IMGUI_RADIO_BUTTON("Linear", m_settings.m_depthImpl, DepthImpl::Linear);
  1768. ShadowMapSettings* currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1769. ImGui::Separator();
  1770. ImGui::Checkbox("Draw depth buffer", &m_settings.m_drawDepthBuffer);
  1771. if (m_settings.m_drawDepthBuffer)
  1772. {
  1773. IMGUI_FLOAT_SLIDER("Depth value pow", currentSmSettings->m_depthValuePow);
  1774. }
  1775. ImGui::Separator();
  1776. ImGui::Text("Shadow Map implementation");
  1777. IMGUI_RADIO_BUTTON("Hard", m_settings.m_smImpl, SmImpl::Hard);
  1778. IMGUI_RADIO_BUTTON("PCF", m_settings.m_smImpl, SmImpl::PCF);
  1779. IMGUI_RADIO_BUTTON("VSM", m_settings.m_smImpl, SmImpl::VSM);
  1780. IMGUI_RADIO_BUTTON("ESM", m_settings.m_smImpl, SmImpl::ESM);
  1781. currentSmSettings = &m_smSettings[m_settings.m_lightType][m_settings.m_depthImpl][m_settings.m_smImpl];
  1782. ImGui::Separator();
  1783. IMGUI_FLOAT_SLIDER("Bias", currentSmSettings->m_bias);
  1784. IMGUI_FLOAT_SLIDER("Normal offset", currentSmSettings->m_normalOffset);
  1785. ImGui::Separator();
  1786. if (LightType::DirectionalLight != m_settings.m_lightType)
  1787. {
  1788. IMGUI_FLOAT_SLIDER("Near plane", currentSmSettings->m_near);
  1789. }
  1790. IMGUI_FLOAT_SLIDER("Far plane", currentSmSettings->m_far);
  1791. ImGui::Separator();
  1792. switch(m_settings.m_smImpl)
  1793. {
  1794. case SmImpl::Hard:
  1795. //ImGui::Text("Hard");
  1796. break;
  1797. case SmImpl::PCF:
  1798. ImGui::Text("PCF");
  1799. IMGUI_FLOAT_SLIDER("X Offset", currentSmSettings->m_xOffset);
  1800. IMGUI_FLOAT_SLIDER("Y Offset", currentSmSettings->m_yOffset);
  1801. break;
  1802. case SmImpl::VSM:
  1803. ImGui::Text("VSM");
  1804. IMGUI_FLOAT_SLIDER("Min variance", currentSmSettings->m_customParam0);
  1805. IMGUI_FLOAT_SLIDER("Depth multiplier", currentSmSettings->m_customParam1);
  1806. ImGui::Checkbox("Blur shadow map", &currentSmSettings->m_doBlur);
  1807. if (currentSmSettings->m_doBlur)
  1808. {
  1809. IMGUI_FLOAT_SLIDER("Blur X Offset", currentSmSettings->m_xOffset);
  1810. IMGUI_FLOAT_SLIDER("Blur Y Offset", currentSmSettings->m_yOffset);
  1811. }
  1812. break;
  1813. case SmImpl::ESM:
  1814. ImGui::Text("ESM");
  1815. IMGUI_FLOAT_SLIDER("ESM Hardness", currentSmSettings->m_customParam0);
  1816. IMGUI_FLOAT_SLIDER("Depth multiplier", currentSmSettings->m_customParam1);
  1817. ImGui::Checkbox("Blur shadow map", &currentSmSettings->m_doBlur);
  1818. if (currentSmSettings->m_doBlur)
  1819. {
  1820. IMGUI_FLOAT_SLIDER("Blur X Offset", currentSmSettings->m_xOffset);
  1821. IMGUI_FLOAT_SLIDER("Blur Y Offset", currentSmSettings->m_yOffset);
  1822. }
  1823. break;
  1824. default:
  1825. break;
  1826. };
  1827. ImGui::End();
  1828. #undef IMGUI_RADIO_BUTTON
  1829. ImGui::SetNextWindowPos(
  1830. ImVec2(10.0f, 260.0f)
  1831. , ImGuiCond_FirstUseEver
  1832. );
  1833. ImGui::SetNextWindowSize(
  1834. ImVec2(m_viewState.m_width / 5.0f, 350.0f)
  1835. , ImGuiCond_FirstUseEver
  1836. );
  1837. ImGui::Begin("Light"
  1838. , NULL
  1839. , 0
  1840. );
  1841. ImGui::PushItemWidth(185.0f);
  1842. bool bLtChanged = false;
  1843. if ( ImGui::RadioButton("Spot light", m_settings.m_lightType == LightType::SpotLight ))
  1844. {
  1845. m_settings.m_lightType = LightType::SpotLight; bLtChanged = true;
  1846. }
  1847. if ( ImGui::RadioButton("Point light", m_settings.m_lightType == LightType::PointLight ))
  1848. {
  1849. m_settings.m_lightType = LightType::PointLight; bLtChanged = true;
  1850. }
  1851. if ( ImGui::RadioButton("Directional light", m_settings.m_lightType == LightType::DirectionalLight ))
  1852. {
  1853. m_settings.m_lightType = LightType::DirectionalLight; bLtChanged = true;
  1854. }
  1855. ImGui::Separator();
  1856. ImGui::Checkbox("Show shadow map coverage.", &m_settings.m_showSmCoverage);
  1857. ImGui::Separator();
  1858. ImGui::Text("Shadow map resolution: %ux%u", m_currentShadowMapSize, m_currentShadowMapSize);
  1859. ImGui::SliderFloat("##SizePwrTwo", &currentSmSettings->m_sizePwrTwo,
  1860. currentSmSettings->m_sizePwrTwoMin,
  1861. currentSmSettings->m_sizePwrTwoMax, "%.0f");
  1862. ImGui::Separator();
  1863. if (LightType::SpotLight == m_settings.m_lightType)
  1864. {
  1865. ImGui::Text("Spot light");
  1866. ImGui::SliderFloat("Shadow map area", &m_settings.m_coverageSpotL, 45.0f, 120.0f);
  1867. ImGui::Separator();
  1868. ImGui::SliderFloat("Spot outer cone", &m_settings.m_spotOuterAngle, 0.0f, 91.0f);
  1869. ImGui::SliderFloat("Spot inner cone", &m_settings.m_spotInnerAngle, 0.0f, 90.0f);
  1870. }
  1871. else if (LightType::PointLight == m_settings.m_lightType)
  1872. {
  1873. ImGui::Text("Point light");
  1874. ImGui::Checkbox("Stencil pack", &m_settings.m_stencilPack);
  1875. ImGui::SliderFloat("Fov X adjust", &m_settings.m_fovXAdjust, -20.0f, 20.0f);
  1876. ImGui::SliderFloat("Fov Y adjust", &m_settings.m_fovYAdjust, -20.0f, 20.0f);
  1877. }
  1878. else if (LightType::DirectionalLight == m_settings.m_lightType)
  1879. {
  1880. ImGui::Text("Directional light");
  1881. ImGui::Checkbox("Stabilize cascades", &m_settings.m_stabilize);
  1882. ImGui::SliderInt("Cascade splits", &m_settings.m_numSplits, 1, 4);
  1883. ImGui::SliderFloat("Cascade distribution", &m_settings.m_splitDistribution, 0.0f, 1.0f);
  1884. }
  1885. #undef IMGUI_FLOAT_SLIDER
  1886. ImGui::End();
  1887. imguiEndFrame();
  1888. // Update uniforms.
  1889. s_uniforms.m_shadowMapBias = currentSmSettings->m_bias;
  1890. s_uniforms.m_shadowMapOffset = currentSmSettings->m_normalOffset;
  1891. s_uniforms.m_shadowMapParam0 = currentSmSettings->m_customParam0;
  1892. s_uniforms.m_shadowMapParam1 = currentSmSettings->m_customParam1;
  1893. s_uniforms.m_depthValuePow = currentSmSettings->m_depthValuePow;
  1894. s_uniforms.m_XNum = currentSmSettings->m_xNum;
  1895. s_uniforms.m_YNum = currentSmSettings->m_yNum;
  1896. s_uniforms.m_XOffset = currentSmSettings->m_xOffset;
  1897. s_uniforms.m_YOffset = currentSmSettings->m_yOffset;
  1898. s_uniforms.m_showSmCoverage = float(m_settings.m_showSmCoverage);
  1899. s_uniforms.m_lightPtr = (LightType::DirectionalLight == m_settings.m_lightType) ? &m_directionalLight : &m_pointLight;
  1900. if (LightType::SpotLight == m_settings.m_lightType)
  1901. {
  1902. m_pointLight.m_attenuationSpotOuter.m_outer = m_settings.m_spotOuterAngle;
  1903. m_pointLight.m_spotDirectionInner.m_inner = m_settings.m_spotInnerAngle;
  1904. }
  1905. else
  1906. {
  1907. m_pointLight.m_attenuationSpotOuter.m_outer = 91.0f; //above 90.0f means point light
  1908. }
  1909. s_uniforms.submitPerFrameUniforms();
  1910. // Time.
  1911. int64_t now = bx::getHPCounter();
  1912. static int64_t last = now;
  1913. const int64_t frameTime = now - last;
  1914. last = now;
  1915. const double freq = double(bx::getHPFrequency() );
  1916. const float deltaTime = float(frameTime/freq);
  1917. // Update camera.
  1918. cameraUpdate(deltaTime, m_mouseState);
  1919. // Update view mtx.
  1920. cameraGetViewMtx(m_viewState.m_view);
  1921. // Update lights.
  1922. m_pointLight.computeViewSpaceComponents(m_viewState.m_view);
  1923. m_directionalLight.computeViewSpaceComponents(m_viewState.m_view);
  1924. // Update time accumulators.
  1925. if (m_settings.m_updateLights) { m_timeAccumulatorLight += deltaTime; }
  1926. if (m_settings.m_updateScene) { m_timeAccumulatorScene += deltaTime; }
  1927. // Setup lights.
  1928. m_pointLight.m_position.m_x = bx::cos(m_timeAccumulatorLight) * 20.0f;
  1929. m_pointLight.m_position.m_y = 26.0f;
  1930. m_pointLight.m_position.m_z = bx::sin(m_timeAccumulatorLight) * 20.0f;
  1931. m_pointLight.m_spotDirectionInner.m_x = -m_pointLight.m_position.m_x;
  1932. m_pointLight.m_spotDirectionInner.m_y = -m_pointLight.m_position.m_y;
  1933. m_pointLight.m_spotDirectionInner.m_z = -m_pointLight.m_position.m_z;
  1934. m_directionalLight.m_position.m_x = -bx::cos(m_timeAccumulatorLight);
  1935. m_directionalLight.m_position.m_y = -1.0f;
  1936. m_directionalLight.m_position.m_z = -bx::sin(m_timeAccumulatorLight);
  1937. // Setup instance matrices.
  1938. float mtxFloor[16];
  1939. const float floorScale = 550.0f;
  1940. bx::mtxSRT(mtxFloor
  1941. , floorScale //scaleX
  1942. , floorScale //scaleY
  1943. , floorScale //scaleZ
  1944. , 0.0f //rotX
  1945. , 0.0f //rotY
  1946. , 0.0f //rotZ
  1947. , 0.0f //translateX
  1948. , 0.0f //translateY
  1949. , 0.0f //translateZ
  1950. );
  1951. float mtxBunny[16];
  1952. bx::mtxSRT(mtxBunny
  1953. , 5.0f
  1954. , 5.0f
  1955. , 5.0f
  1956. , 0.0f
  1957. , 1.56f - m_timeAccumulatorScene
  1958. , 0.0f
  1959. , 15.0f
  1960. , 5.0f
  1961. , 0.0f
  1962. );
  1963. float mtxHollowcube[16];
  1964. bx::mtxSRT(mtxHollowcube
  1965. , 2.5f
  1966. , 2.5f
  1967. , 2.5f
  1968. , 0.0f
  1969. , 1.56f - m_timeAccumulatorScene
  1970. , 0.0f
  1971. , 0.0f
  1972. , 10.0f
  1973. , 0.0f
  1974. );
  1975. float mtxCube[16];
  1976. bx::mtxSRT(mtxCube
  1977. , 2.5f
  1978. , 2.5f
  1979. , 2.5f
  1980. , 0.0f
  1981. , 1.56f - m_timeAccumulatorScene
  1982. , 0.0f
  1983. , -15.0f
  1984. , 5.0f
  1985. , 0.0f
  1986. );
  1987. const uint8_t numTrees = 10;
  1988. float mtxTrees[numTrees][16];
  1989. for (uint8_t ii = 0; ii < numTrees; ++ii)
  1990. {
  1991. bx::mtxSRT(mtxTrees[ii]
  1992. , 2.0f
  1993. , 2.0f
  1994. , 2.0f
  1995. , 0.0f
  1996. , float(ii)
  1997. , 0.0f
  1998. , bx::sin(float(ii)*2.0f*bx::kPi/float(numTrees) ) * 60.0f
  1999. , 0.0f
  2000. , bx::cos(float(ii)*2.0f*bx::kPi/float(numTrees) ) * 60.0f
  2001. );
  2002. }
  2003. // Compute transform matrices.
  2004. const uint8_t shadowMapPasses = ShadowMapRenderTargets::Count;
  2005. float lightView[shadowMapPasses][16];
  2006. float lightProj[shadowMapPasses][16];
  2007. float mtxYpr[TetrahedronFaces::Count][16];
  2008. float screenProj[16];
  2009. float screenView[16];
  2010. bx::mtxIdentity(screenView);
  2011. bx::mtxOrtho(
  2012. screenProj
  2013. , 0.0f
  2014. , 1.0f
  2015. , 1.0f
  2016. , 0.0f
  2017. , 0.0f
  2018. , 100.0f
  2019. , 0.0f
  2020. , caps->homogeneousDepth
  2021. );
  2022. // Update render target size.
  2023. uint16_t shadowMapSize = 1 << uint32_t(currentSmSettings->m_sizePwrTwo);
  2024. if (bLtChanged || m_currentShadowMapSize != shadowMapSize)
  2025. {
  2026. m_currentShadowMapSize = shadowMapSize;
  2027. s_uniforms.m_shadowMapTexelSize = 1.0f / currentShadowMapSizef;
  2028. {
  2029. bgfx::destroy(s_rtShadowMap[0]);
  2030. bgfx::TextureHandle fbtextures[] =
  2031. {
  2032. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  2033. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT),
  2034. };
  2035. s_rtShadowMap[0] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  2036. }
  2037. if (LightType::DirectionalLight == m_settings.m_lightType)
  2038. {
  2039. for (uint8_t ii = 1; ii < ShadowMapRenderTargets::Count; ++ii)
  2040. {
  2041. {
  2042. bgfx::destroy(s_rtShadowMap[ii]);
  2043. bgfx::TextureHandle fbtextures[] =
  2044. {
  2045. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  2046. bgfx::createTexture2D(m_currentShadowMapSize, m_currentShadowMapSize, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT),
  2047. };
  2048. s_rtShadowMap[ii] = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  2049. }
  2050. }
  2051. }
  2052. bgfx::destroy(s_rtBlur);
  2053. s_rtBlur = bgfx::createFrameBuffer(m_currentShadowMapSize, m_currentShadowMapSize, bgfx::TextureFormat::BGRA8);
  2054. }
  2055. if (LightType::SpotLight == m_settings.m_lightType)
  2056. {
  2057. const float fovy = m_settings.m_coverageSpotL;
  2058. const float aspect = 1.0f;
  2059. bx::mtxProj(
  2060. lightProj[ProjType::Horizontal]
  2061. , fovy
  2062. , aspect
  2063. , currentSmSettings->m_near
  2064. , currentSmSettings->m_far
  2065. , false
  2066. );
  2067. //For linear depth, prevent depth division by variable w-component in shaders and divide here by far plane
  2068. if (DepthImpl::Linear == m_settings.m_depthImpl)
  2069. {
  2070. lightProj[ProjType::Horizontal][10] /= currentSmSettings->m_far;
  2071. lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far;
  2072. }
  2073. const bx::Vec3 at = bx::add(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(m_pointLight.m_spotDirectionInner.m_v) );
  2074. bx::mtxLookAt(lightView[TetrahedronFaces::Green], bx::load<bx::Vec3>(m_pointLight.m_position.m_v), at);
  2075. }
  2076. else if (LightType::PointLight == m_settings.m_lightType)
  2077. {
  2078. float ypr[TetrahedronFaces::Count][3] =
  2079. {
  2080. { bx::toRad( 0.0f), bx::toRad( 27.36780516f), bx::toRad(0.0f) },
  2081. { bx::toRad(180.0f), bx::toRad( 27.36780516f), bx::toRad(0.0f) },
  2082. { bx::toRad(-90.0f), bx::toRad(-27.36780516f), bx::toRad(0.0f) },
  2083. { bx::toRad( 90.0f), bx::toRad(-27.36780516f), bx::toRad(0.0f) },
  2084. };
  2085. if (m_settings.m_stencilPack)
  2086. {
  2087. const float fovx = 143.98570868f + 3.51f + m_settings.m_fovXAdjust;
  2088. const float fovy = 125.26438968f + 9.85f + m_settings.m_fovYAdjust;
  2089. const float aspect = bx::tan(bx::toRad(fovx*0.5f) )/bx::tan(bx::toRad(fovy*0.5f) );
  2090. bx::mtxProj(
  2091. lightProj[ProjType::Vertical]
  2092. , fovx
  2093. , aspect
  2094. , currentSmSettings->m_near
  2095. , currentSmSettings->m_far
  2096. , false
  2097. );
  2098. //For linear depth, prevent depth division by variable w-component in shaders and divide here by far plane
  2099. if (DepthImpl::Linear == m_settings.m_depthImpl)
  2100. {
  2101. lightProj[ProjType::Vertical][10] /= currentSmSettings->m_far;
  2102. lightProj[ProjType::Vertical][14] /= currentSmSettings->m_far;
  2103. }
  2104. ypr[TetrahedronFaces::Green ][2] = bx::toRad(180.0f);
  2105. ypr[TetrahedronFaces::Yellow][2] = bx::toRad( 0.0f);
  2106. ypr[TetrahedronFaces::Blue ][2] = bx::toRad( 90.0f);
  2107. ypr[TetrahedronFaces::Red ][2] = bx::toRad(-90.0f);
  2108. }
  2109. const float fovx = 143.98570868f + 7.8f + m_settings.m_fovXAdjust;
  2110. const float fovy = 125.26438968f + 3.0f + m_settings.m_fovYAdjust;
  2111. const float aspect = bx::tan(bx::toRad(fovx*0.5f) )/bx::tan(bx::toRad(fovy*0.5f) );
  2112. bx::mtxProj(
  2113. lightProj[ProjType::Horizontal]
  2114. , fovy
  2115. , aspect
  2116. , currentSmSettings->m_near
  2117. , currentSmSettings->m_far
  2118. , caps->homogeneousDepth
  2119. );
  2120. //For linear depth, prevent depth division by variable w component in shaders and divide here by far plane
  2121. if (DepthImpl::Linear == m_settings.m_depthImpl)
  2122. {
  2123. lightProj[ProjType::Horizontal][10] /= currentSmSettings->m_far;
  2124. lightProj[ProjType::Horizontal][14] /= currentSmSettings->m_far;
  2125. }
  2126. for (uint8_t ii = 0; ii < TetrahedronFaces::Count; ++ii)
  2127. {
  2128. float mtxTmp[16];
  2129. mtxYawPitchRoll(mtxTmp, ypr[ii][0], ypr[ii][1], ypr[ii][2]);
  2130. float tmp[3] =
  2131. {
  2132. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[0]) ),
  2133. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[4]) ),
  2134. -bx::dot(bx::load<bx::Vec3>(m_pointLight.m_position.m_v), bx::load<bx::Vec3>(&mtxTmp[8]) ),
  2135. };
  2136. bx::mtxTranspose(mtxYpr[ii], mtxTmp);
  2137. bx::memCopy(lightView[ii], mtxYpr[ii], 12*sizeof(float) );
  2138. lightView[ii][12] = tmp[0];
  2139. lightView[ii][13] = tmp[1];
  2140. lightView[ii][14] = tmp[2];
  2141. lightView[ii][15] = 1.0f;
  2142. }
  2143. }
  2144. else // LightType::DirectionalLight == settings.m_lightType
  2145. {
  2146. // Setup light view mtx.
  2147. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  2148. const bx::Vec3 eye =
  2149. {
  2150. -m_directionalLight.m_position.m_x,
  2151. -m_directionalLight.m_position.m_y,
  2152. -m_directionalLight.m_position.m_z,
  2153. };
  2154. bx::mtxLookAt(lightView[0], eye, at);
  2155. // Compute camera inverse view mtx.
  2156. float mtxViewInv[16];
  2157. bx::mtxInverse(mtxViewInv, m_viewState.m_view);
  2158. // Compute split distances.
  2159. const uint8_t maxNumSplits = 4;
  2160. BX_CHECK(maxNumSplits >= settings.m_numSplits, "Error! Max num splits.");
  2161. float splitSlices[maxNumSplits*2];
  2162. splitFrustum(splitSlices
  2163. , uint8_t(m_settings.m_numSplits)
  2164. , currentSmSettings->m_near
  2165. , currentSmSettings->m_far
  2166. , m_settings.m_splitDistribution
  2167. );
  2168. // Update uniforms.
  2169. for (uint8_t ii = 0, ff = 1; ii < m_settings.m_numSplits; ++ii, ff+=2)
  2170. {
  2171. // This lags for 1 frame, but it's not a problem.
  2172. s_uniforms.m_csmFarDistances[ii] = splitSlices[ff];
  2173. }
  2174. float mtxProj[16];
  2175. bx::mtxOrtho(
  2176. mtxProj
  2177. , 1.0f
  2178. , -1.0f
  2179. , 1.0f
  2180. , -1.0f
  2181. , -currentSmSettings->m_far
  2182. , currentSmSettings->m_far
  2183. , 0.0f
  2184. , caps->homogeneousDepth
  2185. );
  2186. const uint8_t numCorners = 8;
  2187. float frustumCorners[maxNumSplits][numCorners][3];
  2188. for (uint8_t ii = 0, nn = 0, ff = 1; ii < m_settings.m_numSplits; ++ii, nn+=2, ff+=2)
  2189. {
  2190. // Compute frustum corners for one split in world space.
  2191. worldSpaceFrustumCorners( (float*)frustumCorners[ii], splitSlices[nn], splitSlices[ff], projWidth, projHeight, mtxViewInv);
  2192. bx::Vec3 min = { 9000.0f, 9000.0f, 9000.0f };
  2193. bx::Vec3 max = { -9000.0f, -9000.0f, -9000.0f };
  2194. for (uint8_t jj = 0; jj < numCorners; ++jj)
  2195. {
  2196. // Transform to light space.
  2197. const bx::Vec3 xyz = bx::mul(bx::load<bx::Vec3>(frustumCorners[ii][jj]), lightView[0]);
  2198. // Update bounding box.
  2199. min = bx::min(min, xyz);
  2200. max = bx::max(max, xyz);
  2201. }
  2202. const bx::Vec3 minproj = bx::mulH(min, mtxProj);
  2203. const bx::Vec3 maxproj = bx::mulH(max, mtxProj);
  2204. float scalex = 2.0f / (maxproj.x - minproj.x);
  2205. float scaley = 2.0f / (maxproj.y - minproj.y);
  2206. if (m_settings.m_stabilize)
  2207. {
  2208. const float quantizer = 64.0f;
  2209. scalex = quantizer / bx::ceil(quantizer / scalex);
  2210. scaley = quantizer / bx::ceil(quantizer / scaley);
  2211. }
  2212. float offsetx = 0.5f * (maxproj.x + minproj.x) * scalex;
  2213. float offsety = 0.5f * (maxproj.y + minproj.y) * scaley;
  2214. if (m_settings.m_stabilize)
  2215. {
  2216. const float halfSize = currentShadowMapSizef * 0.5f;
  2217. offsetx = bx::ceil(offsetx * halfSize) / halfSize;
  2218. offsety = bx::ceil(offsety * halfSize) / halfSize;
  2219. }
  2220. float mtxCrop[16];
  2221. bx::mtxIdentity(mtxCrop);
  2222. mtxCrop[ 0] = scalex;
  2223. mtxCrop[ 5] = scaley;
  2224. mtxCrop[12] = offsetx;
  2225. mtxCrop[13] = offsety;
  2226. bx::mtxMul(lightProj[ii], mtxCrop, mtxProj);
  2227. }
  2228. }
  2229. // Reset render targets.
  2230. const bgfx::FrameBufferHandle invalidRt = BGFX_INVALID_HANDLE;
  2231. for (uint8_t ii = 0; ii < RENDERVIEW_DRAWDEPTH_3_ID+1; ++ii)
  2232. {
  2233. bgfx::setViewFrameBuffer(ii, invalidRt);
  2234. bgfx::setViewRect(ii, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2235. }
  2236. // Determine on-screen rectangle size where depth buffer will be drawn.
  2237. uint16_t depthRectHeight = uint16_t(float(m_viewState.m_height) / 2.5f);
  2238. uint16_t depthRectWidth = depthRectHeight;
  2239. uint16_t depthRectX = 0;
  2240. uint16_t depthRectY = m_viewState.m_height - depthRectHeight;
  2241. // Setup views and render targets.
  2242. bgfx::setViewRect(0, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2243. bgfx::setViewTransform(0, m_viewState.m_view, m_viewState.m_proj);
  2244. if (LightType::SpotLight == m_settings.m_lightType)
  2245. {
  2246. /**
  2247. * RENDERVIEW_SHADOWMAP_0_ID - Clear shadow map. (used as convenience, otherwise render_pass_1 could be cleared)
  2248. * RENDERVIEW_SHADOWMAP_1_ID - Craft shadow map.
  2249. * RENDERVIEW_VBLUR_0_ID - Vertical blur.
  2250. * RENDERVIEW_HBLUR_0_ID - Horizontal blur.
  2251. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2252. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2253. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer.
  2254. */
  2255. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2256. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2257. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2258. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2259. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2260. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2261. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX, depthRectY, depthRectWidth, depthRectHeight);
  2262. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_0_ID, screenView, screenProj);
  2263. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[0], lightProj[ProjType::Horizontal]);
  2264. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2265. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2266. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2267. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2268. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2269. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_0_ID, s_rtShadowMap[0]);
  2270. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2271. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur);
  2272. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]);
  2273. }
  2274. else if (LightType::PointLight == m_settings.m_lightType)
  2275. {
  2276. /**
  2277. * RENDERVIEW_SHADOWMAP_0_ID - Clear entire shadow map.
  2278. * RENDERVIEW_SHADOWMAP_1_ID - Craft green tetrahedron shadow face.
  2279. * RENDERVIEW_SHADOWMAP_2_ID - Craft yellow tetrahedron shadow face.
  2280. * RENDERVIEW_SHADOWMAP_3_ID - Craft blue tetrahedron shadow face.
  2281. * RENDERVIEW_SHADOWMAP_4_ID - Craft red tetrahedron shadow face.
  2282. * RENDERVIEW_VBLUR_0_ID - Vertical blur.
  2283. * RENDERVIEW_HBLUR_0_ID - Horizontal blur.
  2284. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2285. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2286. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer.
  2287. */
  2288. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2289. if (m_settings.m_stencilPack)
  2290. {
  2291. const uint16_t f = m_currentShadowMapSize; //full size
  2292. const uint16_t h = m_currentShadowMapSize/2; //half size
  2293. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, f, h);
  2294. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, 0, h, f, h);
  2295. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, 0, h, f);
  2296. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, h, 0, h, f);
  2297. }
  2298. else
  2299. {
  2300. const uint16_t h = m_currentShadowMapSize/2; //half size
  2301. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, h, h);
  2302. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, h, 0, h, h);
  2303. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, h, h, h);
  2304. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, h, h, h, h);
  2305. }
  2306. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2307. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2308. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2309. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2310. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX, depthRectY, depthRectWidth, depthRectHeight);
  2311. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_0_ID, screenView, screenProj);
  2312. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[TetrahedronFaces::Green], lightProj[ProjType::Horizontal]);
  2313. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_2_ID, lightView[TetrahedronFaces::Yellow], lightProj[ProjType::Horizontal]);
  2314. if(m_settings.m_stencilPack)
  2315. {
  2316. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[TetrahedronFaces::Blue], lightProj[ProjType::Vertical]);
  2317. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[TetrahedronFaces::Red], lightProj[ProjType::Vertical]);
  2318. }
  2319. else
  2320. {
  2321. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[TetrahedronFaces::Blue], lightProj[ProjType::Horizontal]);
  2322. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[TetrahedronFaces::Red], lightProj[ProjType::Horizontal]);
  2323. }
  2324. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2325. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2326. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2327. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2328. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2329. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_0_ID, s_rtShadowMap[0]);
  2330. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2331. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_2_ID, s_rtShadowMap[0]);
  2332. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_3_ID, s_rtShadowMap[0]);
  2333. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_4_ID, s_rtShadowMap[0]);
  2334. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur);
  2335. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]);
  2336. }
  2337. else // LightType::DirectionalLight == settings.m_lightType
  2338. {
  2339. /**
  2340. * RENDERVIEW_SHADOWMAP_1_ID - Craft shadow map for first split.
  2341. * RENDERVIEW_SHADOWMAP_2_ID - Craft shadow map for second split.
  2342. * RENDERVIEW_SHADOWMAP_3_ID - Craft shadow map for third split.
  2343. * RENDERVIEW_SHADOWMAP_4_ID - Craft shadow map for fourth split.
  2344. * RENDERVIEW_VBLUR_0_ID - Vertical blur for first split.
  2345. * RENDERVIEW_HBLUR_0_ID - Horizontal blur for first split.
  2346. * RENDERVIEW_VBLUR_1_ID - Vertical blur for second split.
  2347. * RENDERVIEW_HBLUR_1_ID - Horizontal blur for second split.
  2348. * RENDERVIEW_VBLUR_2_ID - Vertical blur for third split.
  2349. * RENDERVIEW_HBLUR_2_ID - Horizontal blur for third split.
  2350. * RENDERVIEW_VBLUR_3_ID - Vertical blur for fourth split.
  2351. * RENDERVIEW_HBLUR_3_ID - Horizontal blur for fourth split.
  2352. * RENDERVIEW_DRAWSCENE_0_ID - Draw scene.
  2353. * RENDERVIEW_DRAWSCENE_1_ID - Draw floor bottom.
  2354. * RENDERVIEW_DRAWDEPTH_0_ID - Draw depth buffer for first split.
  2355. * RENDERVIEW_DRAWDEPTH_1_ID - Draw depth buffer for second split.
  2356. * RENDERVIEW_DRAWDEPTH_2_ID - Draw depth buffer for third split.
  2357. * RENDERVIEW_DRAWDEPTH_3_ID - Draw depth buffer for fourth split.
  2358. */
  2359. depthRectHeight = m_viewState.m_height / 3;
  2360. depthRectWidth = depthRectHeight;
  2361. depthRectX = 0;
  2362. depthRectY = m_viewState.m_height - depthRectHeight;
  2363. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2364. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2365. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2366. bgfx::setViewRect(RENDERVIEW_SHADOWMAP_4_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2367. bgfx::setViewRect(RENDERVIEW_VBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2368. bgfx::setViewRect(RENDERVIEW_HBLUR_0_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2369. bgfx::setViewRect(RENDERVIEW_VBLUR_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2370. bgfx::setViewRect(RENDERVIEW_HBLUR_1_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2371. bgfx::setViewRect(RENDERVIEW_VBLUR_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2372. bgfx::setViewRect(RENDERVIEW_HBLUR_2_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2373. bgfx::setViewRect(RENDERVIEW_VBLUR_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2374. bgfx::setViewRect(RENDERVIEW_HBLUR_3_ID, 0, 0, m_currentShadowMapSize, m_currentShadowMapSize);
  2375. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_0_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2376. bgfx::setViewRect(RENDERVIEW_DRAWSCENE_1_ID, 0, 0, m_viewState.m_width, m_viewState.m_height);
  2377. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_0_ID, depthRectX+(0*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2378. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_1_ID, depthRectX+(1*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2379. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_2_ID, depthRectX+(2*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2380. bgfx::setViewRect(RENDERVIEW_DRAWDEPTH_3_ID, depthRectX+(3*depthRectWidth), depthRectY, depthRectWidth, depthRectHeight);
  2381. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_1_ID, lightView[0], lightProj[0]);
  2382. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_2_ID, lightView[0], lightProj[1]);
  2383. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_3_ID, lightView[0], lightProj[2]);
  2384. bgfx::setViewTransform(RENDERVIEW_SHADOWMAP_4_ID, lightView[0], lightProj[3]);
  2385. bgfx::setViewTransform(RENDERVIEW_VBLUR_0_ID, screenView, screenProj);
  2386. bgfx::setViewTransform(RENDERVIEW_HBLUR_0_ID, screenView, screenProj);
  2387. bgfx::setViewTransform(RENDERVIEW_VBLUR_1_ID, screenView, screenProj);
  2388. bgfx::setViewTransform(RENDERVIEW_HBLUR_1_ID, screenView, screenProj);
  2389. bgfx::setViewTransform(RENDERVIEW_VBLUR_2_ID, screenView, screenProj);
  2390. bgfx::setViewTransform(RENDERVIEW_HBLUR_2_ID, screenView, screenProj);
  2391. bgfx::setViewTransform(RENDERVIEW_VBLUR_3_ID, screenView, screenProj);
  2392. bgfx::setViewTransform(RENDERVIEW_HBLUR_3_ID, screenView, screenProj);
  2393. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_0_ID, m_viewState.m_view, m_viewState.m_proj);
  2394. bgfx::setViewTransform(RENDERVIEW_DRAWSCENE_1_ID, m_viewState.m_view, m_viewState.m_proj);
  2395. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_0_ID, screenView, screenProj);
  2396. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_1_ID, screenView, screenProj);
  2397. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_2_ID, screenView, screenProj);
  2398. bgfx::setViewTransform(RENDERVIEW_DRAWDEPTH_3_ID, screenView, screenProj);
  2399. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_1_ID, s_rtShadowMap[0]);
  2400. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_2_ID, s_rtShadowMap[1]);
  2401. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_3_ID, s_rtShadowMap[2]);
  2402. bgfx::setViewFrameBuffer(RENDERVIEW_SHADOWMAP_4_ID, s_rtShadowMap[3]);
  2403. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_0_ID, s_rtBlur); //vblur
  2404. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_0_ID, s_rtShadowMap[0]); //hblur
  2405. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_1_ID, s_rtBlur); //vblur
  2406. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_1_ID, s_rtShadowMap[1]); //hblur
  2407. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_2_ID, s_rtBlur); //vblur
  2408. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_2_ID, s_rtShadowMap[2]); //hblur
  2409. bgfx::setViewFrameBuffer(RENDERVIEW_VBLUR_3_ID, s_rtBlur); //vblur
  2410. bgfx::setViewFrameBuffer(RENDERVIEW_HBLUR_3_ID, s_rtShadowMap[3]); //hblur
  2411. }
  2412. // Clear backbuffer at beginning.
  2413. bgfx::setViewClear(0
  2414. , BGFX_CLEAR_COLOR
  2415. | BGFX_CLEAR_DEPTH
  2416. , m_clearValues.m_clearRgba
  2417. , m_clearValues.m_clearDepth
  2418. , m_clearValues.m_clearStencil
  2419. );
  2420. bgfx::touch(0);
  2421. // Clear shadowmap rendertarget at beginning.
  2422. const uint8_t flags0 = (LightType::DirectionalLight == m_settings.m_lightType)
  2423. ? 0
  2424. : BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH | BGFX_CLEAR_STENCIL
  2425. ;
  2426. bgfx::setViewClear(RENDERVIEW_SHADOWMAP_0_ID
  2427. , flags0
  2428. , 0xfefefefe //blur fails on completely white regions
  2429. , m_clearValues.m_clearDepth
  2430. , m_clearValues.m_clearStencil
  2431. );
  2432. bgfx::touch(RENDERVIEW_SHADOWMAP_0_ID);
  2433. const uint8_t flags1 = (LightType::DirectionalLight == m_settings.m_lightType)
  2434. ? BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  2435. : 0
  2436. ;
  2437. for (uint8_t ii = 0; ii < 4; ++ii)
  2438. {
  2439. bgfx::setViewClear(RENDERVIEW_SHADOWMAP_1_ID+ii
  2440. , flags1
  2441. , 0xfefefefe //blur fails on completely white regions
  2442. , m_clearValues.m_clearDepth
  2443. , m_clearValues.m_clearStencil
  2444. );
  2445. bgfx::touch(RENDERVIEW_SHADOWMAP_1_ID+ii);
  2446. }
  2447. // Render.
  2448. // Craft shadow map.
  2449. {
  2450. // Craft stencil mask for point light shadow map packing.
  2451. if(LightType::PointLight == m_settings.m_lightType && m_settings.m_stencilPack)
  2452. {
  2453. if (6 == bgfx::getAvailTransientVertexBuffer(6, m_posDecl) )
  2454. {
  2455. struct Pos
  2456. {
  2457. float m_x, m_y, m_z;
  2458. };
  2459. bgfx::TransientVertexBuffer vb;
  2460. bgfx::allocTransientVertexBuffer(&vb, 6, m_posDecl);
  2461. Pos* vertex = (Pos*)vb.data;
  2462. const float min = 0.0f;
  2463. const float max = 1.0f;
  2464. const float center = 0.5f;
  2465. const float zz = 0.0f;
  2466. vertex[0].m_x = min;
  2467. vertex[0].m_y = min;
  2468. vertex[0].m_z = zz;
  2469. vertex[1].m_x = max;
  2470. vertex[1].m_y = min;
  2471. vertex[1].m_z = zz;
  2472. vertex[2].m_x = center;
  2473. vertex[2].m_y = center;
  2474. vertex[2].m_z = zz;
  2475. vertex[3].m_x = center;
  2476. vertex[3].m_y = center;
  2477. vertex[3].m_z = zz;
  2478. vertex[4].m_x = max;
  2479. vertex[4].m_y = max;
  2480. vertex[4].m_z = zz;
  2481. vertex[5].m_x = min;
  2482. vertex[5].m_y = max;
  2483. vertex[5].m_z = zz;
  2484. bgfx::setState(0);
  2485. bgfx::setStencil(BGFX_STENCIL_TEST_ALWAYS
  2486. | BGFX_STENCIL_FUNC_REF(1)
  2487. | BGFX_STENCIL_FUNC_RMASK(0xff)
  2488. | BGFX_STENCIL_OP_FAIL_S_REPLACE
  2489. | BGFX_STENCIL_OP_FAIL_Z_REPLACE
  2490. | BGFX_STENCIL_OP_PASS_Z_REPLACE
  2491. );
  2492. bgfx::setVertexBuffer(0, &vb);
  2493. bgfx::submit(RENDERVIEW_SHADOWMAP_0_ID, s_programs.m_black);
  2494. }
  2495. }
  2496. // Draw scene into shadowmap.
  2497. uint8_t drawNum;
  2498. if (LightType::SpotLight == m_settings.m_lightType)
  2499. {
  2500. drawNum = 1;
  2501. }
  2502. else if (LightType::PointLight == m_settings.m_lightType)
  2503. {
  2504. drawNum = 4;
  2505. }
  2506. else //LightType::DirectionalLight == settings.m_lightType)
  2507. {
  2508. drawNum = uint8_t(m_settings.m_numSplits);
  2509. }
  2510. for (uint8_t ii = 0; ii < drawNum; ++ii)
  2511. {
  2512. const uint8_t viewId = RENDERVIEW_SHADOWMAP_1_ID + ii;
  2513. uint8_t renderStateIndex = RenderState::ShadowMap_PackDepth;
  2514. if(LightType::PointLight == m_settings.m_lightType && m_settings.m_stencilPack)
  2515. {
  2516. renderStateIndex = uint8_t( (ii < 2) ? RenderState::ShadowMap_PackDepthHoriz : RenderState::ShadowMap_PackDepthVert);
  2517. }
  2518. // Floor.
  2519. m_hplaneMesh.submit(viewId
  2520. , mtxFloor
  2521. , *currentSmSettings->m_progPack
  2522. , s_renderStates[renderStateIndex]
  2523. );
  2524. // Bunny.
  2525. m_bunnyMesh.submit(viewId
  2526. , mtxBunny
  2527. , *currentSmSettings->m_progPack
  2528. , s_renderStates[renderStateIndex]
  2529. );
  2530. // Hollow cube.
  2531. m_hollowcubeMesh.submit(viewId
  2532. , mtxHollowcube
  2533. , *currentSmSettings->m_progPack
  2534. , s_renderStates[renderStateIndex]
  2535. );
  2536. // Cube.
  2537. m_cubeMesh.submit(viewId
  2538. , mtxCube
  2539. , *currentSmSettings->m_progPack
  2540. , s_renderStates[renderStateIndex]
  2541. );
  2542. // Trees.
  2543. for (uint8_t jj = 0; jj < numTrees; ++jj)
  2544. {
  2545. m_treeMesh.submit(viewId
  2546. , mtxTrees[jj]
  2547. , *currentSmSettings->m_progPack
  2548. , s_renderStates[renderStateIndex]
  2549. );
  2550. }
  2551. }
  2552. }
  2553. PackDepth::Enum depthType = (SmImpl::VSM == m_settings.m_smImpl) ? PackDepth::VSM : PackDepth::RGBA;
  2554. bool bVsmOrEsm = (SmImpl::VSM == m_settings.m_smImpl) || (SmImpl::ESM == m_settings.m_smImpl);
  2555. // Blur shadow map.
  2556. if (bVsmOrEsm
  2557. && currentSmSettings->m_doBlur)
  2558. {
  2559. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[0]) );
  2560. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2561. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2562. bgfx::submit(RENDERVIEW_VBLUR_0_ID, s_programs.m_vBlur[depthType]);
  2563. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtBlur) );
  2564. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2565. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2566. bgfx::submit(RENDERVIEW_HBLUR_0_ID, s_programs.m_hBlur[depthType]);
  2567. if (LightType::DirectionalLight == m_settings.m_lightType)
  2568. {
  2569. for (uint8_t ii = 1, jj = 2; ii < m_settings.m_numSplits; ++ii, jj+=2)
  2570. {
  2571. const uint8_t viewId = RENDERVIEW_VBLUR_0_ID + jj;
  2572. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[ii]) );
  2573. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2574. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2575. bgfx::submit(viewId, s_programs.m_vBlur[depthType]);
  2576. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtBlur) );
  2577. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2578. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2579. bgfx::submit(viewId+1, s_programs.m_hBlur[depthType]);
  2580. }
  2581. }
  2582. }
  2583. // Draw scene.
  2584. {
  2585. // Setup shadow mtx.
  2586. float mtxShadow[16];
  2587. const float ymul = (s_flipV) ? 0.5f : -0.5f;
  2588. float zadd = (DepthImpl::Linear == m_settings.m_depthImpl) ? 0.0f : 0.5f;
  2589. const float mtxBias[16] =
  2590. {
  2591. 0.5f, 0.0f, 0.0f, 0.0f,
  2592. 0.0f, ymul, 0.0f, 0.0f,
  2593. 0.0f, 0.0f, 0.5f, 0.0f,
  2594. 0.5f, 0.5f, zadd, 1.0f,
  2595. };
  2596. if (LightType::SpotLight == m_settings.m_lightType)
  2597. {
  2598. float mtxTmp[16];
  2599. bx::mtxMul(mtxTmp, lightProj[ProjType::Horizontal], mtxBias);
  2600. bx::mtxMul(mtxShadow, lightView[0], mtxTmp); //lightViewProjBias
  2601. }
  2602. else if (LightType::PointLight == m_settings.m_lightType)
  2603. {
  2604. const float s = (s_flipV) ? 1.0f : -1.0f; //sign
  2605. zadd = (DepthImpl::Linear == m_settings.m_depthImpl) ? 0.0f : 0.5f;
  2606. const float mtxCropBias[2][TetrahedronFaces::Count][16] =
  2607. {
  2608. { // settings.m_stencilPack == false
  2609. { // D3D: Green, OGL: Blue
  2610. 0.25f, 0.0f, 0.0f, 0.0f,
  2611. 0.0f, s*0.25f, 0.0f, 0.0f,
  2612. 0.0f, 0.0f, 0.5f, 0.0f,
  2613. 0.25f, 0.25f, zadd, 1.0f,
  2614. },
  2615. { // D3D: Yellow, OGL: Red
  2616. 0.25f, 0.0f, 0.0f, 0.0f,
  2617. 0.0f, s*0.25f, 0.0f, 0.0f,
  2618. 0.0f, 0.0f, 0.5f, 0.0f,
  2619. 0.75f, 0.25f, zadd, 1.0f,
  2620. },
  2621. { // D3D: Blue, OGL: Green
  2622. 0.25f, 0.0f, 0.0f, 0.0f,
  2623. 0.0f, s*0.25f, 0.0f, 0.0f,
  2624. 0.0f, 0.0f, 0.5f, 0.0f,
  2625. 0.25f, 0.75f, zadd, 1.0f,
  2626. },
  2627. { // D3D: Red, OGL: Yellow
  2628. 0.25f, 0.0f, 0.0f, 0.0f,
  2629. 0.0f, s*0.25f, 0.0f, 0.0f,
  2630. 0.0f, 0.0f, 0.5f, 0.0f,
  2631. 0.75f, 0.75f, zadd, 1.0f,
  2632. },
  2633. },
  2634. { // settings.m_stencilPack == true
  2635. { // D3D: Red, OGL: Blue
  2636. 0.25f, 0.0f, 0.0f, 0.0f,
  2637. 0.0f, s*0.5f, 0.0f, 0.0f,
  2638. 0.0f, 0.0f, 0.5f, 0.0f,
  2639. 0.25f, 0.5f, zadd, 1.0f,
  2640. },
  2641. { // D3D: Blue, OGL: Red
  2642. 0.25f, 0.0f, 0.0f, 0.0f,
  2643. 0.0f, s*0.5f, 0.0f, 0.0f,
  2644. 0.0f, 0.0f, 0.5f, 0.0f,
  2645. 0.75f, 0.5f, zadd, 1.0f,
  2646. },
  2647. { // D3D: Green, OGL: Green
  2648. 0.5f, 0.0f, 0.0f, 0.0f,
  2649. 0.0f, s*0.25f, 0.0f, 0.0f,
  2650. 0.0f, 0.0f, 0.5f, 0.0f,
  2651. 0.5f, 0.75f, zadd, 1.0f,
  2652. },
  2653. { // D3D: Yellow, OGL: Yellow
  2654. 0.5f, 0.0f, 0.0f, 0.0f,
  2655. 0.0f, s*0.25f, 0.0f, 0.0f,
  2656. 0.0f, 0.0f, 0.5f, 0.0f,
  2657. 0.5f, 0.25f, zadd, 1.0f,
  2658. },
  2659. }
  2660. };
  2661. //Use as: [stencilPack][flipV][tetrahedronFace]
  2662. static const uint8_t cropBiasIndices[2][2][4] =
  2663. {
  2664. { // settings.m_stencilPack == false
  2665. { 0, 1, 2, 3 }, //flipV == false
  2666. { 2, 3, 0, 1 }, //flipV == true
  2667. },
  2668. { // settings.m_stencilPack == true
  2669. { 3, 2, 0, 1 }, //flipV == false
  2670. { 2, 3, 0, 1 }, //flipV == true
  2671. },
  2672. };
  2673. for (uint8_t ii = 0; ii < TetrahedronFaces::Count; ++ii)
  2674. {
  2675. ProjType::Enum projType = (m_settings.m_stencilPack) ? ProjType::Enum(ii>1) : ProjType::Horizontal;
  2676. uint8_t biasIndex = cropBiasIndices[m_settings.m_stencilPack][uint8_t(s_flipV)][ii];
  2677. float mtxTmp[16];
  2678. bx::mtxMul(mtxTmp, mtxYpr[ii], lightProj[projType]);
  2679. bx::mtxMul(m_shadowMapMtx[ii], mtxTmp, mtxCropBias[m_settings.m_stencilPack][biasIndex]); //mtxYprProjBias
  2680. }
  2681. bx::mtxTranslate(mtxShadow //lightInvTranslate
  2682. , -m_pointLight.m_position.m_v[0]
  2683. , -m_pointLight.m_position.m_v[1]
  2684. , -m_pointLight.m_position.m_v[2]
  2685. );
  2686. }
  2687. else //LightType::DirectionalLight == settings.m_lightType
  2688. {
  2689. for (uint8_t ii = 0; ii < m_settings.m_numSplits; ++ii)
  2690. {
  2691. float mtxTmp[16];
  2692. bx::mtxMul(mtxTmp, lightProj[ii], mtxBias);
  2693. bx::mtxMul(m_shadowMapMtx[ii], lightView[0], mtxTmp); //lViewProjCropBias
  2694. }
  2695. }
  2696. // Floor.
  2697. if (LightType::DirectionalLight != m_settings.m_lightType)
  2698. {
  2699. bx::mtxMul(m_lightMtx, mtxFloor, mtxShadow); //not needed for directional light
  2700. }
  2701. m_hplaneMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2702. , mtxFloor
  2703. , *currentSmSettings->m_progDraw
  2704. , s_renderStates[RenderState::Default]
  2705. , true
  2706. );
  2707. // Bunny.
  2708. if (LightType::DirectionalLight != m_settings.m_lightType)
  2709. {
  2710. bx::mtxMul(m_lightMtx, mtxBunny, mtxShadow);
  2711. }
  2712. m_bunnyMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2713. , mtxBunny
  2714. , *currentSmSettings->m_progDraw
  2715. , s_renderStates[RenderState::Default]
  2716. , true
  2717. );
  2718. // Hollow cube.
  2719. if (LightType::DirectionalLight != m_settings.m_lightType)
  2720. {
  2721. bx::mtxMul(m_lightMtx, mtxHollowcube, mtxShadow);
  2722. }
  2723. m_hollowcubeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2724. , mtxHollowcube
  2725. , *currentSmSettings->m_progDraw
  2726. , s_renderStates[RenderState::Default]
  2727. , true
  2728. );
  2729. // Cube.
  2730. if (LightType::DirectionalLight != m_settings.m_lightType)
  2731. {
  2732. bx::mtxMul(m_lightMtx, mtxCube, mtxShadow);
  2733. }
  2734. m_cubeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2735. , mtxCube
  2736. , *currentSmSettings->m_progDraw
  2737. , s_renderStates[RenderState::Default]
  2738. , true
  2739. );
  2740. // Trees.
  2741. for (uint8_t ii = 0; ii < numTrees; ++ii)
  2742. {
  2743. if (LightType::DirectionalLight != m_settings.m_lightType)
  2744. {
  2745. bx::mtxMul(m_lightMtx, mtxTrees[ii], mtxShadow);
  2746. }
  2747. m_treeMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2748. , mtxTrees[ii]
  2749. , *currentSmSettings->m_progDraw
  2750. , s_renderStates[RenderState::Default]
  2751. , true
  2752. );
  2753. }
  2754. // Lights.
  2755. if (LightType::SpotLight == m_settings.m_lightType || LightType::PointLight == m_settings.m_lightType)
  2756. {
  2757. const float lightScale[3] = { 1.5f, 1.5f, 1.5f };
  2758. float mtx[16];
  2759. mtxBillboard(mtx, m_viewState.m_view, m_pointLight.m_position.m_v, lightScale);
  2760. m_vplaneMesh.submit(RENDERVIEW_DRAWSCENE_0_ID
  2761. , mtx
  2762. , s_programs.m_colorTexture
  2763. , s_renderStates[RenderState::Custom_BlendLightTexture]
  2764. , m_texFlare
  2765. );
  2766. }
  2767. // Draw floor bottom.
  2768. float floorBottomMtx[16];
  2769. bx::mtxSRT(floorBottomMtx
  2770. , floorScale //scaleX
  2771. , floorScale //scaleY
  2772. , floorScale //scaleZ
  2773. , 0.0f //rotX
  2774. , 0.0f //rotY
  2775. , 0.0f //rotZ
  2776. , 0.0f //translateX
  2777. , -0.1f //translateY
  2778. , 0.0f //translateZ
  2779. );
  2780. m_hplaneMesh.submit(RENDERVIEW_DRAWSCENE_1_ID
  2781. , floorBottomMtx
  2782. , s_programs.m_texture
  2783. , s_renderStates[RenderState::Custom_DrawPlaneBottom]
  2784. , m_texFigure
  2785. );
  2786. }
  2787. // Draw depth rect.
  2788. if (m_settings.m_drawDepthBuffer)
  2789. {
  2790. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[0]) );
  2791. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2792. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2793. bgfx::submit(RENDERVIEW_DRAWDEPTH_0_ID, s_programs.m_drawDepth[depthType]);
  2794. if (LightType::DirectionalLight == m_settings.m_lightType)
  2795. {
  2796. for (uint8_t ii = 1; ii < m_settings.m_numSplits; ++ii)
  2797. {
  2798. bgfx::setTexture(4, s_shadowMap[0], bgfx::getTexture(s_rtShadowMap[ii]) );
  2799. bgfx::setState(BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A);
  2800. screenSpaceQuad(currentShadowMapSizef, currentShadowMapSizef, s_flipV);
  2801. bgfx::submit(RENDERVIEW_DRAWDEPTH_0_ID+ii, s_programs.m_drawDepth[depthType]);
  2802. }
  2803. }
  2804. }
  2805. // Advance to next frame. Rendering thread will be kicked to
  2806. // process submitted rendering primitives.
  2807. bgfx::frame();
  2808. return true;
  2809. }
  2810. return false;
  2811. }
  2812. entry::MouseState m_mouseState;
  2813. uint32_t m_width;
  2814. uint32_t m_height;
  2815. uint32_t m_debug;
  2816. uint32_t m_reset;
  2817. ViewState m_viewState;
  2818. ClearValues m_clearValues;
  2819. bgfx::VertexDecl m_posDecl;
  2820. bgfx::TextureHandle m_texFigure;
  2821. bgfx::TextureHandle m_texFlare;
  2822. bgfx::TextureHandle m_texFieldstone;
  2823. Mesh m_bunnyMesh;
  2824. Mesh m_treeMesh;
  2825. Mesh m_cubeMesh;
  2826. Mesh m_hollowcubeMesh;
  2827. Mesh m_hplaneMesh;
  2828. Mesh m_vplaneMesh;
  2829. float m_color[4];
  2830. Material m_defaultMaterial;
  2831. Light m_pointLight;
  2832. Light m_directionalLight;
  2833. float m_lightMtx[16];
  2834. float m_shadowMapMtx[ShadowMapRenderTargets::Count][16];
  2835. ShadowMapSettings m_smSettings[LightType::Count][DepthImpl::Count][SmImpl::Count];
  2836. SceneSettings m_settings;
  2837. uint16_t m_currentShadowMapSize;
  2838. float m_timeAccumulatorLight;
  2839. float m_timeAccumulatorScene;
  2840. };
  2841. } // namespace
  2842. ENTRY_IMPLEMENT_MAIN(ExampleShadowmaps, "16-shadowmaps", "Shadow maps example.");