instancing.cpp 5.0 KB

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