shadowmaps_simple.cpp 11 KB

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