occlusion.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include "camera.h"
  8. #include "imgui/imgui.h"
  9. namespace
  10. {
  11. #define CUBES_DIM 10
  12. struct PosColorVertex
  13. {
  14. float m_x;
  15. float m_y;
  16. float m_z;
  17. uint32_t m_abgr;
  18. static void init()
  19. {
  20. ms_decl
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  24. .end();
  25. };
  26. static bgfx::VertexDecl ms_decl;
  27. };
  28. bgfx::VertexDecl PosColorVertex::ms_decl;
  29. static PosColorVertex s_cubeVertices[8] =
  30. {
  31. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  32. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  33. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  34. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  35. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  36. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  37. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  38. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  39. };
  40. static const uint16_t s_cubeIndices[36] =
  41. {
  42. 0, 1, 2, // 0
  43. 1, 3, 2,
  44. 4, 6, 5, // 2
  45. 5, 6, 7,
  46. 0, 2, 4, // 4
  47. 4, 2, 6,
  48. 1, 5, 3, // 6
  49. 5, 7, 3,
  50. 0, 4, 1, // 8
  51. 4, 5, 1,
  52. 2, 3, 6, // 10
  53. 6, 3, 7,
  54. };
  55. class ExampleOcclusion : public entry::AppI
  56. {
  57. public:
  58. ExampleOcclusion(const char* _name, const char* _description)
  59. : entry::AppI(_name, _description)
  60. {
  61. }
  62. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  63. {
  64. Args args(_argc, _argv);
  65. m_width = _width;
  66. m_height = _height;
  67. m_debug = BGFX_DEBUG_TEXT;
  68. m_reset = BGFX_RESET_VSYNC;
  69. bgfx::init(args.m_type, args.m_pciId);
  70. bgfx::reset(m_width, m_height, m_reset);
  71. // Enable debug text.
  72. bgfx::setDebug(m_debug);
  73. // Set view 0 clear state.
  74. bgfx::setViewClear(0
  75. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  76. , 0x303030ff
  77. , 1.0f
  78. , 0
  79. );
  80. bgfx::setViewClear(2
  81. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  82. , 0x202020ff
  83. , 1.0f
  84. , 0
  85. );
  86. // Create vertex stream declaration.
  87. PosColorVertex::init();
  88. // Create static vertex buffer.
  89. m_vbh = bgfx::createVertexBuffer(
  90. // Static data can be passed with bgfx::makeRef
  91. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  92. , PosColorVertex::ms_decl
  93. );
  94. // Create static index buffer.
  95. m_ibh = bgfx::createIndexBuffer(
  96. // Static data can be passed with bgfx::makeRef
  97. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  98. );
  99. // Create program from shaders.
  100. m_program = loadProgram("vs_cubes", "fs_cubes");
  101. const bgfx::Caps* caps = bgfx::getCaps();
  102. m_occlusionQuerySupported = !!(caps->supported & BGFX_CAPS_OCCLUSION_QUERY);
  103. if (m_occlusionQuerySupported)
  104. {
  105. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  106. {
  107. m_occlusionQueries[ii] = bgfx::createOcclusionQuery();
  108. }
  109. }
  110. cameraCreate();
  111. const float initialPos[3] = { 15.5f, 0.0f, -15.5f };
  112. cameraSetPosition(initialPos);
  113. cameraSetHorizontalAngle(bx::toRad(-45.0f) );
  114. m_timeOffset = bx::getHPCounter();
  115. imguiCreate();
  116. }
  117. virtual int shutdown() override
  118. {
  119. imguiDestroy();
  120. // Cleanup.
  121. cameraDestroy();
  122. if (m_occlusionQuerySupported)
  123. {
  124. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  125. {
  126. bgfx::destroy(m_occlusionQueries[ii]);
  127. }
  128. }
  129. bgfx::destroy(m_ibh);
  130. bgfx::destroy(m_vbh);
  131. bgfx::destroy(m_program);
  132. // Shutdown bgfx.
  133. bgfx::shutdown();
  134. return 0;
  135. }
  136. bool update() override
  137. {
  138. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  139. {
  140. imguiBeginFrame(m_mouseState.m_mx
  141. , m_mouseState.m_my
  142. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  143. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  144. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  145. , m_mouseState.m_mz
  146. , uint16_t(m_width)
  147. , uint16_t(m_height)
  148. );
  149. showExampleDialog(this
  150. , !m_occlusionQuerySupported
  151. ? "Occlusion query is not supported."
  152. : NULL
  153. );
  154. imguiEndFrame();
  155. if (m_occlusionQuerySupported)
  156. {
  157. int64_t now = bx::getHPCounter();
  158. static int64_t last = now;
  159. const int64_t frameTime = now - last;
  160. last = now;
  161. const double freq = double(bx::getHPFrequency() );
  162. const float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );
  163. const float deltaTime = float(frameTime/freq);
  164. // Update camera.
  165. float view[16];
  166. cameraUpdate(deltaTime, m_state.m_mouse);
  167. cameraGetViewMtx(view);
  168. // Set view and projection matrix for view 0.
  169. const bgfx::HMD* hmd = bgfx::getHMD();
  170. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  171. {
  172. float viewHead[16];
  173. float eye[3] = {};
  174. bx::mtxQuatTranslationHMD(viewHead, hmd->eye[0].rotation, eye);
  175. float tmp[16];
  176. bx::mtxMul(tmp, view, viewHead);
  177. bgfx::setViewTransform(0, tmp, hmd->eye[0].projection);
  178. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  179. bgfx::setViewTransform(1, tmp, hmd->eye[1].projection);
  180. bgfx::setViewRect(1, 0, 0, hmd->width, hmd->height);
  181. }
  182. else
  183. {
  184. float proj[16];
  185. bx::mtxProj(proj, 90.0f, float(m_width)/float(m_height), 0.1f, 10000.0f, bgfx::getCaps()->homogeneousDepth);
  186. bgfx::setViewTransform(0, view, proj);
  187. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  188. bgfx::setViewTransform(1, view, proj);
  189. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  190. float at[3] = { 0.0f, 0.0f, 0.0f };
  191. float eye[3] = { 17.5f, 10.0f, -17.5f };
  192. bx::mtxLookAt(view, eye, at);
  193. bgfx::setViewTransform(2, view, proj);
  194. bgfx::setViewRect(2, 10, uint16_t(m_height - m_height/4 - 10), uint16_t(m_width/4), uint16_t(m_height/4) );
  195. }
  196. bgfx::touch(0);
  197. bgfx::touch(2);
  198. uint8_t img[CUBES_DIM*CUBES_DIM*2];
  199. for (uint32_t yy = 0; yy < CUBES_DIM; ++yy)
  200. {
  201. for (uint32_t xx = 0; xx < CUBES_DIM; ++xx)
  202. {
  203. float mtx[16];
  204. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  205. mtx[12] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(xx)*3.0f;
  206. mtx[13] = 0.0f;
  207. mtx[14] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(yy)*3.0f;
  208. bgfx::OcclusionQueryHandle occlusionQuery = m_occlusionQueries[yy*CUBES_DIM+xx];
  209. bgfx::setTransform(mtx);
  210. bgfx::setVertexBuffer(0, m_vbh);
  211. bgfx::setIndexBuffer(m_ibh);
  212. bgfx::setCondition(occlusionQuery, true);
  213. bgfx::setState(BGFX_STATE_DEFAULT);
  214. bgfx::submit(0, m_program);
  215. bgfx::setTransform(mtx);
  216. bgfx::setVertexBuffer(0, m_vbh);
  217. bgfx::setIndexBuffer(m_ibh);
  218. bgfx::setState(0
  219. | BGFX_STATE_DEPTH_TEST_LEQUAL
  220. | BGFX_STATE_CULL_CW
  221. );
  222. bgfx::submit(1, m_program, occlusionQuery);
  223. bgfx::setTransform(mtx);
  224. bgfx::setVertexBuffer(0, m_vbh);
  225. bgfx::setIndexBuffer(m_ibh);
  226. bgfx::setCondition(occlusionQuery, true);
  227. bgfx::setState(BGFX_STATE_DEFAULT);
  228. bgfx::submit(2, m_program);
  229. img[(yy*CUBES_DIM+xx)*2+0] = " \xfex"[bgfx::getResult(occlusionQuery)];
  230. img[(yy*CUBES_DIM+xx)*2+1] = 0xf;
  231. }
  232. }
  233. for (uint16_t xx = 0; xx < CUBES_DIM; ++xx)
  234. {
  235. bgfx::dbgTextImage(5 + xx*2, 20, 1, CUBES_DIM, img + xx*2, CUBES_DIM*2);
  236. }
  237. int32_t numPixels = 0;
  238. bgfx::getResult(m_occlusionQueries[0], &numPixels);
  239. bgfx::dbgTextPrintf(5, 20 + CUBES_DIM + 1, 0xf, "Passing pixels count: %d", numPixels);
  240. }
  241. // Advance to next frame. Rendering thread will be kicked to
  242. // process submitted rendering primitives.
  243. bgfx::frame();
  244. return true;
  245. }
  246. return false;
  247. }
  248. entry::MouseState m_mouseState;
  249. uint32_t m_width;
  250. uint32_t m_height;
  251. uint32_t m_debug;
  252. uint32_t m_reset;
  253. bgfx::VertexBufferHandle m_vbh;
  254. bgfx::IndexBufferHandle m_ibh;
  255. bgfx::ProgramHandle m_program;
  256. int64_t m_timeOffset;
  257. bool m_occlusionQuerySupported;
  258. bgfx::OcclusionQueryHandle m_occlusionQueries[CUBES_DIM*CUBES_DIM];
  259. entry::WindowState m_state;
  260. };
  261. } // namespace
  262. ENTRY_IMPLEMENT_MAIN(ExampleOcclusion, "26-occlusion", "Using occlusion query for conditional rendering.");