shadowmaps_simple.cpp 11 KB

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