instancing.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx.h>
  6. #include <bx/bx.h>
  7. #include <bx/timer.h>
  8. #include "../common/entry.h"
  9. #include "../common/dbg.h"
  10. #include "../common/math.h"
  11. #include "../common/processevents.h"
  12. #include <stdio.h>
  13. #include <string.h>
  14. struct PosColorVertex
  15. {
  16. float m_x;
  17. float m_y;
  18. float m_z;
  19. uint32_t m_abgr;
  20. };
  21. static bgfx::VertexDecl s_PosColorDecl;
  22. static PosColorVertex s_cubeVertices[8] =
  23. {
  24. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  25. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  26. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  27. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  28. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  29. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  30. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  31. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  32. };
  33. static const uint16_t s_cubeIndices[36] =
  34. {
  35. 0, 1, 2, // 0
  36. 1, 3, 2,
  37. 4, 6, 5, // 2
  38. 5, 6, 7,
  39. 0, 2, 4, // 4
  40. 4, 2, 6,
  41. 1, 5, 3, // 6
  42. 5, 7, 3,
  43. 0, 4, 1, // 8
  44. 4, 5, 1,
  45. 2, 3, 6, // 10
  46. 6, 3, 7,
  47. };
  48. static const char* s_shaderPath = NULL;
  49. static void shaderFilePath(char* _out, const char* _name)
  50. {
  51. strcpy(_out, s_shaderPath);
  52. strcat(_out, _name);
  53. strcat(_out, ".bin");
  54. }
  55. long int fsize(FILE* _file)
  56. {
  57. long int pos = ftell(_file);
  58. fseek(_file, 0L, SEEK_END);
  59. long int size = ftell(_file);
  60. fseek(_file, pos, SEEK_SET);
  61. return size;
  62. }
  63. static const bgfx::Memory* load(const char* _filePath)
  64. {
  65. FILE* file = fopen(_filePath, "rb");
  66. if (NULL != file)
  67. {
  68. uint32_t size = (uint32_t)fsize(file);
  69. const bgfx::Memory* mem = bgfx::alloc(size+1);
  70. size_t ignore = fread(mem->data, 1, size, file);
  71. BX_UNUSED(ignore);
  72. fclose(file);
  73. mem->data[mem->size-1] = '\0';
  74. return mem;
  75. }
  76. return NULL;
  77. }
  78. static const bgfx::Memory* loadShader(const char* _name)
  79. {
  80. char filePath[512];
  81. shaderFilePath(filePath, _name);
  82. return load(filePath);
  83. }
  84. int _main_(int /*_argc*/, char** /*_argv*/)
  85. {
  86. uint32_t width = 1280;
  87. uint32_t height = 720;
  88. uint32_t debug = BGFX_DEBUG_TEXT;
  89. uint32_t reset = BGFX_RESET_NONE;
  90. bgfx::init();
  91. bgfx::reset(width, height);
  92. // Enable debug text.
  93. bgfx::setDebug(debug);
  94. // Set view 0 clear state.
  95. bgfx::setViewClear(0
  96. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  97. , 0x303030ff
  98. , 1.0f
  99. , 0
  100. );
  101. // Setup root path for binary shaders. Shader binaries are different
  102. // for each renderer.
  103. switch (bgfx::getRendererType() )
  104. {
  105. default:
  106. case bgfx::RendererType::Direct3D9:
  107. s_shaderPath = "shaders/dx9/";
  108. break;
  109. case bgfx::RendererType::Direct3D11:
  110. s_shaderPath = "shaders/dx11/";
  111. break;
  112. case bgfx::RendererType::OpenGL:
  113. s_shaderPath = "shaders/glsl/";
  114. break;
  115. case bgfx::RendererType::OpenGLES2:
  116. case bgfx::RendererType::OpenGLES3:
  117. s_shaderPath = "shaders/gles/";
  118. break;
  119. }
  120. // Create vertex stream declaration.
  121. s_PosColorDecl.begin();
  122. s_PosColorDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  123. s_PosColorDecl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  124. s_PosColorDecl.end();
  125. const bgfx::Memory* mem;
  126. // Create static vertex buffer.
  127. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  128. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosColorDecl);
  129. // Create static index buffer.
  130. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  131. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  132. // Load vertex shader.
  133. mem = loadShader("vs_instancing");
  134. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  135. // Load fragment shader.
  136. mem = loadShader("fs_instancing");
  137. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  138. // Create program from shaders.
  139. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  140. // We can destroy vertex and fragment shader here since
  141. // their reference is kept inside bgfx after calling createProgram.
  142. // Vertex and fragment shader will be destroyed once program is
  143. // destroyed.
  144. bgfx::destroyVertexShader(vsh);
  145. bgfx::destroyFragmentShader(fsh);
  146. while (!processEvents(width, height, debug, reset) )
  147. {
  148. // Set view 0 default viewport.
  149. bgfx::setViewRect(0, 0, 0, width, height);
  150. // This dummy draw call is here to make sure that view 0 is cleared
  151. // if no other draw calls are submitted to view 0.
  152. bgfx::submit(0);
  153. int64_t now = bx::getHPCounter();
  154. static int64_t last = now;
  155. const int64_t frameTime = now - last;
  156. last = now;
  157. const double freq = double(bx::getHPFrequency() );
  158. const double toMs = 1000.0/freq;
  159. // Use debug font to print information about this example.
  160. bgfx::dbgTextClear();
  161. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/05-instancing");
  162. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Geometry instancing.");
  163. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  164. float at[3] = { 0.0f, 0.0f, 0.0f };
  165. float eye[3] = { 0.0f, 0.0f, -35.0f };
  166. float view[16];
  167. float proj[16];
  168. mtxLookAt(view, eye, at);
  169. mtxProj(proj, 60.0f, 16.0f/9.0f, 0.1f, 100.0f);
  170. // Set view and projection matrix for view 0.
  171. bgfx::setViewTransform(0, view, proj);
  172. float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) );
  173. const uint16_t instanceStride = 80;
  174. const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(121, instanceStride);
  175. if (NULL != idb)
  176. {
  177. uint8_t* data = idb->data;
  178. // Write instance data for 11x11 cubes.
  179. for (uint32_t yy = 0; yy < 11; ++yy)
  180. {
  181. for (uint32_t xx = 0; xx < 11; ++xx)
  182. {
  183. float* mtx = (float*)data;
  184. mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  185. mtx[12] = -15.0f + float(xx)*3.0f;
  186. mtx[13] = -15.0f + float(yy)*3.0f;
  187. mtx[14] = 0.0f;
  188. float* color = (float*)&data[64];
  189. color[0] = sin(time+float(xx)/11.0f)*0.5f+0.5f;
  190. color[1] = cos(time+float(yy)/11.0f)*0.5f+0.5f;
  191. color[2] = sin(time*3.0f)*0.5f+0.5f;
  192. color[3] = 1.0f;
  193. data += instanceStride;
  194. }
  195. }
  196. // Set vertex and fragment shaders.
  197. bgfx::setProgram(program);
  198. // Set vertex and index buffer.
  199. bgfx::setVertexBuffer(vbh);
  200. bgfx::setIndexBuffer(ibh);
  201. // Set instance data buffer.
  202. bgfx::setInstanceDataBuffer(idb);
  203. // Set render states.
  204. bgfx::setState(BGFX_STATE_DEFAULT);
  205. // Submit primitive for rendering to view 0.
  206. bgfx::submit(0);
  207. }
  208. // Advance to next frame. Rendering thread will be kicked to
  209. // process submitted rendering primitives.
  210. bgfx::frame();
  211. }
  212. // Cleanup.
  213. bgfx::destroyIndexBuffer(ibh);
  214. bgfx::destroyVertexBuffer(vbh);
  215. bgfx::destroyProgram(program);
  216. // Shutdown bgfx.
  217. bgfx::shutdown();
  218. return 0;
  219. }