callback.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #include <bx/allocator.h>
  8. #include <bx/string.h>
  9. #include "aviwriter.h"
  10. #include <inttypes.h>
  11. struct PosColorVertex
  12. {
  13. float m_x;
  14. float m_y;
  15. float m_z;
  16. uint32_t m_abgr;
  17. static void init()
  18. {
  19. ms_decl
  20. .begin()
  21. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  22. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  23. .end();
  24. };
  25. static bgfx::VertexDecl ms_decl;
  26. };
  27. bgfx::VertexDecl PosColorVertex::ms_decl;
  28. static PosColorVertex s_cubeVertices[8] =
  29. {
  30. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  31. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  32. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  33. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  34. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  35. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  36. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  37. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  38. };
  39. static const uint16_t s_cubeIndices[36] =
  40. {
  41. 0, 1, 2, // 0
  42. 1, 3, 2,
  43. 4, 6, 5, // 2
  44. 5, 6, 7,
  45. 0, 2, 4, // 4
  46. 4, 2, 6,
  47. 1, 5, 3, // 6
  48. 5, 7, 3,
  49. 0, 4, 1, // 8
  50. 4, 5, 1,
  51. 2, 3, 6, // 10
  52. 6, 3, 7,
  53. };
  54. void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip)
  55. {
  56. FILE* file = fopen(_filePath, "wb");
  57. if (NULL != file)
  58. {
  59. uint8_t type = _grayscale ? 3 : 2;
  60. uint8_t bpp = _grayscale ? 8 : 32;
  61. putc(0, file);
  62. putc(0, file);
  63. putc(type, file);
  64. putc(0, file);
  65. putc(0, file);
  66. putc(0, file);
  67. putc(0, file);
  68. putc(0, file);
  69. putc(0, file);
  70. putc(0, file);
  71. putc(0, file);
  72. putc(0, file);
  73. putc(_width&0xff, file);
  74. putc( (_width>>8)&0xff, file);
  75. putc(_height&0xff, file);
  76. putc( (_height>>8)&0xff, file);
  77. putc(bpp, file);
  78. putc(32, file);
  79. uint32_t dstPitch = _width*bpp/8;
  80. if (_yflip)
  81. {
  82. uint8_t* data = (uint8_t*)_src + _srcPitch*_height - _srcPitch;
  83. for (uint32_t yy = 0; yy < _height; ++yy)
  84. {
  85. fwrite(data, dstPitch, 1, file);
  86. data -= _srcPitch;
  87. }
  88. }
  89. else
  90. {
  91. uint8_t* data = (uint8_t*)_src;
  92. for (uint32_t yy = 0; yy < _height; ++yy)
  93. {
  94. fwrite(data, dstPitch, 1, file);
  95. data += _srcPitch;
  96. }
  97. }
  98. fclose(file);
  99. }
  100. }
  101. long int fsize(FILE* _file)
  102. {
  103. long int pos = ftell(_file);
  104. fseek(_file, 0L, SEEK_END);
  105. long int size = ftell(_file);
  106. fseek(_file, pos, SEEK_SET);
  107. return size;
  108. }
  109. struct BgfxCallback : public bgfx::CallbackI
  110. {
  111. virtual ~BgfxCallback()
  112. {
  113. }
  114. virtual void fatal(bgfx::Fatal::Enum _code, const char* _str) BX_OVERRIDE
  115. {
  116. // Something unexpected happened, inform user and bail out.
  117. dbgPrintf("Fatal error: 0x%08x: %s", _code, _str);
  118. // Must terminate, continuing will cause crash anyway.
  119. abort();
  120. }
  121. virtual uint32_t cacheReadSize(uint64_t _id) BX_OVERRIDE
  122. {
  123. char filePath[256];
  124. bx::snprintf(filePath, sizeof(filePath), "%016" PRIx64, _id);
  125. // Use cache id as filename.
  126. FILE* file = fopen(filePath, "rb");
  127. if (NULL != file)
  128. {
  129. uint32_t size = fsize(file);
  130. fclose(file);
  131. // Return size of shader file.
  132. return size;
  133. }
  134. // Return 0 if shader is not found.
  135. return 0;
  136. }
  137. virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) BX_OVERRIDE
  138. {
  139. char filePath[256];
  140. bx::snprintf(filePath, sizeof(filePath), "temp/%016" PRIx64, _id);
  141. // Use cache id as filename.
  142. FILE* file = fopen(filePath, "rb");
  143. if (NULL != file)
  144. {
  145. // Read shader.
  146. size_t result = fread(_data, 1, _size, file);
  147. fclose(file);
  148. // Make sure that read size matches requested size.
  149. return result == _size;
  150. }
  151. // Shader is not found in cache, needs to be rebuilt.
  152. return false;
  153. }
  154. virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) BX_OVERRIDE
  155. {
  156. char filePath[256];
  157. bx::snprintf(filePath, sizeof(filePath), "temp/%016" PRIx64, _id);
  158. // Use cache id as filename.
  159. FILE* file = fopen(filePath, "wb");
  160. if (NULL != file)
  161. {
  162. // Write shader to cache location.
  163. fwrite(_data, 1, _size, file);
  164. fclose(file);
  165. }
  166. }
  167. virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t /*_size*/, bool _yflip) BX_OVERRIDE
  168. {
  169. char temp[1024];
  170. // Save screen shot as TGA.
  171. bx::snprintf(temp, BX_COUNTOF(temp), "%s.mip0.tga", _filePath);
  172. saveTga(temp, _width, _height, _pitch, _data, false, _yflip);
  173. uint32_t width = _width;
  174. uint32_t height = _height;
  175. uint32_t pitch = _pitch;
  176. uint8_t* data = (uint8_t*)_data;
  177. // Generate mip maps.
  178. uint32_t mip = 1;
  179. for (; 2 <= width && 2 <= height; ++mip)
  180. {
  181. bx::snprintf(temp, BX_COUNTOF(temp), "%s.mip%d.tga", _filePath, mip);
  182. bgfx::imageRgba8Downsample2x2(width, height, pitch, data, data);
  183. width >>= 1;
  184. height >>= 1;
  185. pitch = width*4;
  186. saveTga(temp, width, height, pitch, _data, false, _yflip);
  187. }
  188. if (width > height)
  189. {
  190. for (; 2 <= width; ++mip)
  191. {
  192. memcpy(&data[width*4], data, width*4);
  193. bx::snprintf(temp, BX_COUNTOF(temp), "%s.mip%d.tga", _filePath, mip);
  194. bgfx::imageRgba8Downsample2x2(width, 2, pitch, data, data);
  195. width >>= 1;
  196. pitch = width*4;
  197. saveTga(temp, width, 2, pitch, _data, false, _yflip);
  198. }
  199. }
  200. else
  201. {
  202. for (; 2 <= height; ++mip)
  203. {
  204. uint32_t* src = (uint32_t*)data;
  205. for (uint32_t ii = 0; ii < height; ++ii, src += 2)
  206. {
  207. src[1] = src[0];
  208. }
  209. bx::snprintf(temp, BX_COUNTOF(temp), "%s.mip%d.tga", _filePath, mip);
  210. bgfx::imageRgba8Downsample2x2(2, height, 8, data, data);
  211. height >>= 1;
  212. saveTga(temp, 2, height, 8, _data, false, _yflip);
  213. }
  214. }
  215. }
  216. virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool _yflip) BX_OVERRIDE
  217. {
  218. m_writer = new AviWriter(entry::getFileWriter() );
  219. if (!m_writer->open("temp/capture.avi", _width, _height, 60, _yflip) )
  220. {
  221. delete m_writer;
  222. m_writer = NULL;
  223. }
  224. }
  225. virtual void captureEnd() BX_OVERRIDE
  226. {
  227. if (NULL != m_writer)
  228. {
  229. m_writer->close();
  230. delete m_writer;
  231. m_writer = NULL;
  232. }
  233. }
  234. virtual void captureFrame(const void* _data, uint32_t /*_size*/) BX_OVERRIDE
  235. {
  236. if (NULL != m_writer)
  237. {
  238. m_writer->frame(_data);
  239. }
  240. }
  241. AviWriter* m_writer;
  242. };
  243. class BgfxAllocator : public bx::ReallocatorI
  244. {
  245. public:
  246. BgfxAllocator()
  247. : m_numBlocks(0)
  248. , m_maxBlocks(0)
  249. {
  250. }
  251. virtual ~BgfxAllocator()
  252. {
  253. }
  254. virtual void* alloc(size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  255. {
  256. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  257. {
  258. void* ptr = ::malloc(_size);
  259. dbgPrintf("%s(%d): ALLOC %p of %d byte(s)\n", _file, _line, ptr, _size);
  260. ++m_numBlocks;
  261. m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
  262. return ptr;
  263. }
  264. return bx::alignedAlloc(this, _size, _align, _file, _line);
  265. }
  266. virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  267. {
  268. if (NULL != _ptr)
  269. {
  270. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  271. {
  272. dbgPrintf("%s(%d): FREE %p\n", _file, _line, _ptr);
  273. ::free(_ptr);
  274. --m_numBlocks;
  275. }
  276. else
  277. {
  278. bx::alignedFree(this, _ptr, _align, _file, _line);
  279. }
  280. }
  281. }
  282. virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  283. {
  284. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  285. {
  286. void* ptr = ::realloc(_ptr, _size);
  287. dbgPrintf("%s(%d): REALLOC %p (old %p) of %d byte(s)\n", _file, _line, ptr, _ptr, _size);
  288. if (NULL == _ptr)
  289. {
  290. ++m_numBlocks;
  291. m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
  292. }
  293. return ptr;
  294. }
  295. return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line);
  296. }
  297. void dumpStats() const
  298. {
  299. dbgPrintf("Allocator stats: num blocks %d (peak: %d)\n", m_numBlocks, m_maxBlocks);
  300. }
  301. private:
  302. uint32_t m_numBlocks;
  303. uint32_t m_maxBlocks;
  304. };
  305. int _main_(int /*_argc*/, char** /*_argv*/)
  306. {
  307. BgfxCallback callback;
  308. BgfxAllocator allocator;
  309. uint32_t width = 1280;
  310. uint32_t height = 720;
  311. // Enumerate supported backend renderers.
  312. bgfx::RendererType::Enum renderers[bgfx::RendererType::Count];
  313. uint8_t numRenderers = bgfx::getSupportedRenderers(renderers);
  314. bgfx::init(
  315. renderers[bx::getHPCounter() % numRenderers] /* randomize renderer */
  316. , &callback // custom callback handler
  317. , &allocator // custom allocator
  318. );
  319. bgfx::reset(width, height, BGFX_RESET_CAPTURE|BGFX_RESET_MSAA_X16);
  320. // Enable debug text.
  321. bgfx::setDebug(BGFX_DEBUG_TEXT);
  322. // Set view 0 default viewport.
  323. bgfx::setViewRect(0, 0, 0, 1280, 720);
  324. // Set view 0 clear state.
  325. bgfx::setViewClear(0
  326. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  327. , 0x303030ff
  328. , 1.0f
  329. , 0
  330. );
  331. // Create vertex stream declaration.
  332. PosColorVertex::init();
  333. // Create static vertex buffer.
  334. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  335. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  336. , PosColorVertex::ms_decl
  337. );
  338. // Create static index buffer.
  339. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  340. // Create program from shaders.
  341. bgfx::ProgramHandle program = loadProgram("vs_callback", "fs_callback");
  342. float time = 0.0f;
  343. const bgfx::RendererType::Enum rendererType = bgfx::getRendererType();
  344. // 5 second 60Hz video
  345. for (uint32_t frame = 0; frame < 300; ++frame)
  346. {
  347. // This dummy draw call is here to make sure that view 0 is cleared
  348. // if no other draw calls are submitted to view 0.
  349. bgfx::submit(0);
  350. int64_t now = bx::getHPCounter();
  351. static int64_t last = now;
  352. const int64_t frameTime = now - last;
  353. last = now;
  354. const double freq = double(bx::getHPFrequency() );
  355. const double toMs = 1000.0/freq;
  356. // Use debug font to print information about this example.
  357. bgfx::dbgTextClear();
  358. bgfx::dbgTextPrintf( 0, 1, 0x4f, "bgfx/examples/07-callback");
  359. bgfx::dbgTextPrintf( 0, 2, 0x6f, "Description: Implementing application specific callbacks for taking screen shots,");
  360. bgfx::dbgTextPrintf(13, 3, 0x6f, "caching OpenGL binary shaders, and video capture.");
  361. bgfx::dbgTextPrintf( 0, 4, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  362. bgfx::dbgTextPrintf( 2, 6, 0x0e, "Supported renderers:");
  363. for (uint8_t ii = 0; ii < numRenderers; ++ii)
  364. {
  365. bgfx::dbgTextPrintf( 2, 7+ii, 0x0c, "[%c] %s"
  366. , renderers[ii] == rendererType ? '\xfe' : ' '
  367. , bgfx::getRendererName(renderers[ii])
  368. );
  369. }
  370. float at[3] = { 0.0f, 0.0f, 0.0f };
  371. float eye[3] = { 0.0f, 0.0f, -35.0f };
  372. float view[16];
  373. float proj[16];
  374. bx::mtxLookAt(view, eye, at);
  375. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  376. // Set view and projection matrix for view 0.
  377. bgfx::setViewTransform(0, view, proj);
  378. time += 1.0f/60.0f;
  379. // Submit 11x11 cubes.
  380. for (uint32_t yy = 0; yy < 11; ++yy)
  381. {
  382. for (uint32_t xx = 0; xx < 11-yy; ++xx)
  383. {
  384. float mtx[16];
  385. bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  386. mtx[12] = -15.0f + float(xx)*3.0f;
  387. mtx[13] = -15.0f + float(yy)*3.0f;
  388. mtx[14] = 0.0f;
  389. // Set model matrix for rendering.
  390. bgfx::setTransform(mtx);
  391. // Set vertex and fragment shaders.
  392. bgfx::setProgram(program);
  393. // Set vertex and index buffer.
  394. bgfx::setVertexBuffer(vbh);
  395. bgfx::setIndexBuffer(ibh);
  396. // Set render states.
  397. bgfx::setState(BGFX_STATE_DEFAULT);
  398. // Submit primitive for rendering to view 0.
  399. bgfx::submit(0);
  400. }
  401. }
  402. // Take screen shot at frame 150.
  403. if (150 == frame)
  404. {
  405. bgfx::saveScreenShot("temp/frame150");
  406. }
  407. // Advance to next frame. Rendering thread will be kicked to
  408. // process submitted rendering primitives.
  409. bgfx::frame();
  410. }
  411. // Cleanup.
  412. bgfx::destroyIndexBuffer(ibh);
  413. bgfx::destroyVertexBuffer(vbh);
  414. bgfx::destroyProgram(program);
  415. // Shutdown bgfx.
  416. bgfx::shutdown();
  417. allocator.dumpStats();
  418. return 0;
  419. }