shadowmaps_simple.cpp 12 KB

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