shadowmaps_simple.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * Copyright 2013-2014 Dario Manesku. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include "common.h"
  9. #include <bgfx.h>
  10. #include <bx/timer.h>
  11. #include <bx/readerwriter.h>
  12. #include <bx/fpumath.h>
  13. #include "entry/entry.h"
  14. #define RENDER_SHADOW_PASS_ID 0
  15. #define RENDER_SHADOW_PASS_BIT (1<<RENDER_SHADOW_PASS_ID)
  16. #define RENDER_SCENE_PASS_ID 1
  17. #define RENDER_SCENE_PASS_BIT (1<<RENDER_SCENE_PASS_ID)
  18. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  19. {
  20. union
  21. {
  22. uint32_t ui32;
  23. uint8_t arr[4];
  24. } un;
  25. un.arr[0] = _x;
  26. un.arr[1] = _y;
  27. un.arr[2] = _z;
  28. un.arr[3] = _w;
  29. return un.ui32;
  30. }
  31. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  32. {
  33. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  34. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  35. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  36. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  37. return packUint32(xx, yy, zz, ww);
  38. }
  39. struct PosNormalVertex
  40. {
  41. float m_x;
  42. float m_y;
  43. float m_z;
  44. uint32_t m_normal;
  45. };
  46. static const uint32_t s_numHPlaneVertices = 4;
  47. static PosNormalVertex s_hplaneVertices[s_numHPlaneVertices] =
  48. {
  49. { -1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  50. { 1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  51. { -1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  52. { 1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  53. };
  54. static const uint32_t s_numPlaneIndices = 6;
  55. static const uint16_t s_planeIndices[s_numPlaneIndices] =
  56. {
  57. 0, 1, 2,
  58. 1, 3, 2,
  59. };
  60. static const char* s_shaderPath = NULL;
  61. static bool s_flipV = false;
  62. static float s_texelHalf = 0.0f;
  63. bgfx::FrameBufferHandle s_shadowMapFB;
  64. static bgfx::UniformHandle u_shadowMap;
  65. static void shaderFilePath(char* _out, const char* _name)
  66. {
  67. strcpy(_out, s_shaderPath);
  68. strcat(_out, _name);
  69. strcat(_out, ".bin");
  70. }
  71. long int fsize(FILE* _file)
  72. {
  73. long int pos = ftell(_file);
  74. fseek(_file, 0L, SEEK_END);
  75. long int size = ftell(_file);
  76. fseek(_file, pos, SEEK_SET);
  77. return size;
  78. }
  79. static const bgfx::Memory* load(const char* _filePath)
  80. {
  81. FILE* file = fopen(_filePath, "rb");
  82. if (NULL != file)
  83. {
  84. uint32_t size = (uint32_t)fsize(file);
  85. const bgfx::Memory* mem = bgfx::alloc(size+1);
  86. size_t ignore = fread(mem->data, 1, size, file);
  87. BX_UNUSED(ignore);
  88. fclose(file);
  89. mem->data[mem->size-1] = '\0';
  90. return mem;
  91. }
  92. return NULL;
  93. }
  94. static const bgfx::Memory* loadShader(const char* _name)
  95. {
  96. char filePath[512];
  97. shaderFilePath(filePath, _name);
  98. return load(filePath);
  99. }
  100. static bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
  101. {
  102. const bgfx::Memory* mem;
  103. // Load vertex shader.
  104. mem = loadShader(_vsName);
  105. bgfx::ShaderHandle vsh = bgfx::createShader(mem);
  106. // Load fragment shader.
  107. mem = loadShader(_fsName);
  108. bgfx::ShaderHandle fsh = bgfx::createShader(mem);
  109. // Create program from shaders.
  110. return bgfx::createProgram(vsh, fsh, true /* destroy shaders when program is destroyed */);
  111. }
  112. struct Aabb
  113. {
  114. float m_min[3];
  115. float m_max[3];
  116. };
  117. struct Obb
  118. {
  119. float m_mtx[16];
  120. };
  121. struct Sphere
  122. {
  123. float m_center[3];
  124. float m_radius;
  125. };
  126. struct Primitive
  127. {
  128. uint32_t m_startIndex;
  129. uint32_t m_numIndices;
  130. uint32_t m_startVertex;
  131. uint32_t m_numVertices;
  132. Sphere m_sphere;
  133. Aabb m_aabb;
  134. Obb m_obb;
  135. };
  136. typedef std::vector<Primitive> PrimitiveArray;
  137. struct Group
  138. {
  139. Group()
  140. {
  141. reset();
  142. }
  143. void reset()
  144. {
  145. m_vbh.idx = bgfx::invalidHandle;
  146. m_ibh.idx = bgfx::invalidHandle;
  147. m_prims.clear();
  148. }
  149. bgfx::VertexBufferHandle m_vbh;
  150. bgfx::IndexBufferHandle m_ibh;
  151. Sphere m_sphere;
  152. Aabb m_aabb;
  153. Obb m_obb;
  154. PrimitiveArray m_prims;
  155. };
  156. ;
  157. struct Mesh
  158. {
  159. void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices)
  160. {
  161. Group group;
  162. const bgfx::Memory* mem;
  163. uint32_t size;
  164. size = _numVertices*_decl.getStride();
  165. mem = bgfx::makeRef(_vertices, size);
  166. group.m_vbh = bgfx::createVertexBuffer(mem, _decl);
  167. size = _numIndices*2;
  168. mem = bgfx::makeRef(_indices, size);
  169. group.m_ibh = bgfx::createIndexBuffer(mem);
  170. //TODO:
  171. // group.m_sphere = ...
  172. // group.m_aabb = ...
  173. // group.m_obb = ...
  174. // group.m_prims = ...
  175. m_groups.push_back(group);
  176. }
  177. void load(const char* _filePath)
  178. {
  179. #define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x0)
  180. #define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
  181. #define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
  182. bx::CrtFileReader reader;
  183. reader.open(_filePath);
  184. Group group;
  185. uint32_t chunk;
  186. while (4 == bx::read(&reader, chunk) )
  187. {
  188. switch (chunk)
  189. {
  190. case BGFX_CHUNK_MAGIC_VB:
  191. {
  192. bx::read(&reader, group.m_sphere);
  193. bx::read(&reader, group.m_aabb);
  194. bx::read(&reader, group.m_obb);
  195. bx::read(&reader, m_decl);
  196. uint16_t stride = m_decl.getStride();
  197. uint16_t numVertices;
  198. bx::read(&reader, numVertices);
  199. const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
  200. bx::read(&reader, mem->data, mem->size);
  201. group.m_vbh = bgfx::createVertexBuffer(mem, m_decl);
  202. }
  203. break;
  204. case BGFX_CHUNK_MAGIC_IB:
  205. {
  206. uint32_t numIndices;
  207. bx::read(&reader, numIndices);
  208. const bgfx::Memory* mem = bgfx::alloc(numIndices*2);
  209. bx::read(&reader, mem->data, mem->size);
  210. group.m_ibh = bgfx::createIndexBuffer(mem);
  211. }
  212. break;
  213. case BGFX_CHUNK_MAGIC_PRI:
  214. {
  215. uint16_t len;
  216. bx::read(&reader, len);
  217. std::string material;
  218. material.resize(len);
  219. bx::read(&reader, const_cast<char*>(material.c_str() ), len);
  220. uint16_t num;
  221. bx::read(&reader, num);
  222. for (uint32_t ii = 0; ii < num; ++ii)
  223. {
  224. bx::read(&reader, len);
  225. std::string name;
  226. name.resize(len);
  227. bx::read(&reader, const_cast<char*>(name.c_str() ), len);
  228. Primitive prim;
  229. bx::read(&reader, prim.m_startIndex);
  230. bx::read(&reader, prim.m_numIndices);
  231. bx::read(&reader, prim.m_startVertex);
  232. bx::read(&reader, prim.m_numVertices);
  233. bx::read(&reader, prim.m_sphere);
  234. bx::read(&reader, prim.m_aabb);
  235. bx::read(&reader, prim.m_obb);
  236. group.m_prims.push_back(prim);
  237. }
  238. m_groups.push_back(group);
  239. group.reset();
  240. }
  241. break;
  242. default:
  243. DBG("%08x at %d", chunk, reader.seek() );
  244. break;
  245. }
  246. }
  247. reader.close();
  248. }
  249. void unload()
  250. {
  251. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  252. {
  253. const Group& group = *it;
  254. bgfx::destroyVertexBuffer(group.m_vbh);
  255. if (bgfx::isValid(group.m_ibh) )
  256. {
  257. bgfx::destroyIndexBuffer(group.m_ibh);
  258. }
  259. }
  260. m_groups.clear();
  261. }
  262. void submit(uint8_t _view, float* _mtx, bgfx::ProgramHandle _program)
  263. {
  264. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  265. {
  266. const Group& group = *it;
  267. // Set model matrix for rendering.
  268. bgfx::setTransform(_mtx);
  269. bgfx::setProgram(_program);
  270. bgfx::setIndexBuffer(group.m_ibh);
  271. bgfx::setVertexBuffer(group.m_vbh);
  272. // Set shadow map.
  273. bgfx::setTexture(4, u_shadowMap, s_shadowMapFB);
  274. // Set render states.
  275. bgfx::setState(0
  276. | BGFX_STATE_RGB_WRITE
  277. | BGFX_STATE_ALPHA_WRITE
  278. | BGFX_STATE_DEPTH_WRITE
  279. | BGFX_STATE_DEPTH_TEST_LESS
  280. | BGFX_STATE_CULL_CCW
  281. | BGFX_STATE_MSAA
  282. );
  283. // Submit primitive for rendering.
  284. bgfx::submit(_view);
  285. }
  286. }
  287. void submitShadow(uint8_t _view, float* _mtx, bgfx::ProgramHandle _program)
  288. {
  289. for (GroupArray::const_iterator it = m_groups.begin(), itEnd = m_groups.end(); it != itEnd; ++it)
  290. {
  291. const Group& group = *it;
  292. // Set model matrix for rendering.
  293. bgfx::setTransform(_mtx);
  294. bgfx::setProgram(_program);
  295. bgfx::setIndexBuffer(group.m_ibh);
  296. bgfx::setVertexBuffer(group.m_vbh);
  297. // Set render states.
  298. bgfx::setState(0
  299. | BGFX_STATE_RGB_WRITE
  300. | BGFX_STATE_ALPHA_WRITE
  301. | BGFX_STATE_DEPTH_WRITE
  302. | BGFX_STATE_DEPTH_TEST_LESS
  303. | BGFX_STATE_CULL_CCW
  304. | BGFX_STATE_MSAA
  305. );
  306. // Submit primitive for rendering.
  307. bgfx::submit(_view);
  308. }
  309. }
  310. bgfx::VertexDecl m_decl;
  311. typedef std::vector<Group> GroupArray;
  312. GroupArray m_groups;
  313. };
  314. int _main_(int /*_argc*/, char** /*_argv*/)
  315. {
  316. uint32_t width = 1280;
  317. uint32_t height = 720;
  318. uint32_t debug = BGFX_DEBUG_TEXT;
  319. uint32_t reset = BGFX_RESET_VSYNC;
  320. bgfx::init();
  321. bgfx::reset(width, height, reset);
  322. // Enable debug text.
  323. bgfx::setDebug(debug);
  324. // Setup root path for binary shaders. Shader binaries are different
  325. // for each renderer.
  326. switch (bgfx::getRendererType() )
  327. {
  328. default:
  329. case bgfx::RendererType::Direct3D9:
  330. s_shaderPath = "shaders/dx9/";
  331. s_texelHalf = 0.5f;
  332. break;
  333. case bgfx::RendererType::Direct3D11:
  334. s_shaderPath = "shaders/dx11/";
  335. break;
  336. case bgfx::RendererType::OpenGL:
  337. s_shaderPath = "shaders/glsl/";
  338. s_flipV = true;
  339. break;
  340. case bgfx::RendererType::OpenGLES:
  341. s_shaderPath = "shaders/gles/";
  342. s_flipV = true;
  343. break;
  344. }
  345. // Uniforms.
  346. u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Uniform1iv);
  347. bgfx::UniformHandle u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Uniform4fv);
  348. bgfx::UniformHandle u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Uniform4x4fv);
  349. // Vertex declarations.
  350. bgfx::VertexDecl PosNormalDecl;
  351. PosNormalDecl.begin()
  352. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  353. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  354. .end();
  355. // Meshes.
  356. Mesh bunnyMesh;
  357. Mesh cubeMesh;
  358. Mesh hollowcubeMesh;
  359. Mesh hplaneMesh;
  360. bunnyMesh.load("meshes/bunny.bin");
  361. cubeMesh.load("meshes/cube.bin");
  362. hollowcubeMesh.load("meshes/hollowcube.bin");
  363. hplaneMesh.load(s_hplaneVertices, s_numHPlaneVertices, PosNormalDecl, s_planeIndices, s_numPlaneIndices);
  364. // Render targets.
  365. uint16_t shadowMapSize = 512;
  366. // Get renderer capabilities info.
  367. const bgfx::Caps* caps = bgfx::getCaps();
  368. // Shadow samplers are supported at least partially supported if texture
  369. // compare less equal feature is supported.
  370. bool shadowSamplerSupported = 0 != (caps->supported & BGFX_CAPS_TEXTURE_COMPARE_LEQUAL);
  371. bgfx::ProgramHandle progShadow;
  372. bgfx::ProgramHandle progMesh;
  373. if (shadowSamplerSupported)
  374. {
  375. // Depth textures and shadow samplers are supported.
  376. progShadow = loadProgram("vs_sms_shadow", "fs_sms_shadow");
  377. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh");
  378. bgfx::TextureHandle fbtextures[] =
  379. {
  380. bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_COMPARE_LEQUAL),
  381. };
  382. s_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  383. }
  384. else
  385. {
  386. // Depth textures and shadow samplers are not supported. Use float
  387. // depth packing into color buffer instead.
  388. progShadow = loadProgram("vs_sms_shadow_pd", "fs_sms_shadow_pd");
  389. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh_pd");
  390. bgfx::TextureHandle fbtextures[] =
  391. {
  392. bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
  393. bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY),
  394. };
  395. s_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  396. }
  397. // Set view and projection matrices.
  398. float view[16];
  399. float proj[16];
  400. const float eye[3] = { 0.0f, 30.0f, -60.0f };
  401. const float at[3] = { 0.0f, 5.0f, 0.0f };
  402. bx::mtxLookAt(view, eye, at);
  403. const float aspect = float(int32_t(width) ) / float(int32_t(height) );
  404. bx::mtxProj(proj, 60.0f, aspect, 0.1f, 1000.0f);
  405. // Time acumulators.
  406. float timeAccumulatorLight = 0.0f;
  407. float timeAccumulatorScene = 0.0f;
  408. entry::MouseState mouseState;
  409. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  410. {
  411. // Time.
  412. int64_t now = bx::getHPCounter();
  413. static int64_t last = now;
  414. const int64_t frameTime = now - last;
  415. last = now;
  416. const double freq = double(bx::getHPFrequency() );
  417. const double toMs = 1000.0/freq;
  418. const float deltaTime = float(frameTime/freq);
  419. // Update time accumulators.
  420. timeAccumulatorLight += deltaTime;
  421. timeAccumulatorScene += deltaTime;
  422. // Use debug font to print information about this example.
  423. bgfx::dbgTextClear();
  424. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/15-shadowmaps-simple");
  425. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Shadow maps example (technique: %s).", shadowSamplerSupported ? "depth texture and shadow samplers" : "shadow depth packed into color texture");
  426. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  427. // Setup lights.
  428. float lightPos[4];
  429. lightPos[0] = -cos(timeAccumulatorLight);
  430. lightPos[1] = -1.0f;
  431. lightPos[2] = -sin(timeAccumulatorLight);
  432. lightPos[3] = 0.0f;
  433. bgfx::setUniform(u_lightPos, lightPos);
  434. // Setup instance matrices.
  435. float mtxFloor[16];
  436. bx::mtxSRT(mtxFloor
  437. , 30.0f, 30.0f, 30.0f
  438. , 0.0f, 0.0f, 0.0f
  439. , 0.0f, 0.0f, 0.0f
  440. );
  441. float mtxBunny[16];
  442. bx::mtxSRT(mtxBunny
  443. , 5.0f, 5.0f, 5.0f
  444. , 0.0f, float(M_PI) - timeAccumulatorScene, 0.0f
  445. , 15.0f, 5.0f, 0.0f
  446. );
  447. float mtxHollowcube[16];
  448. bx::mtxSRT(mtxHollowcube
  449. , 2.5f, 2.5f, 2.5f
  450. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  451. , 0.0f, 10.0f, 0.0f
  452. );
  453. float mtxCube[16];
  454. bx::mtxSRT(mtxCube
  455. , 2.5f, 2.5f, 2.5f
  456. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  457. , -15.0f, 5.0f, 0.0f
  458. );
  459. // Define matrices.
  460. float lightView[16];
  461. float lightProj[16];
  462. const float eye[3] =
  463. {
  464. -lightPos[0],
  465. -lightPos[1],
  466. -lightPos[2],
  467. };
  468. const float at[3] = { 0.0f, 0.0f, 0.0f };
  469. bx::mtxLookAt(lightView, eye, at);
  470. const float area = 30.0f;
  471. bx::mtxOrtho(lightProj, -area, area, -area, area, -100.0f, 100.0f);
  472. bgfx::setViewRect(RENDER_SHADOW_PASS_ID, 0, 0, shadowMapSize, shadowMapSize);
  473. bgfx::setViewFrameBuffer(RENDER_SHADOW_PASS_ID, s_shadowMapFB);
  474. bgfx::setViewTransform(RENDER_SHADOW_PASS_ID, lightView, lightProj);
  475. bgfx::setViewRect(RENDER_SCENE_PASS_ID, 0, 0, width, height);
  476. bgfx::setViewTransform(RENDER_SCENE_PASS_ID, view, proj);
  477. // Clear backbuffer and shadowmap framebuffer at beginning.
  478. bgfx::setViewClearMask(RENDER_SHADOW_PASS_BIT|RENDER_SCENE_PASS_BIT
  479. , BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
  480. , 0x303030ff, 1.0f, 0
  481. );
  482. // Render.
  483. float mtxShadow[16];
  484. float lightMtx[16];
  485. const float sy = s_flipV ? 0.5f : -0.5f;
  486. const float mtxCrop[16] =
  487. {
  488. 0.5f, 0.0f, 0.0f, 0.0f,
  489. 0.0f, sy, 0.0f, 0.0f,
  490. 0.0f, 0.0f, 0.5f, 0.0f,
  491. 0.5f, 0.5f, 0.5f, 1.0f,
  492. };
  493. float mtxTmp[16];
  494. bx::mtxMul(mtxTmp, lightProj, mtxCrop);
  495. bx::mtxMul(mtxShadow, lightView, mtxTmp);
  496. // Floor.
  497. bx::mtxMul(lightMtx, mtxFloor, mtxShadow);
  498. bgfx::setUniform(u_lightMtx, lightMtx);
  499. hplaneMesh.submit(RENDER_SCENE_PASS_ID, mtxFloor, progMesh);
  500. hplaneMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxFloor, progShadow);
  501. // Bunny.
  502. bx::mtxMul(lightMtx, mtxBunny, mtxShadow);
  503. bgfx::setUniform(u_lightMtx, lightMtx);
  504. bunnyMesh.submit(RENDER_SCENE_PASS_ID, mtxBunny, progMesh);
  505. bunnyMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxBunny, progShadow);
  506. // Hollow cube.
  507. bx::mtxMul(lightMtx, mtxHollowcube, mtxShadow);
  508. bgfx::setUniform(u_lightMtx, lightMtx);
  509. hollowcubeMesh.submit(RENDER_SCENE_PASS_ID, mtxHollowcube, progMesh);
  510. hollowcubeMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxHollowcube, progShadow);
  511. // Cube.
  512. bx::mtxMul(lightMtx, mtxCube, mtxShadow);
  513. bgfx::setUniform(u_lightMtx, lightMtx);
  514. cubeMesh.submit(RENDER_SCENE_PASS_ID, mtxCube, progMesh);
  515. cubeMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxCube, progShadow);
  516. // Advance to next frame. Rendering thread will be kicked to
  517. // process submitted rendering primitives.
  518. bgfx::frame();
  519. }
  520. bunnyMesh.unload();
  521. cubeMesh.unload();
  522. hollowcubeMesh.unload();
  523. hplaneMesh.unload();
  524. bgfx::destroyProgram(progShadow);
  525. bgfx::destroyProgram(progMesh);
  526. bgfx::destroyFrameBuffer(s_shadowMapFB);
  527. bgfx::destroyUniform(u_shadowMap);
  528. bgfx::destroyUniform(u_lightPos);
  529. bgfx::destroyUniform(u_lightMtx);
  530. // Shutdown bgfx.
  531. bgfx::shutdown();
  532. return 0;
  533. }