shadowmaps_simple.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. 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. Args args(_argc, _argv);
  60. uint32_t width = 1280;
  61. uint32_t height = 720;
  62. uint32_t debug = BGFX_DEBUG_TEXT;
  63. uint32_t reset = BGFX_RESET_VSYNC;
  64. bgfx::init(args.m_type, args.m_pciId);
  65. bgfx::reset(width, height, reset);
  66. bgfx::RendererType::Enum renderer = bgfx::getRendererType();
  67. bool flipV = false
  68. || renderer == bgfx::RendererType::OpenGL
  69. || renderer == bgfx::RendererType::OpenGLES
  70. ;
  71. // Enable debug text.
  72. bgfx::setDebug(debug);
  73. // Uniforms.
  74. bgfx::UniformHandle u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Int1);
  75. bgfx::UniformHandle u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Vec4);
  76. bgfx::UniformHandle u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4);
  77. // Vertex declarations.
  78. bgfx::VertexDecl PosNormalDecl;
  79. PosNormalDecl.begin()
  80. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  81. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  82. .end();
  83. // Meshes.
  84. Mesh* bunny = meshLoad("meshes/bunny.bin");
  85. Mesh* cube = meshLoad("meshes/cube.bin");
  86. Mesh* hollowcube = meshLoad("meshes/hollowcube.bin");
  87. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  88. bgfx::makeRef(s_hplaneVertices, sizeof(s_hplaneVertices) )
  89. , PosNormalDecl
  90. );
  91. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(
  92. bgfx::makeRef(s_planeIndices, sizeof(s_planeIndices) )
  93. );
  94. // Render targets.
  95. uint16_t shadowMapSize = 512;
  96. // Get renderer capabilities info.
  97. const bgfx::Caps* caps = bgfx::getCaps();
  98. // Shadow samplers are supported at least partially supported if texture
  99. // compare less equal feature is supported.
  100. bool shadowSamplerSupported = 0 != (caps->supported & BGFX_CAPS_TEXTURE_COMPARE_LEQUAL);
  101. bgfx::ProgramHandle progShadow;
  102. bgfx::ProgramHandle progMesh;
  103. bgfx::TextureHandle shadowMapTexture;
  104. bgfx::FrameBufferHandle shadowMapFB;
  105. if (shadowSamplerSupported)
  106. {
  107. // Depth textures and shadow samplers are supported.
  108. progShadow = loadProgram("vs_sms_shadow", "fs_sms_shadow");
  109. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh");
  110. shadowMapTexture = bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT | BGFX_TEXTURE_COMPARE_LEQUAL);
  111. bgfx::TextureHandle fbtextures[] = { shadowMapTexture };
  112. shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  113. }
  114. else
  115. {
  116. // Depth textures and shadow samplers are not supported. Use float
  117. // depth packing into color buffer instead.
  118. progShadow = loadProgram("vs_sms_shadow_pd", "fs_sms_shadow_pd");
  119. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh_pd");
  120. shadowMapTexture = bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT);
  121. bgfx::TextureHandle fbtextures[] =
  122. {
  123. shadowMapTexture,
  124. bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY),
  125. };
  126. shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  127. }
  128. MeshState* state[2];
  129. state[0] = meshStateCreate();
  130. state[0]->m_state = 0
  131. | BGFX_STATE_RGB_WRITE
  132. | BGFX_STATE_ALPHA_WRITE
  133. | BGFX_STATE_DEPTH_WRITE
  134. | BGFX_STATE_DEPTH_TEST_LESS
  135. | BGFX_STATE_CULL_CCW
  136. | BGFX_STATE_MSAA
  137. ;
  138. state[0]->m_program = progShadow;
  139. state[0]->m_viewId = RENDER_SHADOW_PASS_ID;
  140. state[0]->m_numTextures = 0;
  141. state[1] = meshStateCreate();
  142. state[1]->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. state[1]->m_program = progMesh;
  151. state[1]->m_viewId = RENDER_SCENE_PASS_ID;
  152. state[1]->m_numTextures = 1;
  153. state[1]->m_textures[0].m_flags = UINT32_MAX;
  154. state[1]->m_textures[0].m_stage = 0;
  155. state[1]->m_textures[0].m_sampler = u_shadowMap;
  156. state[1]->m_textures[0].m_texture = shadowMapTexture;
  157. // Set view and projection matrices.
  158. float view[16];
  159. float proj[16];
  160. float eye[3] = { 0.0f, 30.0f, -60.0f };
  161. float at[3] = { 0.0f, 5.0f, 0.0f };
  162. bx::mtxLookAt(view, eye, at);
  163. const float aspect = float(int32_t(width) ) / float(int32_t(height) );
  164. bx::mtxProj(proj, 60.0f, aspect, 0.1f, 1000.0f, flipV);
  165. // Time acumulators.
  166. float timeAccumulatorLight = 0.0f;
  167. float timeAccumulatorScene = 0.0f;
  168. entry::MouseState mouseState;
  169. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  170. {
  171. // Time.
  172. int64_t now = bx::getHPCounter();
  173. static int64_t last = now;
  174. const int64_t frameTime = now - last;
  175. last = now;
  176. const double freq = double(bx::getHPFrequency() );
  177. const double toMs = 1000.0/freq;
  178. const float deltaTime = float(frameTime/freq);
  179. // Update time accumulators.
  180. timeAccumulatorLight += deltaTime;
  181. timeAccumulatorScene += deltaTime;
  182. // Use debug font to print information about this example.
  183. bgfx::dbgTextClear();
  184. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/15-shadowmaps-simple");
  185. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Shadow maps example (technique: %s).", shadowSamplerSupported ? "depth texture and shadow samplers" : "shadow depth packed into color texture");
  186. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  187. // Setup lights.
  188. float lightPos[4];
  189. lightPos[0] = -cosf(timeAccumulatorLight);
  190. lightPos[1] = -1.0f;
  191. lightPos[2] = -sinf(timeAccumulatorLight);
  192. lightPos[3] = 0.0f;
  193. bgfx::setUniform(u_lightPos, lightPos);
  194. // Setup instance matrices.
  195. float mtxFloor[16];
  196. bx::mtxSRT(mtxFloor
  197. , 30.0f, 30.0f, 30.0f
  198. , 0.0f, 0.0f, 0.0f
  199. , 0.0f, 0.0f, 0.0f
  200. );
  201. float mtxBunny[16];
  202. bx::mtxSRT(mtxBunny
  203. , 5.0f, 5.0f, 5.0f
  204. , 0.0f, bx::pi - timeAccumulatorScene, 0.0f
  205. , 15.0f, 5.0f, 0.0f
  206. );
  207. float mtxHollowcube[16];
  208. bx::mtxSRT(mtxHollowcube
  209. , 2.5f, 2.5f, 2.5f
  210. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  211. , 0.0f, 10.0f, 0.0f
  212. );
  213. float mtxCube[16];
  214. bx::mtxSRT(mtxCube
  215. , 2.5f, 2.5f, 2.5f
  216. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  217. , -15.0f, 5.0f, 0.0f
  218. );
  219. // Define matrices.
  220. float lightView[16];
  221. float lightProj[16];
  222. eye[0] = -lightPos[0];
  223. eye[1] = -lightPos[1];
  224. eye[2] = -lightPos[2];
  225. at[0] = 0.0f;
  226. at[1] = 0.0f;
  227. at[2] = 0.0f;
  228. bx::mtxLookAt(lightView, eye, at);
  229. const float area = 30.0f;
  230. bx::mtxOrtho(lightProj, -area, area, -area, area, -100.0f, 100.0f);
  231. bgfx::setViewRect(RENDER_SHADOW_PASS_ID, 0, 0, shadowMapSize, shadowMapSize);
  232. bgfx::setViewFrameBuffer(RENDER_SHADOW_PASS_ID, shadowMapFB);
  233. bgfx::setViewTransform(RENDER_SHADOW_PASS_ID, lightView, lightProj);
  234. bgfx::setViewRect(RENDER_SCENE_PASS_ID, 0, 0, width, height);
  235. bgfx::setViewTransform(RENDER_SCENE_PASS_ID, view, proj);
  236. // Clear backbuffer and shadowmap framebuffer at beginning.
  237. bgfx::setViewClear(RENDER_SHADOW_PASS_ID
  238. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  239. , 0x303030ff, 1.0f, 0
  240. );
  241. bgfx::setViewClear(RENDER_SCENE_PASS_ID
  242. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  243. , 0x303030ff, 1.0f, 0
  244. );
  245. // Render.
  246. float mtxShadow[16];
  247. float lightMtx[16];
  248. const float sy = flipV ? 0.5f : -0.5f;
  249. const float mtxCrop[16] =
  250. {
  251. 0.5f, 0.0f, 0.0f, 0.0f,
  252. 0.0f, sy, 0.0f, 0.0f,
  253. 0.0f, 0.0f, 0.5f, 0.0f,
  254. 0.5f, 0.5f, 0.5f, 1.0f,
  255. };
  256. float mtxTmp[16];
  257. bx::mtxMul(mtxTmp, lightProj, mtxCrop);
  258. bx::mtxMul(mtxShadow, lightView, mtxTmp);
  259. // Floor.
  260. bx::mtxMul(lightMtx, mtxFloor, mtxShadow);
  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. }