occlusion.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 2011-2019 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_layout
  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::VertexLayout ms_layout;
  27. };
  28. bgfx::VertexLayout PosColorVertex::ms_layout;
  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 init;
  70. init.type = args.m_type;
  71. init.vendorId = args.m_pciId;
  72. init.resolution.width = m_width;
  73. init.resolution.height = m_height;
  74. init.resolution.reset = m_reset;
  75. bgfx::init(init);
  76. // Enable debug text.
  77. bgfx::setDebug(m_debug);
  78. // Set view 0 clear state.
  79. bgfx::setViewClear(0
  80. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  81. , 0x303030ff
  82. , 1.0f
  83. , 0
  84. );
  85. bgfx::setViewClear(2
  86. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  87. , 0x202020ff
  88. , 1.0f
  89. , 0
  90. );
  91. // Create vertex stream declaration.
  92. PosColorVertex::init();
  93. // Create static vertex buffer.
  94. m_vbh = bgfx::createVertexBuffer(
  95. // Static data can be passed with bgfx::makeRef
  96. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  97. , PosColorVertex::ms_layout
  98. );
  99. // Create static index buffer.
  100. m_ibh = bgfx::createIndexBuffer(
  101. // Static data can be passed with bgfx::makeRef
  102. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  103. );
  104. // Create program from shaders.
  105. m_program = loadProgram("vs_cubes", "fs_cubes");
  106. const bgfx::Caps* caps = bgfx::getCaps();
  107. m_occlusionQuerySupported = !!(caps->supported & BGFX_CAPS_OCCLUSION_QUERY);
  108. if (m_occlusionQuerySupported)
  109. {
  110. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  111. {
  112. m_occlusionQueries[ii] = bgfx::createOcclusionQuery();
  113. }
  114. }
  115. cameraCreate();
  116. cameraSetPosition({ 15.5f, 0.0f, -15.5f });
  117. cameraSetHorizontalAngle(bx::toRad(-45.0f) );
  118. m_timeOffset = bx::getHPCounter();
  119. imguiCreate();
  120. }
  121. virtual int shutdown() override
  122. {
  123. imguiDestroy();
  124. // Cleanup.
  125. cameraDestroy();
  126. if (m_occlusionQuerySupported)
  127. {
  128. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  129. {
  130. bgfx::destroy(m_occlusionQueries[ii]);
  131. }
  132. }
  133. bgfx::destroy(m_ibh);
  134. bgfx::destroy(m_vbh);
  135. bgfx::destroy(m_program);
  136. // Shutdown bgfx.
  137. bgfx::shutdown();
  138. return 0;
  139. }
  140. bool update() override
  141. {
  142. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  143. {
  144. imguiBeginFrame(m_mouseState.m_mx
  145. , m_mouseState.m_my
  146. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  147. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  148. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  149. , m_mouseState.m_mz
  150. , uint16_t(m_width)
  151. , uint16_t(m_height)
  152. );
  153. showExampleDialog(this
  154. , !m_occlusionQuerySupported
  155. ? "Occlusion query is not supported."
  156. : NULL
  157. );
  158. imguiEndFrame();
  159. if (m_occlusionQuerySupported)
  160. {
  161. int64_t now = bx::getHPCounter();
  162. static int64_t last = now;
  163. const int64_t frameTime = now - last;
  164. last = now;
  165. const double freq = double(bx::getHPFrequency() );
  166. const float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );
  167. const float deltaTime = float(frameTime/freq);
  168. // Update camera.
  169. float view[16];
  170. cameraUpdate(deltaTime, m_state.m_mouse);
  171. cameraGetViewMtx(view);
  172. // Set view and projection matrix for view 0.
  173. {
  174. float proj[16];
  175. bx::mtxProj(proj, 90.0f, float(m_width)/float(m_height), 0.1f, 10000.0f, bgfx::getCaps()->homogeneousDepth);
  176. bgfx::setViewTransform(0, view, proj);
  177. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  178. bgfx::setViewTransform(1, view, proj);
  179. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  180. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  181. const bx::Vec3 eye = { 17.5f, 10.0f, -17.5f };
  182. bx::mtxLookAt(view, eye, at);
  183. bgfx::setViewTransform(2, view, proj);
  184. bgfx::setViewRect(2, 10, uint16_t(m_height - m_height/4 - 10), uint16_t(m_width/4), uint16_t(m_height/4) );
  185. }
  186. bgfx::touch(0);
  187. bgfx::touch(2);
  188. uint8_t img[CUBES_DIM*CUBES_DIM*2];
  189. for (uint32_t yy = 0; yy < CUBES_DIM; ++yy)
  190. {
  191. for (uint32_t xx = 0; xx < CUBES_DIM; ++xx)
  192. {
  193. float mtx[16];
  194. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  195. mtx[12] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(xx)*3.0f;
  196. mtx[13] = 0.0f;
  197. mtx[14] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(yy)*3.0f;
  198. bgfx::OcclusionQueryHandle occlusionQuery = m_occlusionQueries[yy*CUBES_DIM+xx];
  199. bgfx::setTransform(mtx);
  200. bgfx::setVertexBuffer(0, m_vbh);
  201. bgfx::setIndexBuffer(m_ibh);
  202. bgfx::setCondition(occlusionQuery, true);
  203. bgfx::setState(BGFX_STATE_DEFAULT);
  204. bgfx::submit(0, m_program);
  205. bgfx::setTransform(mtx);
  206. bgfx::setVertexBuffer(0, m_vbh);
  207. bgfx::setIndexBuffer(m_ibh);
  208. bgfx::setState(0
  209. | BGFX_STATE_DEPTH_TEST_LEQUAL
  210. | BGFX_STATE_CULL_CW
  211. );
  212. bgfx::submit(1, m_program, occlusionQuery);
  213. bgfx::setTransform(mtx);
  214. bgfx::setVertexBuffer(0, m_vbh);
  215. bgfx::setIndexBuffer(m_ibh);
  216. bgfx::setCondition(occlusionQuery, true);
  217. bgfx::setState(BGFX_STATE_DEFAULT);
  218. bgfx::submit(2, m_program);
  219. img[(yy*CUBES_DIM+xx)*2+0] = " \xfex"[bgfx::getResult(occlusionQuery)];
  220. img[(yy*CUBES_DIM+xx)*2+1] = 0xf;
  221. }
  222. }
  223. for (uint16_t xx = 0; xx < CUBES_DIM; ++xx)
  224. {
  225. bgfx::dbgTextImage(5 + xx*2, 20, 1, CUBES_DIM, img + xx*2, CUBES_DIM*2);
  226. }
  227. int32_t numPixels = 0;
  228. bgfx::getResult(m_occlusionQueries[0], &numPixels);
  229. bgfx::dbgTextPrintf(5, 20 + CUBES_DIM + 1, 0xf, "Passing pixels count: %d", numPixels);
  230. }
  231. // Advance to next frame. Rendering thread will be kicked to
  232. // process submitted rendering primitives.
  233. bgfx::frame();
  234. return true;
  235. }
  236. return false;
  237. }
  238. entry::MouseState m_mouseState;
  239. uint32_t m_width;
  240. uint32_t m_height;
  241. uint32_t m_debug;
  242. uint32_t m_reset;
  243. bgfx::VertexBufferHandle m_vbh;
  244. bgfx::IndexBufferHandle m_ibh;
  245. bgfx::ProgramHandle m_program;
  246. int64_t m_timeOffset;
  247. bool m_occlusionQuerySupported;
  248. bgfx::OcclusionQueryHandle m_occlusionQueries[CUBES_DIM*CUBES_DIM];
  249. entry::WindowState m_state;
  250. };
  251. } // namespace
  252. ENTRY_IMPLEMENT_MAIN(ExampleOcclusion, "26-occlusion", "Using occlusion query for conditional rendering.");