occlusion.cpp 7.8 KB

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