shadowmaps_simple.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. // When using GL clip space depth range [-1, 1] and packing depth into color buffer, we need to
  78. // adjust the depth range to be [0, 1] for writing to the color buffer
  79. bgfx::UniformHandle u_depthScaleOffset = bgfx::createUniform("u_depthScaleOffset", bgfx::UniformType::Vec4);
  80. const float depthScale = flipV ? 0.5f : 1.0f;
  81. const float depthOffset = flipV ? 0.5f : 0.0f;
  82. float depthScaleOffset[4] = {depthScale, depthOffset, 0.0f, 0.0f};
  83. bgfx::setUniform(u_depthScaleOffset, depthScaleOffset);
  84. // Vertex declarations.
  85. bgfx::VertexDecl PosNormalDecl;
  86. PosNormalDecl.begin()
  87. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  88. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  89. .end();
  90. // Meshes.
  91. Mesh* bunny = meshLoad("meshes/bunny.bin");
  92. Mesh* cube = meshLoad("meshes/cube.bin");
  93. Mesh* hollowcube = meshLoad("meshes/hollowcube.bin");
  94. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  95. bgfx::makeRef(s_hplaneVertices, sizeof(s_hplaneVertices) )
  96. , PosNormalDecl
  97. );
  98. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(
  99. bgfx::makeRef(s_planeIndices, sizeof(s_planeIndices) )
  100. );
  101. // Render targets.
  102. uint16_t shadowMapSize = 512;
  103. // Get renderer capabilities info.
  104. const bgfx::Caps* caps = bgfx::getCaps();
  105. // Shadow samplers are supported at least partially supported if texture
  106. // compare less equal feature is supported.
  107. bool shadowSamplerSupported = 0 != (caps->supported & BGFX_CAPS_TEXTURE_COMPARE_LEQUAL);
  108. bgfx::ProgramHandle progShadow;
  109. bgfx::ProgramHandle progMesh;
  110. bgfx::TextureHandle shadowMapTexture;
  111. bgfx::FrameBufferHandle shadowMapFB;
  112. if (shadowSamplerSupported)
  113. {
  114. // Depth textures and shadow samplers are supported.
  115. progShadow = loadProgram("vs_sms_shadow", "fs_sms_shadow");
  116. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh");
  117. shadowMapTexture = bgfx::createTexture2D(shadowMapSize, shadowMapSize, false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT | BGFX_TEXTURE_COMPARE_LEQUAL);
  118. bgfx::TextureHandle fbtextures[] = { shadowMapTexture };
  119. 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. progShadow = loadProgram("vs_sms_shadow_pd", "fs_sms_shadow_pd");
  126. progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh_pd");
  127. shadowMapTexture = bgfx::createTexture2D(shadowMapSize, shadowMapSize, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT);
  128. bgfx::TextureHandle fbtextures[] =
  129. {
  130. shadowMapTexture,
  131. bgfx::createTexture2D(shadowMapSize, shadowMapSize, false, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_WRITE_ONLY),
  132. };
  133. shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
  134. }
  135. MeshState* state[2];
  136. state[0] = meshStateCreate();
  137. state[0]->m_state = 0
  138. | BGFX_STATE_RGB_WRITE
  139. | BGFX_STATE_ALPHA_WRITE
  140. | BGFX_STATE_DEPTH_WRITE
  141. | BGFX_STATE_DEPTH_TEST_LESS
  142. | BGFX_STATE_CULL_CCW
  143. | BGFX_STATE_MSAA
  144. ;
  145. state[0]->m_program = progShadow;
  146. state[0]->m_viewId = RENDER_SHADOW_PASS_ID;
  147. state[0]->m_numTextures = 0;
  148. state[1] = meshStateCreate();
  149. state[1]->m_state = 0
  150. | BGFX_STATE_RGB_WRITE
  151. | BGFX_STATE_ALPHA_WRITE
  152. | BGFX_STATE_DEPTH_WRITE
  153. | BGFX_STATE_DEPTH_TEST_LESS
  154. | BGFX_STATE_CULL_CCW
  155. | BGFX_STATE_MSAA
  156. ;
  157. state[1]->m_program = progMesh;
  158. state[1]->m_viewId = RENDER_SCENE_PASS_ID;
  159. state[1]->m_numTextures = 1;
  160. state[1]->m_textures[0].m_flags = UINT32_MAX;
  161. state[1]->m_textures[0].m_stage = 0;
  162. state[1]->m_textures[0].m_sampler = u_shadowMap;
  163. state[1]->m_textures[0].m_texture = shadowMapTexture;
  164. // Set view and projection matrices.
  165. float view[16];
  166. float proj[16];
  167. float eye[3] = { 0.0f, 30.0f, -60.0f };
  168. float at[3] = { 0.0f, 5.0f, 0.0f };
  169. bx::mtxLookAt(view, eye, at);
  170. const float aspect = float(int32_t(width) ) / float(int32_t(height) );
  171. bx::mtxProj(proj, 60.0f, aspect, 0.1f, 1000.0f, flipV);
  172. // Time acumulators.
  173. float timeAccumulatorLight = 0.0f;
  174. float timeAccumulatorScene = 0.0f;
  175. entry::MouseState mouseState;
  176. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  177. {
  178. // Time.
  179. int64_t now = bx::getHPCounter();
  180. static int64_t last = now;
  181. const int64_t frameTime = now - last;
  182. last = now;
  183. const double freq = double(bx::getHPFrequency() );
  184. const double toMs = 1000.0/freq;
  185. const float deltaTime = float(frameTime/freq);
  186. // Update time accumulators.
  187. timeAccumulatorLight += deltaTime;
  188. timeAccumulatorScene += deltaTime;
  189. // Use debug font to print information about this example.
  190. bgfx::dbgTextClear();
  191. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/15-shadowmaps-simple");
  192. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Shadow maps example (technique: %s).", shadowSamplerSupported ? "depth texture and shadow samplers" : "shadow depth packed into color texture");
  193. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  194. // Setup lights.
  195. float lightPos[4];
  196. lightPos[0] = -cosf(timeAccumulatorLight);
  197. lightPos[1] = -1.0f;
  198. lightPos[2] = -sinf(timeAccumulatorLight);
  199. lightPos[3] = 0.0f;
  200. bgfx::setUniform(u_lightPos, lightPos);
  201. // Setup instance matrices.
  202. float mtxFloor[16];
  203. bx::mtxSRT(mtxFloor
  204. , 30.0f, 30.0f, 30.0f
  205. , 0.0f, 0.0f, 0.0f
  206. , 0.0f, 0.0f, 0.0f
  207. );
  208. float mtxBunny[16];
  209. bx::mtxSRT(mtxBunny
  210. , 5.0f, 5.0f, 5.0f
  211. , 0.0f, bx::pi - timeAccumulatorScene, 0.0f
  212. , 15.0f, 5.0f, 0.0f
  213. );
  214. float mtxHollowcube[16];
  215. bx::mtxSRT(mtxHollowcube
  216. , 2.5f, 2.5f, 2.5f
  217. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  218. , 0.0f, 10.0f, 0.0f
  219. );
  220. float mtxCube[16];
  221. bx::mtxSRT(mtxCube
  222. , 2.5f, 2.5f, 2.5f
  223. , 0.0f, 1.56f - timeAccumulatorScene, 0.0f
  224. , -15.0f, 5.0f, 0.0f
  225. );
  226. // Define matrices.
  227. float lightView[16];
  228. float lightProj[16];
  229. eye[0] = -lightPos[0];
  230. eye[1] = -lightPos[1];
  231. eye[2] = -lightPos[2];
  232. at[0] = 0.0f;
  233. at[1] = 0.0f;
  234. at[2] = 0.0f;
  235. bx::mtxLookAt(lightView, eye, at);
  236. const float area = 30.0f;
  237. bx::mtxOrtho(lightProj, -area, area, -area, area, -100.0f, 100.0f, 0.0f, flipV);
  238. bgfx::setViewRect(RENDER_SHADOW_PASS_ID, 0, 0, shadowMapSize, shadowMapSize);
  239. bgfx::setViewFrameBuffer(RENDER_SHADOW_PASS_ID, shadowMapFB);
  240. bgfx::setViewTransform(RENDER_SHADOW_PASS_ID, lightView, lightProj);
  241. bgfx::setViewRect(RENDER_SCENE_PASS_ID, 0, 0, width, height);
  242. bgfx::setViewTransform(RENDER_SCENE_PASS_ID, view, proj);
  243. // Clear backbuffer and shadowmap framebuffer at beginning.
  244. bgfx::setViewClear(RENDER_SHADOW_PASS_ID
  245. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  246. , 0x303030ff, 1.0f, 0
  247. );
  248. bgfx::setViewClear(RENDER_SCENE_PASS_ID
  249. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  250. , 0x303030ff, 1.0f, 0
  251. );
  252. // Render.
  253. float mtxShadow[16];
  254. float lightMtx[16];
  255. const float sy = flipV ? 0.5f : -0.5f;
  256. const float mtxCrop[16] =
  257. {
  258. 0.5f, 0.0f, 0.0f, 0.0f,
  259. 0.0f, sy, 0.0f, 0.0f,
  260. 0.0f, 0.0f, depthScale, 0.0f,
  261. 0.5f, 0.5f, depthOffset, 1.0f,
  262. };
  263. float mtxTmp[16];
  264. bx::mtxMul(mtxTmp, lightProj, mtxCrop);
  265. bx::mtxMul(mtxShadow, lightView, mtxTmp);
  266. // Floor.
  267. bx::mtxMul(lightMtx, mtxFloor, mtxShadow);
  268. uint32_t cached = bgfx::setTransform(mtxFloor);
  269. for (uint32_t pass = 0; pass < 2; ++pass)
  270. {
  271. const MeshState& st = *state[pass];
  272. bgfx::setTransform(cached);
  273. for (uint8_t tex = 0; tex < st.m_numTextures; ++tex)
  274. {
  275. const MeshState::Texture& texture = st.m_textures[tex];
  276. bgfx::setTexture(texture.m_stage
  277. , texture.m_sampler
  278. , texture.m_texture
  279. , texture.m_flags
  280. );
  281. }
  282. bgfx::setUniform(u_lightMtx, lightMtx);
  283. bgfx::setIndexBuffer(ibh);
  284. bgfx::setVertexBuffer(vbh);
  285. bgfx::setState(st.m_state);
  286. bgfx::submit(st.m_viewId, st.m_program);
  287. }
  288. // Bunny.
  289. bx::mtxMul(lightMtx, mtxBunny, mtxShadow);
  290. bgfx::setUniform(u_lightMtx, lightMtx);
  291. meshSubmit(bunny, &state[0], 1, mtxBunny);
  292. bgfx::setUniform(u_lightMtx, lightMtx);
  293. meshSubmit(bunny, &state[1], 1, mtxBunny);
  294. // Hollow cube.
  295. bx::mtxMul(lightMtx, mtxHollowcube, mtxShadow);
  296. bgfx::setUniform(u_lightMtx, lightMtx);
  297. meshSubmit(hollowcube, &state[0], 1, mtxHollowcube);
  298. bgfx::setUniform(u_lightMtx, lightMtx);
  299. meshSubmit(hollowcube, &state[1], 1, mtxHollowcube);
  300. // Cube.
  301. bx::mtxMul(lightMtx, mtxCube, mtxShadow);
  302. bgfx::setUniform(u_lightMtx, lightMtx);
  303. meshSubmit(cube, &state[0], 1, mtxCube);
  304. bgfx::setUniform(u_lightMtx, lightMtx);
  305. meshSubmit(cube, &state[1], 1, mtxCube);
  306. // Advance to next frame. Rendering thread will be kicked to
  307. // process submitted rendering primitives.
  308. bgfx::frame();
  309. }
  310. meshUnload(bunny);
  311. meshUnload(cube);
  312. meshUnload(hollowcube);
  313. meshStateDestroy(state[0]);
  314. meshStateDestroy(state[1]);
  315. bgfx::destroyVertexBuffer(vbh);
  316. bgfx::destroyIndexBuffer(ibh);
  317. bgfx::destroyProgram(progShadow);
  318. bgfx::destroyProgram(progMesh);
  319. bgfx::destroyFrameBuffer(shadowMapFB);
  320. bgfx::destroyUniform(u_shadowMap);
  321. bgfx::destroyUniform(u_lightPos);
  322. bgfx::destroyUniform(u_lightMtx);
  323. bgfx::destroyUniform(u_depthScaleOffset);
  324. // Shutdown bgfx.
  325. bgfx::shutdown();
  326. return 0;
  327. }