shadowmaps_simple.cpp 16 KB

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