headless.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <entry/entry.h>
  6. #include <bgfx_utils.h>
  7. #include <bx/timer.h>
  8. #include <bx/file.h>
  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_layout
  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::VertexLayout ms_layout;
  24. };
  25. bgfx::VertexLayout PosColorVertex::ms_layout;
  26. static PosColorVertex s_cubeVertices[] =
  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_cubeTriList[] =
  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. int _main_(int _argc, char** _argv)
  53. {
  54. bx::printf(
  55. "\n"
  56. "\n"
  57. "\tThis example demonstrates headless initialization.\n"
  58. "\n"
  59. );
  60. Args args(_argc, _argv);
  61. bgfx::Init init;
  62. init.type = args.m_type;
  63. init.vendorId = args.m_pciId;
  64. init.resolution.width = 0;
  65. init.resolution.height = 0;
  66. if (!bgfx::init(init) )
  67. {
  68. bx::printf(
  69. "\t - Failed to initialize headless mode!\n"
  70. "\n"
  71. );
  72. return bx::kExitFailure;
  73. }
  74. bx::printf(
  75. "\t - Headless mode initialized successfuly!\n"
  76. "\n"
  77. );
  78. constexpr uint32_t kWidth = 1280;
  79. constexpr uint32_t kHeight = 720;
  80. // Create vertex stream declaration.
  81. PosColorVertex::init();
  82. // Create static vertex buffer.
  83. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  84. // Static data can be passed with bgfx::makeRef
  85. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  86. , PosColorVertex::ms_layout
  87. );
  88. // Create static index buffer for triangle list rendering.
  89. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(
  90. // Static data can be passed with bgfx::makeRef
  91. bgfx::makeRef(s_cubeTriList, sizeof(s_cubeTriList) )
  92. );
  93. bgfx::ProgramHandle program = loadProgram("vs_cubes", "fs_cubes");
  94. bgfx::FrameBufferHandle fbh = bgfx::createFrameBuffer(
  95. kWidth
  96. , kHeight
  97. , bgfx::TextureFormat::BGRA8
  98. );
  99. bgfx::TextureHandle rb = bgfx::createTexture2D(
  100. kWidth
  101. , kHeight
  102. , false
  103. , 1
  104. , bgfx::TextureFormat::BGRA8
  105. , BGFX_TEXTURE_BLIT_DST|BGFX_TEXTURE_READ_BACK
  106. );
  107. // Set view 0 clear state.
  108. bgfx::setViewClear(0
  109. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  110. , 0x303030ff
  111. , 1.0f
  112. , 0
  113. );
  114. constexpr uint64_t state = 0
  115. | BGFX_STATE_WRITE_RGB
  116. | BGFX_STATE_WRITE_A
  117. | BGFX_STATE_WRITE_Z
  118. | BGFX_STATE_DEPTH_TEST_LESS
  119. | BGFX_STATE_CULL_CW
  120. | BGFX_STATE_MSAA
  121. ;
  122. const uint64_t timeOffset = bx::getHPCounter();
  123. uint32_t currentFrame = 0;
  124. bx::printf(
  125. "\t - Rendering into offscreen framebuffer.\n"
  126. "\n"
  127. );
  128. for (uint32_t ii = 0; ii < 5; ++ii)
  129. {
  130. const float time = (float)( (bx::getHPCounter()-timeOffset)/double(bx::getHPFrequency() ) );
  131. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  132. const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
  133. // Set view and projection matrix for view 0.
  134. {
  135. float view[16];
  136. bx::mtxLookAt(view, eye, at);
  137. float proj[16];
  138. bx::mtxProj(proj, 60.0f, float(kWidth)/float(kHeight), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  139. bgfx::setViewTransform(0, view, proj);
  140. // Set view 0 default viewport.
  141. bgfx::setViewRect(0, 0, 0, uint16_t(kWidth), uint16_t(kHeight) );
  142. bgfx::setViewFrameBuffer(0, fbh);
  143. }
  144. // Submit 11x11 cubes.
  145. for (uint32_t yy = 0; yy < 11; ++yy)
  146. {
  147. for (uint32_t xx = 0; xx < 11; ++xx)
  148. {
  149. float mtx[16];
  150. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  151. mtx[12] = -15.0f + float(xx)*3.0f;
  152. mtx[13] = -15.0f + float(yy)*3.0f;
  153. mtx[14] = 0.0f;
  154. // Set model matrix for rendering.
  155. bgfx::setTransform(mtx);
  156. // Set vertex and index buffer.
  157. bgfx::setVertexBuffer(0, vbh);
  158. bgfx::setIndexBuffer(ibh);
  159. // Set render states.
  160. bgfx::setState(state);
  161. // Submit primitive for rendering to view 0.
  162. bgfx::submit(0, program);
  163. }
  164. }
  165. bgfx::blit(1, rb, 0, 0, bgfx::getTexture(fbh) );
  166. currentFrame = bgfx::frame();
  167. }
  168. bx::FilePath filePath(bx::Dir::Current);
  169. filePath.join("temp/headless.tga");
  170. bx::FileWriter writer;
  171. bx::Error err;
  172. if (bx::open(&writer, filePath, false, &err) )
  173. {
  174. bx::DefaultAllocator allocator;
  175. uint8_t* data = (uint8_t*)bx::alloc(&allocator, kWidth*kHeight*4);
  176. uint32_t expectedFrame = bgfx::readTexture(rb, data);
  177. while (currentFrame < expectedFrame) // Make sure read texture is complete.
  178. {
  179. currentFrame = bgfx::frame();
  180. }
  181. bimg::imageWriteTga(&writer, kWidth, kHeight, kWidth*4, data, false, false, &err);
  182. bx::close(&writer);
  183. bx::free(&allocator, data);
  184. }
  185. bx::printf(
  186. "\t - Screenshot written into:\n"
  187. "\t %s\n"
  188. "\n"
  189. "\t - Shuting it down!\n"
  190. "\n"
  191. , filePath.getCPtr()
  192. );
  193. bgfx::destroy(rb);
  194. bgfx::destroy(fbh);
  195. bgfx::destroy(program);
  196. bgfx::destroy(ibh);
  197. bgfx::destroy(vbh);
  198. bgfx::shutdown();
  199. bx::printf(
  200. "\t - Exiting.\n"
  201. "\n"
  202. "\n"
  203. );
  204. return bx::kExitSuccess;
  205. }