shadowmaps_simple.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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/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. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  18. {
  19. union
  20. {
  21. uint32_t ui32;
  22. uint8_t arr[4];
  23. } un;
  24. un.arr[0] = _x;
  25. un.arr[1] = _y;
  26. un.arr[2] = _z;
  27. un.arr[3] = _w;
  28. return un.ui32;
  29. }
  30. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  31. {
  32. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  33. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  34. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  35. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  36. return packUint32(xx, yy, zz, ww);
  37. }
  38. struct PosNormalVertex
  39. {
  40. float m_x;
  41. float m_y;
  42. float m_z;
  43. uint32_t m_normal;
  44. };
  45. static PosNormalVertex s_hplaneVertices[] =
  46. {
  47. { -1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f) },
  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. };
  52. static const uint16_t s_planeIndices[] =
  53. {
  54. 0, 1, 2,
  55. 1, 3, 2,
  56. };
  57. int _main_(int /*_argc*/, char** /*_argv*/)
  58. {
  59. uint32_t width = 1280;
  60. uint32_t height = 720;
  61. uint32_t debug = BGFX_DEBUG_TEXT;
  62. uint32_t reset = BGFX_RESET_VSYNC;
  63. bgfx::init();
  64. bgfx::reset(width, height, reset);
  65. bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  66. bool flipV = false
  67. || renderer == bgfx::RendererType::OpenGL
  68. || renderer == bgfx::RendererType::OpenGLES
  69. ;
  70. // Enable debug text.
  71. bgfx::setDebug(debug);
  72. // Uniforms.
  73. bgfx::UniformHandle u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Int1);
  74. bgfx::UniformHandle u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Vec4);
  75. bgfx::UniformHandle u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4);
  76. // Vertex declarations.
  77. bgfx::VertexDecl PosNormalDecl;
  78. PosNormalDecl.begin()
  79. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  80. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  81. .end();
  82. // Meshes.
  83. Mesh* bunny = meshLoad("meshes/bunny.bin");
  84. Mesh* cube = meshLoad("meshes/cube.bin");
  85. Mesh* hollowcube = meshLoad("meshes/hollowcube.bin");
  86. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  87. bgfx::makeRef(s_hplaneVertices, sizeof(s_hplaneVertices) )
  88. , PosNormalDecl
  89. );
  90. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(
  91. bgfx::makeRef(s_planeIndices, sizeof(s_planeIndices) )
  92. );
  93. // Render targets.
  94. uint16_t shadowMapSize = 512;
  95. // Get renderer capabilities info.
  96. const bgfx::Caps* caps = bgfx::getCaps();
  97. // Shadow samplers are supported at least partially supported if texture
  98. // compare less equal feature is supported.
  99. bool shadowSamplerSupported = 0 != (caps->supported & BGFX_CAPS_TEXTURE_COMPARE_LEQUAL);
  100. bgfx::ProgramHandle progShadow;
  101. bgfx::ProgramHandle progMesh;
  102. bgfx::TextureHandle shadowMapTexture;
  103. bgfx::FrameBufferHandle shadowMapFB;
  104. if (shadowSamplerSupported)
  105. {
  106. // Depth textures and shadow samplers are supported.
  107. progShadow = loadProgram("vs_sms_shadow", "fs_sms_shadow");
  108. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh");
  109. shadowMapTexture = bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_COMPARE_LEQUAL);
  110. bgfx::TextureHandle fbtextures[] = { shadowMapTexture };
  111. 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. progShadow = loadProgram("vs_sms_shadow_pd", "fs_sms_shadow_pd");
  118. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh_pd");
  119. shadowMapTexture = bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT);
  120. bgfx::TextureHandle fbtextures[] =
  121. {
  122. shadowMapTexture,
  123. bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY),
  124. };
  125. shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  126. }
  127. MeshState* state[2];
  128. state[0] = meshStateCreate();
  129. state[0]->m_state = 0
  130. | BGFX_STATE_RGB_WRITE
  131. | BGFX_STATE_ALPHA_WRITE
  132. | BGFX_STATE_DEPTH_WRITE
  133. | BGFX_STATE_DEPTH_TEST_LESS
  134. | BGFX_STATE_CULL_CCW
  135. | BGFX_STATE_MSAA
  136. ;
  137. state[0]->m_program = progShadow;
  138. state[0]->m_viewId = RENDER_SHADOW_PASS_ID;
  139. state[0]->m_numTextures = 0;
  140. state[1] = meshStateCreate();
  141. state[1]->m_state = 0
  142. | BGFX_STATE_RGB_WRITE
  143. | BGFX_STATE_ALPHA_WRITE
  144. | BGFX_STATE_DEPTH_WRITE
  145. | BGFX_STATE_DEPTH_TEST_LESS
  146. | BGFX_STATE_CULL_CCW
  147. | BGFX_STATE_MSAA
  148. ;
  149. state[1]->m_program = progMesh;
  150. state[1]->m_viewId = RENDER_SCENE_PASS_ID;
  151. state[1]->m_numTextures = 1;
  152. state[1]->m_textures[0].m_flags = UINT32_MAX;
  153. state[1]->m_textures[0].m_stage = 0;
  154. state[1]->m_textures[0].m_sampler = u_shadowMap;
  155. state[1]->m_textures[0].m_texture = shadowMapTexture;
  156. // Set view and projection matrices.
  157. float view[16];
  158. float proj[16];
  159. float eye[3] = { 0.0f, 30.0f, -60.0f };
  160. float at[3] = { 0.0f, 5.0f, 0.0f };
  161. bx::mtxLookAt(view, eye, at);
  162. const float aspect = float(int32_t(width) ) / float(int32_t(height) );
  163. bx::mtxProj(proj, 60.0f, aspect, 0.1f, 1000.0f, flipV);
  164. // Time acumulators.
  165. float timeAccumulatorLight = 0.0f;
  166. float timeAccumulatorScene = 0.0f;
  167. entry::MouseState mouseState;
  168. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  169. {
  170. // Time.
  171. int64_t now = bx::getHPCounter();
  172. static int64_t last = now;
  173. const int64_t frameTime = now - last;
  174. last = now;
  175. const double freq = double(bx::getHPFrequency() );
  176. const double toMs = 1000.0/freq;
  177. const float deltaTime = float(frameTime/freq);
  178. // Update time accumulators.
  179. timeAccumulatorLight += deltaTime;
  180. timeAccumulatorScene += deltaTime;
  181. // Use debug font to print information about this example.
  182. bgfx::dbgTextClear();
  183. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/15-shadowmaps-simple");
  184. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Shadow maps example (technique: %s).", shadowSamplerSupported ? "depth texture and shadow samplers" : "shadow depth packed into color texture");
  185. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  186. // Setup lights.
  187. float lightPos[4];
  188. lightPos[0] = -cosf(timeAccumulatorLight);
  189. lightPos[1] = -1.0f;
  190. lightPos[2] = -sinf(timeAccumulatorLight);
  191. lightPos[3] = 0.0f;
  192. bgfx::setUniform(u_lightPos, lightPos);
  193. // Setup instance matrices.
  194. float mtxFloor[16];
  195. bx::mtxSRT(mtxFloor
  196. , 30.0f, 30.0f, 30.0f
  197. , 0.0f, 0.0f, 0.0f
  198. , 0.0f, 0.0f, 0.0f
  199. );
  200. float mtxBunny[16];
  201. bx::mtxSRT(mtxBunny
  202. , 5.0f, 5.0f, 5.0f
  203. , 0.0f, bx::pi - timeAccumulatorScene, 0.0f
  204. , 15.0f, 5.0f, 0.0f
  205. );
  206. float mtxHollowcube[16];
  207. bx::mtxSRT(mtxHollowcube
  208. , 2.5f, 2.5f, 2.5f
  209. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  210. , 0.0f, 10.0f, 0.0f
  211. );
  212. float mtxCube[16];
  213. bx::mtxSRT(mtxCube
  214. , 2.5f, 2.5f, 2.5f
  215. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  216. , -15.0f, 5.0f, 0.0f
  217. );
  218. // Define matrices.
  219. float lightView[16];
  220. float lightProj[16];
  221. eye[0] = -lightPos[0];
  222. eye[1] = -lightPos[1];
  223. eye[2] = -lightPos[2];
  224. at[0] = 0.0f;
  225. at[1] = 0.0f;
  226. at[2] = 0.0f;
  227. bx::mtxLookAt(lightView, eye, at);
  228. const float area = 30.0f;
  229. bx::mtxOrtho(lightProj, -area, area, -area, area, -100.0f, 100.0f);
  230. bgfx::setViewRect(RENDER_SHADOW_PASS_ID, 0, 0, shadowMapSize, shadowMapSize);
  231. bgfx::setViewFrameBuffer(RENDER_SHADOW_PASS_ID, shadowMapFB);
  232. bgfx::setViewTransform(RENDER_SHADOW_PASS_ID, lightView, lightProj);
  233. bgfx::setViewRect(RENDER_SCENE_PASS_ID, 0, 0, width, height);
  234. bgfx::setViewTransform(RENDER_SCENE_PASS_ID, view, proj);
  235. // Clear backbuffer and shadowmap framebuffer at beginning.
  236. bgfx::setViewClear(RENDER_SHADOW_PASS_ID
  237. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  238. , 0x303030ff, 1.0f, 0
  239. );
  240. bgfx::setViewClear(RENDER_SCENE_PASS_ID
  241. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  242. , 0x303030ff, 1.0f, 0
  243. );
  244. // Render.
  245. float mtxShadow[16];
  246. float lightMtx[16];
  247. const float sy = flipV ? 0.5f : -0.5f;
  248. const float mtxCrop[16] =
  249. {
  250. 0.5f, 0.0f, 0.0f, 0.0f,
  251. 0.0f, sy, 0.0f, 0.0f,
  252. 0.0f, 0.0f, 0.5f, 0.0f,
  253. 0.5f, 0.5f, 0.5f, 1.0f,
  254. };
  255. float mtxTmp[16];
  256. bx::mtxMul(mtxTmp, lightProj, mtxCrop);
  257. bx::mtxMul(mtxShadow, lightView, mtxTmp);
  258. // Floor.
  259. bx::mtxMul(lightMtx, mtxFloor, mtxShadow);
  260. bgfx::setUniform(u_lightMtx, lightMtx);
  261. uint32_t cached = bgfx::setTransform(mtxFloor);
  262. for (uint32_t pass = 0; pass < 2; ++pass)
  263. {
  264. const MeshState& st = *state[pass];
  265. bgfx::setTransform(cached);
  266. for (uint8_t tex = 0; tex < st.m_numTextures; ++tex)
  267. {
  268. const MeshState::Texture& texture = st.m_textures[tex];
  269. bgfx::setTexture(texture.m_stage
  270. , texture.m_sampler
  271. , texture.m_texture
  272. , texture.m_flags
  273. );
  274. }
  275. bgfx::setUniform(u_lightMtx, lightMtx);
  276. bgfx::setIndexBuffer(ibh);
  277. bgfx::setVertexBuffer(vbh);
  278. bgfx::setState(st.m_state);
  279. bgfx::submit(st.m_viewId, st.m_program);
  280. }
  281. // Bunny.
  282. bx::mtxMul(lightMtx, mtxBunny, mtxShadow);
  283. bgfx::setUniform(u_lightMtx, lightMtx);
  284. meshSubmit(bunny, &state[0], 1, mtxBunny);
  285. bgfx::setUniform(u_lightMtx, lightMtx);
  286. meshSubmit(bunny, &state[1], 1, mtxBunny);
  287. // Hollow cube.
  288. bx::mtxMul(lightMtx, mtxHollowcube, mtxShadow);
  289. bgfx::setUniform(u_lightMtx, lightMtx);
  290. meshSubmit(hollowcube, &state[0], 1, mtxHollowcube);
  291. bgfx::setUniform(u_lightMtx, lightMtx);
  292. meshSubmit(hollowcube, &state[1], 1, mtxHollowcube);
  293. // Cube.
  294. bx::mtxMul(lightMtx, mtxCube, mtxShadow);
  295. bgfx::setUniform(u_lightMtx, lightMtx);
  296. meshSubmit(cube, &state[0], 1, mtxCube);
  297. bgfx::setUniform(u_lightMtx, lightMtx);
  298. meshSubmit(cube, &state[1], 1, mtxCube);
  299. // Advance to next frame. Rendering thread will be kicked to
  300. // process submitted rendering primitives.
  301. bgfx::frame();
  302. }
  303. meshUnload(bunny);
  304. meshUnload(cube);
  305. meshUnload(hollowcube);
  306. meshStateDestroy(state[0]);
  307. meshStateDestroy(state[1]);
  308. bgfx::destroyVertexBuffer(vbh);
  309. bgfx::destroyIndexBuffer(ibh);
  310. bgfx::destroyProgram(progShadow);
  311. bgfx::destroyProgram(progMesh);
  312. bgfx::destroyFrameBuffer(shadowMapFB);
  313. bgfx::destroyUniform(u_shadowMap);
  314. bgfx::destroyUniform(u_lightPos);
  315. bgfx::destroyUniform(u_lightMtx);
  316. // Shutdown bgfx.
  317. bgfx::shutdown();
  318. return 0;
  319. }