occlusion.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 2011-2017 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(int _argc, char** _argv) BX_OVERRIDE
  63. {
  64. Args args(_argc, _argv);
  65. m_width = 1280;
  66. m_height = 720;
  67. m_debug = BGFX_DEBUG_NONE;
  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. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  102. {
  103. m_occlusionQueries[ii] = bgfx::createOcclusionQuery();
  104. }
  105. cameraCreate();
  106. const float initialPos[3] = { 15.5f, 0.0f, -15.5f };
  107. cameraSetPosition(initialPos);
  108. cameraSetHorizontalAngle(bx::toRad(-45.0f) );
  109. m_timeOffset = bx::getHPCounter();
  110. imguiCreate();
  111. }
  112. virtual int shutdown() BX_OVERRIDE
  113. {
  114. imguiDestroy();
  115. // Cleanup.
  116. cameraDestroy();
  117. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  118. {
  119. bgfx::destroyOcclusionQuery(m_occlusionQueries[ii]);
  120. }
  121. bgfx::destroyIndexBuffer(m_ibh);
  122. bgfx::destroyVertexBuffer(m_vbh);
  123. bgfx::destroyProgram(m_program);
  124. // Shutdown bgfx.
  125. bgfx::shutdown();
  126. return 0;
  127. }
  128. bool update() BX_OVERRIDE
  129. {
  130. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  131. {
  132. imguiBeginFrame(m_mouseState.m_mx
  133. , m_mouseState.m_my
  134. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  135. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  136. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  137. , m_mouseState.m_mz
  138. , uint16_t(m_width)
  139. , uint16_t(m_height)
  140. );
  141. bool restart = showExampleDialog(this);
  142. imguiEndFrame();
  143. int64_t now = bx::getHPCounter();
  144. static int64_t last = now;
  145. const int64_t frameTime = now - last;
  146. last = now;
  147. const double freq = double(bx::getHPFrequency() );
  148. const float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );
  149. const float deltaTime = float(frameTime/freq);
  150. // Update camera.
  151. float view[16];
  152. cameraUpdate(deltaTime, m_state.m_mouse);
  153. cameraGetViewMtx(view);
  154. // Set view and projection matrix for view 0.
  155. const bgfx::HMD* hmd = bgfx::getHMD();
  156. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  157. {
  158. float viewHead[16];
  159. float eye[3] = {};
  160. bx::mtxQuatTranslationHMD(viewHead, hmd->eye[0].rotation, eye);
  161. float tmp[16];
  162. bx::mtxMul(tmp, view, viewHead);
  163. bgfx::setViewTransform(0, tmp, hmd->eye[0].projection);
  164. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  165. bgfx::setViewTransform(1, tmp, hmd->eye[1].projection);
  166. bgfx::setViewRect(1, 0, 0, hmd->width, hmd->height);
  167. }
  168. else
  169. {
  170. float proj[16];
  171. bx::mtxProj(proj, 90.0f, float(m_width)/float(m_height), 0.1f, 10000.0f, bgfx::getCaps()->homogeneousDepth);
  172. bgfx::setViewTransform(0, view, proj);
  173. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  174. bgfx::setViewTransform(1, view, proj);
  175. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  176. float at[3] = { 0.0f, 0.0f, 0.0f };
  177. float eye[3] = { 17.5f, 10.0f, -17.5f };
  178. bx::mtxLookAt(view, eye, at);
  179. bgfx::setViewTransform(2, view, proj);
  180. bgfx::setViewRect(2, 10, uint16_t(m_height - m_height/4 - 10), uint16_t(m_width/4), uint16_t(m_height/4) );
  181. }
  182. bgfx::touch(0);
  183. bgfx::touch(2);
  184. uint8_t img[CUBES_DIM*CUBES_DIM*2];
  185. for (uint32_t yy = 0; yy < CUBES_DIM; ++yy)
  186. {
  187. for (uint32_t xx = 0; xx < CUBES_DIM; ++xx)
  188. {
  189. float mtx[16];
  190. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  191. mtx[12] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(xx)*3.0f;
  192. mtx[13] = 0.0f;
  193. mtx[14] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(yy)*3.0f;
  194. bgfx::OcclusionQueryHandle occlusionQuery = m_occlusionQueries[yy*CUBES_DIM+xx];
  195. bgfx::setTransform(mtx);
  196. bgfx::setVertexBuffer(0, m_vbh);
  197. bgfx::setIndexBuffer(m_ibh);
  198. bgfx::setCondition(occlusionQuery, true);
  199. bgfx::setState(BGFX_STATE_DEFAULT);
  200. bgfx::submit(0, m_program);
  201. bgfx::setTransform(mtx);
  202. bgfx::setVertexBuffer(0, m_vbh);
  203. bgfx::setIndexBuffer(m_ibh);
  204. bgfx::setState(0
  205. | BGFX_STATE_DEPTH_TEST_LEQUAL
  206. | BGFX_STATE_CULL_CW
  207. );
  208. bgfx::submit(1, m_program, occlusionQuery);
  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(2, m_program);
  215. img[(yy*CUBES_DIM+xx)*2+0] = " \xfex"[bgfx::getResult(occlusionQuery)];
  216. img[(yy*CUBES_DIM+xx)*2+1] = 0xf;
  217. }
  218. }
  219. for (uint16_t xx = 0; xx < CUBES_DIM; ++xx)
  220. {
  221. bgfx::dbgTextImage(5 + xx*2, 5, 1, CUBES_DIM, img + xx*2, CUBES_DIM*2);
  222. }
  223. int32_t numPixels = 0;
  224. bgfx::getResult(m_occlusionQueries[0], &numPixels);
  225. bgfx::dbgTextPrintf(5, 5 + CUBES_DIM + 1, 0xf, "%d", numPixels);
  226. // Advance to next frame. Rendering thread will be kicked to
  227. // process submitted rendering primitives.
  228. bgfx::frame();
  229. return !restart;
  230. }
  231. return false;
  232. }
  233. entry::MouseState m_mouseState;
  234. uint32_t m_width;
  235. uint32_t m_height;
  236. uint32_t m_debug;
  237. uint32_t m_reset;
  238. bgfx::VertexBufferHandle m_vbh;
  239. bgfx::IndexBufferHandle m_ibh;
  240. bgfx::ProgramHandle m_program;
  241. int64_t m_timeOffset;
  242. bgfx::OcclusionQueryHandle m_occlusionQueries[CUBES_DIM*CUBES_DIM];
  243. entry::WindowState m_state;
  244. };
  245. } // namespace
  246. ENTRY_IMPLEMENT_MAIN(ExampleOcclusion, "26-occlusion", "Using occlusion query for conditional rendering.");