instancing.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. struct PosColorVertex
  8. {
  9. float m_x;
  10. float m_y;
  11. float m_z;
  12. uint32_t m_abgr;
  13. static void init()
  14. {
  15. ms_decl
  16. .begin()
  17. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  18. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  19. .end();
  20. };
  21. static bgfx::VertexDecl ms_decl;
  22. };
  23. bgfx::VertexDecl PosColorVertex::ms_decl;
  24. static PosColorVertex s_cubeVertices[8] =
  25. {
  26. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  27. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  28. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  29. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  30. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  31. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  32. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  33. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  34. };
  35. static const uint16_t s_cubeIndices[36] =
  36. {
  37. 0, 1, 2, // 0
  38. 1, 3, 2,
  39. 4, 6, 5, // 2
  40. 5, 6, 7,
  41. 0, 2, 4, // 4
  42. 4, 2, 6,
  43. 1, 5, 3, // 6
  44. 5, 7, 3,
  45. 0, 4, 1, // 8
  46. 4, 5, 1,
  47. 2, 3, 6, // 10
  48. 6, 3, 7,
  49. };
  50. int _main_(int /*_argc*/, char** /*_argv*/)
  51. {
  52. uint32_t width = 1280;
  53. uint32_t height = 720;
  54. uint32_t debug = BGFX_DEBUG_TEXT;
  55. uint32_t reset = BGFX_RESET_VSYNC;
  56. bgfx::init();
  57. bgfx::reset(width, height, reset);
  58. // Enable debug text.
  59. bgfx::setDebug(debug);
  60. // Set view 0 clear state.
  61. bgfx::setViewClear(0
  62. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  63. , 0x303030ff
  64. , 1.0f
  65. , 0
  66. );
  67. // Get renderer capabilities info.
  68. const bgfx::Caps* caps = bgfx::getCaps();
  69. // Create vertex stream declaration.
  70. PosColorVertex::init();
  71. const bgfx::Memory* mem;
  72. // Create static vertex buffer.
  73. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  74. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, PosColorVertex::ms_decl);
  75. // Create static index buffer.
  76. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  77. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  78. // Create program from shaders.
  79. bgfx::ProgramHandle program = loadProgram("vs_instancing", "fs_instancing");
  80. int64_t timeOffset = bx::getHPCounter();
  81. while (!entry::processEvents(width, height, debug, reset) )
  82. {
  83. // Set view 0 default viewport.
  84. bgfx::setViewRect(0, 0, 0, width, height);
  85. // This dummy draw call is here to make sure that view 0 is cleared
  86. // if no other draw calls are submitted to view 0.
  87. bgfx::touch(0);
  88. int64_t now = bx::getHPCounter();
  89. static int64_t last = now;
  90. const int64_t frameTime = now - last;
  91. last = now;
  92. const double freq = double(bx::getHPFrequency() );
  93. const double toMs = 1000.0/freq;
  94. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  95. // Use debug font to print information about this example.
  96. bgfx::dbgTextClear();
  97. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/05-instancing");
  98. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Geometry instancing.");
  99. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  100. // Check if instancing is supported.
  101. if (0 == (BGFX_CAPS_INSTANCING & caps->supported) )
  102. {
  103. // When instancing is not supported by GPU, implement alternative
  104. // code path that doesn't use instancing.
  105. bool blink = uint32_t(time*3.0f)&1;
  106. bgfx::dbgTextPrintf(0, 5, blink ? 0x1f : 0x01, " Instancing is not supported by GPU. ");
  107. }
  108. else
  109. {
  110. float at[3] = { 0.0f, 0.0f, 0.0f };
  111. float eye[3] = { 0.0f, 0.0f, -35.0f };
  112. // Set view and projection matrix for view 0.
  113. const bgfx::HMD* hmd = bgfx::getHMD();
  114. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  115. {
  116. float view[16];
  117. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  118. float proj[16];
  119. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  120. bgfx::setViewTransform(0, view, proj);
  121. // Set view 0 default viewport.
  122. //
  123. // Use HMD's width/height since HMD's internal frame buffer size
  124. // might be much larger than window size.
  125. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  126. }
  127. else
  128. {
  129. float view[16];
  130. bx::mtxLookAt(view, eye, at);
  131. float proj[16];
  132. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  133. bgfx::setViewTransform(0, view, proj);
  134. // Set view 0 default viewport.
  135. bgfx::setViewRect(0, 0, 0, width, height);
  136. }
  137. const uint16_t instanceStride = 80;
  138. const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(121, instanceStride);
  139. if (NULL != idb)
  140. {
  141. uint8_t* data = idb->data;
  142. // Write instance data for 11x11 cubes.
  143. for (uint32_t yy = 0, numInstances = 0; yy < 11 && numInstances < idb->num; ++yy)
  144. {
  145. for (uint32_t xx = 0; xx < 11 && numInstances < idb->num; ++xx, ++numInstances)
  146. {
  147. float* mtx = (float*)data;
  148. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  149. mtx[12] = -15.0f + float(xx)*3.0f;
  150. mtx[13] = -15.0f + float(yy)*3.0f;
  151. mtx[14] = 0.0f;
  152. float* color = (float*)&data[64];
  153. color[0] = sinf(time+float(xx)/11.0f)*0.5f+0.5f;
  154. color[1] = cosf(time+float(yy)/11.0f)*0.5f+0.5f;
  155. color[2] = sinf(time*3.0f)*0.5f+0.5f;
  156. color[3] = 1.0f;
  157. data += instanceStride;
  158. }
  159. }
  160. // Set vertex and index buffer.
  161. bgfx::setVertexBuffer(vbh);
  162. bgfx::setIndexBuffer(ibh);
  163. // Set instance data buffer.
  164. bgfx::setInstanceDataBuffer(idb);
  165. // Set render states.
  166. bgfx::setState(BGFX_STATE_DEFAULT);
  167. // Submit primitive for rendering to view 0.
  168. bgfx::submit(0, program);
  169. }
  170. }
  171. // Advance to next frame. Rendering thread will be kicked to
  172. // process submitted rendering primitives.
  173. bgfx::frame();
  174. }
  175. // Cleanup.
  176. bgfx::destroyIndexBuffer(ibh);
  177. bgfx::destroyVertexBuffer(vbh);
  178. bgfx::destroyProgram(program);
  179. // Shutdown bgfx.
  180. bgfx::shutdown();
  181. return 0;
  182. }