callback.cpp 11 KB

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