occlusion.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright 2011-2016 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. #define CUBES_DIM 10
  9. struct PosColorVertex
  10. {
  11. float m_x;
  12. float m_y;
  13. float m_z;
  14. uint32_t m_abgr;
  15. static void init()
  16. {
  17. ms_decl
  18. .begin()
  19. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  20. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  21. .end();
  22. };
  23. static bgfx::VertexDecl ms_decl;
  24. };
  25. bgfx::VertexDecl PosColorVertex::ms_decl;
  26. static PosColorVertex s_cubeVertices[8] =
  27. {
  28. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  29. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  30. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  31. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  32. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  33. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  34. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  35. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  36. };
  37. static const uint16_t s_cubeIndices[36] =
  38. {
  39. 0, 1, 2, // 0
  40. 1, 3, 2,
  41. 4, 6, 5, // 2
  42. 5, 6, 7,
  43. 0, 2, 4, // 4
  44. 4, 2, 6,
  45. 1, 5, 3, // 6
  46. 5, 7, 3,
  47. 0, 4, 1, // 8
  48. 4, 5, 1,
  49. 2, 3, 6, // 10
  50. 6, 3, 7,
  51. };
  52. class ExampleOcclusion : public entry::AppI
  53. {
  54. void init(int _argc, char** _argv) BX_OVERRIDE
  55. {
  56. Args args(_argc, _argv);
  57. uint32_t width = 1280;
  58. uint32_t height = 720;
  59. m_debug = BGFX_DEBUG_TEXT;
  60. m_reset = BGFX_RESET_VSYNC;
  61. bgfx::init(args.m_type, args.m_pciId);
  62. bgfx::reset(width, height, m_reset);
  63. // Enable debug text.
  64. bgfx::setDebug(m_debug);
  65. // Set view 0 clear state.
  66. bgfx::setViewClear(0
  67. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  68. , 0x303030ff
  69. , 1.0f
  70. , 0
  71. );
  72. bgfx::setViewClear(2
  73. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  74. , 0x202020ff
  75. , 1.0f
  76. , 0
  77. );
  78. // Create vertex stream declaration.
  79. PosColorVertex::init();
  80. // Create static vertex buffer.
  81. m_vbh = bgfx::createVertexBuffer(
  82. // Static data can be passed with bgfx::makeRef
  83. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  84. , PosColorVertex::ms_decl
  85. );
  86. // Create static index buffer.
  87. m_ibh = bgfx::createIndexBuffer(
  88. // Static data can be passed with bgfx::makeRef
  89. bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) )
  90. );
  91. // Create program from shaders.
  92. m_program = loadProgram("vs_cubes", "fs_cubes");
  93. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  94. {
  95. m_occlusionQueries[ii] = bgfx::createOcclusionQuery();
  96. }
  97. cameraCreate();
  98. const float initialPos[3] = { 15.5f, 0.0f, -15.5f };
  99. cameraSetPosition(initialPos);
  100. cameraSetHorizontalAngle(bx::toRad(-45.0f) );
  101. m_timeOffset = bx::getHPCounter();
  102. }
  103. virtual int shutdown() BX_OVERRIDE
  104. {
  105. // Cleanup.
  106. cameraDestroy();
  107. for (uint32_t ii = 0; ii < BX_COUNTOF(m_occlusionQueries); ++ii)
  108. {
  109. bgfx::destroyOcclusionQuery(m_occlusionQueries[ii]);
  110. }
  111. bgfx::destroyIndexBuffer(m_ibh);
  112. bgfx::destroyVertexBuffer(m_vbh);
  113. bgfx::destroyProgram(m_program);
  114. // Shutdown bgfx.
  115. bgfx::shutdown();
  116. return 0;
  117. }
  118. bool update() BX_OVERRIDE
  119. {
  120. if (!entry::processWindowEvents(m_state, m_debug, m_reset) )
  121. {
  122. int64_t now = bx::getHPCounter();
  123. static int64_t last = now;
  124. const int64_t frameTime = now - last;
  125. last = now;
  126. const double freq = double(bx::getHPFrequency() );
  127. const double toMs = 1000.0/freq;
  128. const float time = (float)( (now-m_timeOffset)/double(bx::getHPFrequency() ) );
  129. const float deltaTime = float(frameTime/freq);
  130. // Use debug font to print information about this example.
  131. bgfx::dbgTextClear();
  132. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/26-occlusion");
  133. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Using occlusion query for conditional rendering.");
  134. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  135. uint32_t width = m_state.m_width;
  136. uint32_t height = m_state.m_height;
  137. // Update camera.
  138. float view[16];
  139. cameraUpdate(deltaTime, m_state.m_mouse);
  140. cameraGetViewMtx(view);
  141. // Set view and projection matrix for view 0.
  142. const bgfx::HMD* hmd = bgfx::getHMD();
  143. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  144. {
  145. float viewHead[16];
  146. float eye[3] = {};
  147. bx::mtxQuatTranslationHMD(viewHead, hmd->eye[0].rotation, eye);
  148. float tmp[16];
  149. bx::mtxMul(tmp, view, viewHead);
  150. bgfx::setViewTransform(0, tmp, hmd->eye[0].projection);
  151. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  152. bgfx::setViewTransform(1, tmp, hmd->eye[1].projection);
  153. bgfx::setViewRect(1, 0, 0, hmd->width, hmd->height);
  154. }
  155. else
  156. {
  157. float proj[16];
  158. bx::mtxProj(proj, 90.0f, float(width)/float(height), 0.1f, 10000.0f);
  159. bgfx::setViewTransform(0, view, proj);
  160. bgfx::setViewRect(0, 0, 0, width, height);
  161. bgfx::setViewTransform(1, view, proj);
  162. bgfx::setViewRect(1, 0, 0, width, height);
  163. float at[3] = { 0.0f, 0.0f, 0.0f };
  164. float eye[3] = { 17.5f, 10.0f, -17.5f };
  165. bx::mtxLookAt(view, eye, at);
  166. bgfx::setViewTransform(2, view, proj);
  167. bgfx::setViewRect(2, 10, height - height/4 - 10, width/4, height/4);
  168. }
  169. bgfx::touch(0);
  170. bgfx::touch(2);
  171. uint8_t img[CUBES_DIM*CUBES_DIM*2];
  172. for (uint32_t yy = 0; yy < CUBES_DIM; ++yy)
  173. {
  174. for (uint32_t xx = 0; xx < CUBES_DIM; ++xx)
  175. {
  176. float mtx[16];
  177. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  178. mtx[12] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(xx)*3.0f;
  179. mtx[13] = 0.0f;
  180. mtx[14] = -(CUBES_DIM-1) * 3.0f / 2.0f + float(yy)*3.0f;
  181. bgfx::OcclusionQueryHandle occlusionQuery = m_occlusionQueries[yy*CUBES_DIM+xx];
  182. bgfx::setTransform(mtx);
  183. bgfx::setVertexBuffer(m_vbh);
  184. bgfx::setIndexBuffer(m_ibh);
  185. bgfx::setCondition(occlusionQuery, true);
  186. bgfx::setState(BGFX_STATE_DEFAULT);
  187. bgfx::submit(0, m_program);
  188. bgfx::setTransform(mtx);
  189. bgfx::setVertexBuffer(m_vbh);
  190. bgfx::setIndexBuffer(m_ibh);
  191. bgfx::setState(0
  192. | BGFX_STATE_DEPTH_TEST_LEQUAL
  193. | BGFX_STATE_CULL_CW
  194. );
  195. bgfx::submit(1, m_program, occlusionQuery);
  196. bgfx::setTransform(mtx);
  197. bgfx::setVertexBuffer(m_vbh);
  198. bgfx::setIndexBuffer(m_ibh);
  199. bgfx::setCondition(occlusionQuery, true);
  200. bgfx::setState(BGFX_STATE_DEFAULT);
  201. bgfx::submit(2, m_program);
  202. img[(yy*CUBES_DIM+xx)*2+0] = " \xfex"[bgfx::getResult(occlusionQuery)];
  203. img[(yy*CUBES_DIM+xx)*2+1] = 0xf;
  204. }
  205. }
  206. for (uint32_t xx = 0; xx < CUBES_DIM; ++xx)
  207. {
  208. bgfx::dbgTextImage(5 + xx*2, 5, 1, CUBES_DIM, img + xx*2, CUBES_DIM*2);
  209. }
  210. // Advance to next frame. Rendering thread will be kicked to
  211. // process submitted rendering primitives.
  212. bgfx::frame();
  213. return true;
  214. }
  215. return false;
  216. }
  217. uint32_t m_reset;
  218. uint32_t m_debug;
  219. bgfx::VertexBufferHandle m_vbh;
  220. bgfx::IndexBufferHandle m_ibh;
  221. bgfx::ProgramHandle m_program;
  222. int64_t m_timeOffset;
  223. bgfx::OcclusionQueryHandle m_occlusionQueries[CUBES_DIM*CUBES_DIM];
  224. entry::WindowState m_state;
  225. };
  226. ENTRY_IMPLEMENT_MAIN(ExampleOcclusion);