occlusion.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 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_decl
  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. const float initialPos[3] = { 15.5f, 0.0f, -15.5f };
  117. cameraSetPosition(initialPos);
  118. cameraSetHorizontalAngle(bx::toRad(-45.0f) );
  119. m_timeOffset = bx::getHPCounter();
  120. imguiCreate();
  121. }
  122. virtual int shutdown() override
  123. {
  124. imguiDestroy();
  125. // Cleanup.
  126. cameraDestroy();
  127. if (m_occlusionQuerySupported)
  128. {
  129. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  130. {
  131. bgfx::destroy(m_occlusionQueries[ii]);
  132. }
  133. }
  134. bgfx::destroy(m_ibh);
  135. bgfx::destroy(m_vbh);
  136. bgfx::destroy(m_program);
  137. // Shutdown bgfx.
  138. bgfx::shutdown();
  139. return 0;
  140. }
  141. bool update() override
  142. {
  143. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  144. {
  145. imguiBeginFrame(m_mouseState.m_mx
  146. , m_mouseState.m_my
  147. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  148. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  149. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  150. , m_mouseState.m_mz
  151. , uint16_t(m_width)
  152. , uint16_t(m_height)
  153. );
  154. showExampleDialog(this
  155. , !m_occlusionQuerySupported
  156. ? "Occlusion query is not supported."
  157. : NULL
  158. );
  159. imguiEndFrame();
  160. if (m_occlusionQuerySupported)
  161. {
  162. int64_t now = bx::getHPCounter();
  163. static int64_t last = now;
  164. const int64_t frameTime = now - last;
  165. last = now;
  166. const double freq = double(bx::getHPFrequency() );
  167. const float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );
  168. const float deltaTime = float(frameTime/freq);
  169. // Update camera.
  170. float view[16];
  171. cameraUpdate(deltaTime, m_state.m_mouse);
  172. cameraGetViewMtx(view);
  173. // Set view and projection matrix for view 0.
  174. {
  175. float proj[16];
  176. bx::mtxProj(proj, 90.0f, float(m_width)/float(m_height), 0.1f, 10000.0f, bgfx::getCaps()->homogeneousDepth);
  177. bgfx::setViewTransform(0, view, proj);
  178. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  179. bgfx::setViewTransform(1, view, proj);
  180. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  181. float at[3] = { 0.0f, 0.0f, 0.0f };
  182. float eye[3] = { 17.5f, 10.0f, -17.5f };
  183. bx::mtxLookAt(view, eye, at);
  184. bgfx::setViewTransform(2, view, proj);
  185. bgfx::setViewRect(2, 10, uint16_t(m_height - m_height/4 - 10), uint16_t(m_width/4), uint16_t(m_height/4) );
  186. }
  187. bgfx::touch(0);
  188. bgfx::touch(2);
  189. uint8_t img[CUBES_DIM*CUBES_DIM*2];
  190. for (uint32_t yy = 0; yy < CUBES_DIM; ++yy)
  191. {
  192. for (uint32_t xx = 0; xx < CUBES_DIM; ++xx)
  193. {
  194. float mtx[16];
  195. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  196. mtx[12] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(xx)*3.0f;
  197. mtx[13] = 0.0f;
  198. mtx[14] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(yy)*3.0f;
  199. bgfx::OcclusionQueryHandle occlusionQuery = m_occlusionQueries[yy*CUBES_DIM+xx];
  200. bgfx::setTransform(mtx);
  201. bgfx::setVertexBuffer(0, m_vbh);
  202. bgfx::setIndexBuffer(m_ibh);
  203. bgfx::setCondition(occlusionQuery, true);
  204. bgfx::setState(BGFX_STATE_DEFAULT);
  205. bgfx::submit(0, m_program);
  206. bgfx::setTransform(mtx);
  207. bgfx::setVertexBuffer(0, m_vbh);
  208. bgfx::setIndexBuffer(m_ibh);
  209. bgfx::setState(0
  210. | BGFX_STATE_DEPTH_TEST_LEQUAL
  211. | BGFX_STATE_CULL_CW
  212. );
  213. bgfx::submit(1, m_program, occlusionQuery);
  214. bgfx::setTransform(mtx);
  215. bgfx::setVertexBuffer(0, m_vbh);
  216. bgfx::setIndexBuffer(m_ibh);
  217. bgfx::setCondition(occlusionQuery, true);
  218. bgfx::setState(BGFX_STATE_DEFAULT);
  219. bgfx::submit(2, m_program);
  220. img[(yy*CUBES_DIM+xx)*2+0] = " \xfex"[bgfx::getResult(occlusionQuery)];
  221. img[(yy*CUBES_DIM+xx)*2+1] = 0xf;
  222. }
  223. }
  224. for (uint16_t xx = 0; xx < CUBES_DIM; ++xx)
  225. {
  226. bgfx::dbgTextImage(5 + xx*2, 20, 1, CUBES_DIM, img + xx*2, CUBES_DIM*2);
  227. }
  228. int32_t numPixels = 0;
  229. bgfx::getResult(m_occlusionQueries[0], &numPixels);
  230. bgfx::dbgTextPrintf(5, 20 + CUBES_DIM + 1, 0xf, "Passing pixels count: %d", numPixels);
  231. }
  232. // Advance to next frame. Rendering thread will be kicked to
  233. // process submitted rendering primitives.
  234. bgfx::frame();
  235. return true;
  236. }
  237. return false;
  238. }
  239. entry::MouseState m_mouseState;
  240. uint32_t m_width;
  241. uint32_t m_height;
  242. uint32_t m_debug;
  243. uint32_t m_reset;
  244. bgfx::VertexBufferHandle m_vbh;
  245. bgfx::IndexBufferHandle m_ibh;
  246. bgfx::ProgramHandle m_program;
  247. int64_t m_timeOffset;
  248. bool m_occlusionQuerySupported;
  249. bgfx::OcclusionQueryHandle m_occlusionQueries[CUBES_DIM*CUBES_DIM];
  250. entry::WindowState m_state;
  251. };
  252. } // namespace
  253. ENTRY_IMPLEMENT_MAIN(ExampleOcclusion, "26-occlusion", "Using occlusion query for conditional rendering.");