shadowmaps_simple.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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/bgfx.h>
  10. #include <bx/timer.h>
  11. #include <bx/readerwriter.h>
  12. #include <bx/math.h>
  13. #include "entry/entry.h"
  14. #include "bgfx_utils.h"
  15. #include "imgui/imgui.h"
  16. namespace
  17. {
  18. #define RENDER_SHADOW_PASS_ID 0
  19. #define RENDER_SCENE_PASS_ID 1
  20. struct PosNormalVertex
  21. {
  22. float m_x;
  23. float m_y;
  24. float m_z;
  25. uint32_t m_normal;
  26. static void init()
  27. {
  28. ms_decl
  29. .begin()
  30. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  31. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  32. .end();
  33. };
  34. static bgfx::VertexDecl ms_decl;
  35. };
  36. bgfx::VertexDecl PosNormalVertex::ms_decl;
  37. static PosNormalVertex s_hplaneVertices[] =
  38. {
  39. { -1.0f, 0.0f, 1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f) },
  40. { 1.0f, 0.0f, 1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f) },
  41. { -1.0f, 0.0f, -1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f) },
  42. { 1.0f, 0.0f, -1.0f, encodeNormalRgba8(0.0f, 1.0f, 0.0f) },
  43. };
  44. static const uint16_t s_planeIndices[] =
  45. {
  46. 0, 1, 2,
  47. 1, 3, 2,
  48. };
  49. class ExampleShadowmapsSimple : public entry::AppI
  50. {
  51. public:
  52. ExampleShadowmapsSimple(const char* _name, const char* _description)
  53. : entry::AppI(_name, _description)
  54. {
  55. }
  56. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  57. {
  58. Args args(_argc, _argv);
  59. m_width = _width;
  60. m_height = _height;
  61. m_debug = BGFX_DEBUG_NONE;
  62. m_reset = BGFX_RESET_VSYNC;
  63. bgfx::Init init;
  64. init.type = args.m_type;
  65. init.vendorId = args.m_pciId;
  66. init.resolution.width = m_width;
  67. init.resolution.height = m_height;
  68. init.resolution.reset = m_reset;
  69. bgfx::init(init);
  70. // Enable debug text.
  71. bgfx::setDebug(m_debug);
  72. // Uniforms.
  73. s_shadowMap = bgfx::createUniform("s_shadowMap", bgfx::UniformType::Sampler);
  74. u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Vec4);
  75. u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4);
  76. // When using GL clip space depth range [-1, 1] and packing depth into color buffer, we need to
  77. // adjust the depth range to be [0, 1] for writing to the color buffer
  78. u_depthScaleOffset = bgfx::createUniform("u_depthScaleOffset", bgfx::UniformType::Vec4);
  79. // Get renderer capabilities info.
  80. const bgfx::Caps* caps = bgfx::getCaps();
  81. float depthScaleOffset[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
  82. if (caps->homogeneousDepth)
  83. {
  84. depthScaleOffset[0] = 0.5f;
  85. depthScaleOffset[1] = 0.5f;
  86. }
  87. bgfx::setUniform(u_depthScaleOffset, depthScaleOffset);
  88. // Create vertex stream declaration.
  89. PosNormalVertex::init();
  90. // Meshes.
  91. m_bunny = meshLoad("meshes/bunny.bin");
  92. m_cube = meshLoad("meshes/cube.bin");
  93. m_hollowcube = meshLoad("meshes/hollowcube.bin");
  94. m_vbh = bgfx::createVertexBuffer(
  95. bgfx::makeRef(s_hplaneVertices, sizeof(s_hplaneVertices) )
  96. , PosNormalVertex::ms_decl
  97. );
  98. m_ibh = bgfx::createIndexBuffer(
  99. bgfx::makeRef(s_planeIndices, sizeof(s_planeIndices) )
  100. );
  101. // Render targets.
  102. m_shadowMapSize = 512;
  103. // Shadow samplers are supported at least partially supported if texture
  104. // compare less equal feature is supported.
  105. m_shadowSamplerSupported = 0 != (caps->supported & BGFX_CAPS_TEXTURE_COMPARE_LEQUAL);
  106. bgfx::TextureHandle shadowMapTexture;
  107. if (m_shadowSamplerSupported)
  108. {
  109. // Depth textures and shadow samplers are supported.
  110. m_progShadow = loadProgram("vs_sms_shadow", "fs_sms_shadow");
  111. m_progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh");
  112. bgfx::TextureHandle fbtextures[] =
  113. {
  114. bgfx::createTexture2D(
  115. m_shadowMapSize
  116. , m_shadowMapSize
  117. , false
  118. , 1
  119. , bgfx::TextureFormat::D16
  120. , BGFX_TEXTURE_RT | BGFX_SAMPLER_COMPARE_LEQUAL
  121. ),
  122. };
  123. shadowMapTexture = fbtextures[0];
  124. m_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  125. }
  126. else
  127. {
  128. // Depth textures and shadow samplers are not supported. Use float
  129. // depth packing into color buffer instead.
  130. m_progShadow = loadProgram("vs_sms_shadow_pd", "fs_sms_shadow_pd");
  131. m_progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh_pd");
  132. bgfx::TextureHandle fbtextures[] =
  133. {
  134. bgfx::createTexture2D(
  135. m_shadowMapSize
  136. , m_shadowMapSize
  137. , false
  138. , 1
  139. , bgfx::TextureFormat::BGRA8
  140. , BGFX_TEXTURE_RT
  141. ),
  142. bgfx::createTexture2D(
  143. m_shadowMapSize
  144. , m_shadowMapSize
  145. , false
  146. , 1
  147. , bgfx::TextureFormat::D16
  148. , BGFX_TEXTURE_RT_WRITE_ONLY
  149. ),
  150. };
  151. shadowMapTexture = fbtextures[0];
  152. m_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  153. }
  154. m_state[0] = meshStateCreate();
  155. m_state[0]->m_state = 0
  156. | (m_shadowSamplerSupported ? 0 : BGFX_STATE_WRITE_RGB|BGFX_STATE_WRITE_A)
  157. | BGFX_STATE_WRITE_Z
  158. | BGFX_STATE_DEPTH_TEST_LESS
  159. | BGFX_STATE_CULL_CCW
  160. | BGFX_STATE_MSAA
  161. ;
  162. m_state[0]->m_program = m_progShadow;
  163. m_state[0]->m_viewId = RENDER_SHADOW_PASS_ID;
  164. m_state[0]->m_numTextures = 0;
  165. m_state[1] = meshStateCreate();
  166. m_state[1]->m_state = 0
  167. | BGFX_STATE_WRITE_RGB
  168. | BGFX_STATE_WRITE_A
  169. | BGFX_STATE_WRITE_Z
  170. | BGFX_STATE_DEPTH_TEST_LESS
  171. | BGFX_STATE_CULL_CCW
  172. | BGFX_STATE_MSAA
  173. ;
  174. m_state[1]->m_program = m_progMesh;
  175. m_state[1]->m_viewId = RENDER_SCENE_PASS_ID;
  176. m_state[1]->m_numTextures = 1;
  177. m_state[1]->m_textures[0].m_flags = UINT32_MAX;
  178. m_state[1]->m_textures[0].m_stage = 0;
  179. m_state[1]->m_textures[0].m_sampler = s_shadowMap;
  180. m_state[1]->m_textures[0].m_texture = shadowMapTexture;
  181. // Set view and projection matrices.
  182. const bx::Vec3 at = { 0.0f, 5.0f, 0.0f };
  183. const bx::Vec3 eye = { 0.0f, 30.0f, -60.0f };
  184. bx::mtxLookAt(m_view, eye, at);
  185. const float aspect = float(int32_t(m_width) ) / float(int32_t(m_height) );
  186. bx::mtxProj(m_proj, 60.0f, aspect, 0.1f, 1000.0f, bgfx::getCaps()->homogeneousDepth);
  187. m_timeOffset = bx::getHPCounter();
  188. imguiCreate();
  189. }
  190. virtual int shutdown() override
  191. {
  192. imguiDestroy();
  193. meshUnload(m_bunny);
  194. meshUnload(m_cube);
  195. meshUnload(m_hollowcube);
  196. meshStateDestroy(m_state[0]);
  197. meshStateDestroy(m_state[1]);
  198. bgfx::destroy(m_vbh);
  199. bgfx::destroy(m_ibh);
  200. bgfx::destroy(m_progShadow);
  201. bgfx::destroy(m_progMesh);
  202. bgfx::destroy(m_shadowMapFB);
  203. bgfx::destroy(s_shadowMap);
  204. bgfx::destroy(u_lightPos);
  205. bgfx::destroy(u_lightMtx);
  206. bgfx::destroy(u_depthScaleOffset);
  207. // Shutdown bgfx.
  208. bgfx::shutdown();
  209. return 0;
  210. }
  211. bool update() override
  212. {
  213. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  214. {
  215. imguiBeginFrame(m_mouseState.m_mx
  216. , m_mouseState.m_my
  217. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  218. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  219. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  220. , m_mouseState.m_mz
  221. , uint16_t(m_width)
  222. , uint16_t(m_height)
  223. );
  224. showExampleDialog(this);
  225. imguiEndFrame();
  226. int64_t now = bx::getHPCounter();
  227. const double freq = double(bx::getHPFrequency() );
  228. float time = float( (now-m_timeOffset)/freq);
  229. // Setup lights.
  230. float lightPos[4];
  231. lightPos[0] = -bx::cos(time);
  232. lightPos[1] = -1.0f;
  233. lightPos[2] = -bx::sin(time);
  234. lightPos[3] = 0.0f;
  235. bgfx::setUniform(u_lightPos, lightPos);
  236. // Setup instance matrices.
  237. float mtxFloor[16];
  238. bx::mtxSRT(mtxFloor
  239. , 30.0f, 30.0f, 30.0f
  240. , 0.0f, 0.0f, 0.0f
  241. , 0.0f, 0.0f, 0.0f
  242. );
  243. float mtxBunny[16];
  244. bx::mtxSRT(mtxBunny
  245. , 5.0f, 5.0f, 5.0f
  246. , 0.0f, bx::kPi - time, 0.0f
  247. , 15.0f, 5.0f, 0.0f
  248. );
  249. float mtxHollowcube[16];
  250. bx::mtxSRT(mtxHollowcube
  251. , 2.5f, 2.5f, 2.5f
  252. , 0.0f, 1.56f - time, 0.0f
  253. , 0.0f, 10.0f, 0.0f
  254. );
  255. float mtxCube[16];
  256. bx::mtxSRT(mtxCube
  257. , 2.5f, 2.5f, 2.5f
  258. , 0.0f, 1.56f - time, 0.0f
  259. , -15.0f, 5.0f, 0.0f
  260. );
  261. // Define matrices.
  262. float lightView[16];
  263. float lightProj[16];
  264. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  265. const bx::Vec3 eye = { -lightPos[0], -lightPos[1], -lightPos[2] };
  266. bx::mtxLookAt(lightView, eye, at);
  267. const bgfx::Caps* caps = bgfx::getCaps();
  268. const float area = 30.0f;
  269. bx::mtxOrtho(lightProj, -area, area, -area, area, -100.0f, 100.0f, 0.0f, caps->homogeneousDepth);
  270. bgfx::setViewRect(RENDER_SHADOW_PASS_ID, 0, 0, m_shadowMapSize, m_shadowMapSize);
  271. bgfx::setViewFrameBuffer(RENDER_SHADOW_PASS_ID, m_shadowMapFB);
  272. bgfx::setViewTransform(RENDER_SHADOW_PASS_ID, lightView, lightProj);
  273. bgfx::setViewRect(RENDER_SCENE_PASS_ID, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  274. bgfx::setViewTransform(RENDER_SCENE_PASS_ID, m_view, m_proj);
  275. // Clear backbuffer and shadowmap framebuffer at beginning.
  276. bgfx::setViewClear(RENDER_SHADOW_PASS_ID
  277. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  278. , 0x303030ff, 1.0f, 0
  279. );
  280. bgfx::setViewClear(RENDER_SCENE_PASS_ID
  281. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  282. , 0x303030ff, 1.0f, 0
  283. );
  284. // Render.
  285. float mtxShadow[16];
  286. float lightMtx[16];
  287. const float sy = caps->originBottomLeft ? 0.5f : -0.5f;
  288. const float sz = caps->homogeneousDepth ? 0.5f : 1.0f;
  289. const float tz = caps->homogeneousDepth ? 0.5f : 0.0f;
  290. const float mtxCrop[16] =
  291. {
  292. 0.5f, 0.0f, 0.0f, 0.0f,
  293. 0.0f, sy, 0.0f, 0.0f,
  294. 0.0f, 0.0f, sz, 0.0f,
  295. 0.5f, 0.5f, tz, 1.0f,
  296. };
  297. float mtxTmp[16];
  298. bx::mtxMul(mtxTmp, lightProj, mtxCrop);
  299. bx::mtxMul(mtxShadow, lightView, mtxTmp);
  300. // Floor.
  301. bx::mtxMul(lightMtx, mtxFloor, mtxShadow);
  302. uint32_t cached = bgfx::setTransform(mtxFloor);
  303. for (uint32_t pass = 0; pass < 2; ++pass)
  304. {
  305. const MeshState& st = *m_state[pass];
  306. bgfx::setTransform(cached);
  307. for (uint8_t tex = 0; tex < st.m_numTextures; ++tex)
  308. {
  309. const MeshState::Texture& texture = st.m_textures[tex];
  310. bgfx::setTexture(texture.m_stage
  311. , texture.m_sampler
  312. , texture.m_texture
  313. , texture.m_flags
  314. );
  315. }
  316. bgfx::setUniform(u_lightMtx, lightMtx);
  317. bgfx::setIndexBuffer(m_ibh);
  318. bgfx::setVertexBuffer(0, m_vbh);
  319. bgfx::setState(st.m_state);
  320. bgfx::submit(st.m_viewId, st.m_program);
  321. }
  322. // Bunny.
  323. bx::mtxMul(lightMtx, mtxBunny, mtxShadow);
  324. bgfx::setUniform(u_lightMtx, lightMtx);
  325. meshSubmit(m_bunny, &m_state[0], 1, mtxBunny);
  326. bgfx::setUniform(u_lightMtx, lightMtx);
  327. meshSubmit(m_bunny, &m_state[1], 1, mtxBunny);
  328. // Hollow cube.
  329. bx::mtxMul(lightMtx, mtxHollowcube, mtxShadow);
  330. bgfx::setUniform(u_lightMtx, lightMtx);
  331. meshSubmit(m_hollowcube, &m_state[0], 1, mtxHollowcube);
  332. bgfx::setUniform(u_lightMtx, lightMtx);
  333. meshSubmit(m_hollowcube, &m_state[1], 1, mtxHollowcube);
  334. // Cube.
  335. bx::mtxMul(lightMtx, mtxCube, mtxShadow);
  336. bgfx::setUniform(u_lightMtx, lightMtx);
  337. meshSubmit(m_cube, &m_state[0], 1, mtxCube);
  338. bgfx::setUniform(u_lightMtx, lightMtx);
  339. meshSubmit(m_cube, &m_state[1], 1, mtxCube);
  340. // Advance to next frame. Rendering thread will be kicked to
  341. // process submitted rendering primitives.
  342. bgfx::frame();
  343. return true;
  344. }
  345. return false;
  346. }
  347. entry::MouseState m_mouseState;
  348. uint32_t m_width;
  349. uint32_t m_height;
  350. uint32_t m_debug;
  351. uint32_t m_reset;
  352. bgfx::UniformHandle s_shadowMap;
  353. bgfx::UniformHandle u_lightPos;
  354. bgfx::UniformHandle u_lightMtx;
  355. bgfx::UniformHandle u_depthScaleOffset;
  356. Mesh* m_bunny;
  357. Mesh* m_cube;
  358. Mesh* m_hollowcube;
  359. bgfx::VertexBufferHandle m_vbh;
  360. bgfx::IndexBufferHandle m_ibh;
  361. uint16_t m_shadowMapSize;
  362. bgfx::ProgramHandle m_progShadow;
  363. bgfx::ProgramHandle m_progMesh;
  364. bgfx::FrameBufferHandle m_shadowMapFB;
  365. bool m_shadowSamplerSupported;
  366. MeshState* m_state[2];
  367. float m_view[16];
  368. float m_proj[16];
  369. int64_t m_timeOffset;
  370. };
  371. } // namespace
  372. ENTRY_IMPLEMENT_MAIN(ExampleShadowmapsSimple, "15-shadowmaps-simple", "Shadow maps example");