update.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include <bx/uint32_t.h>
  8. #include "packrect.h"
  9. #include <list>
  10. struct PosTexcoordVertex
  11. {
  12. float m_x;
  13. float m_y;
  14. float m_z;
  15. float m_u;
  16. float m_v;
  17. float m_w;
  18. static void init()
  19. {
  20. ms_decl
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  24. .end();
  25. };
  26. static bgfx::VertexDecl ms_decl;
  27. };
  28. bgfx::VertexDecl PosTexcoordVertex::ms_decl;
  29. static PosTexcoordVertex s_m_cubeVertices[28] =
  30. {
  31. {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
  32. { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
  33. {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
  34. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  35. {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
  36. { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
  37. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
  38. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  39. {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
  40. {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
  41. {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
  42. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
  43. { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
  44. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  45. { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
  46. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  47. {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
  48. { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
  49. {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
  50. { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
  51. {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
  52. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
  53. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  54. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  55. {-1.0f, 1.0f, 1.0f, -2.0f, 2.0f, 2.0f },
  56. { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f },
  57. {-1.0f, -1.0f, 1.0f, -2.0f, -2.0f, 2.0f },
  58. { 1.0f, -1.0f, 1.0f, 2.0f, -2.0f, 2.0f },
  59. };
  60. static const uint16_t s_m_cubeIndices[36] =
  61. {
  62. 0, 1, 2, // 0
  63. 1, 3, 2,
  64. 4, 6, 5, // 2
  65. 5, 6, 7,
  66. 8, 10, 9, // 4
  67. 9, 10, 11,
  68. 12, 14, 13, // 6
  69. 14, 15, 13,
  70. 16, 18, 17, // 8
  71. 18, 19, 17,
  72. 20, 22, 21, // 10
  73. 21, 22, 23,
  74. };
  75. static void updateTextureCubeRectBgra8(bgfx::TextureHandle _handle, uint8_t _side, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height, uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a = 0xff)
  76. {
  77. bgfx::TextureInfo ti;
  78. bgfx::calcTextureSize(ti, _width, _height, 1, 1, false, bgfx::TextureFormat::BGRA8);
  79. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  80. uint8_t* data = (uint8_t*)mem->data;
  81. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  82. {
  83. data[0] = _b;
  84. data[1] = _g;
  85. data[2] = _r;
  86. data[3] = _a;
  87. data += 4;
  88. }
  89. bgfx::updateTextureCube(_handle, _side, 0, _x, _y, _width, _height, mem);
  90. }
  91. static const uint32_t m_textureside = 512;
  92. static const uint32_t m_texture2dSize = 256;
  93. class ExampleUpdate : public entry::AppI
  94. {
  95. public:
  96. ExampleUpdate()
  97. : m_cube(m_textureside)
  98. {
  99. }
  100. void init(int _argc, char** _argv) BX_OVERRIDE
  101. {
  102. Args args(_argc, _argv);
  103. m_width = 1280;
  104. m_height = 720;
  105. m_debug = BGFX_DEBUG_TEXT;
  106. m_reset = BGFX_RESET_VSYNC;
  107. bgfx::init(args.m_type, args.m_pciId);
  108. bgfx::reset(m_width, m_height, m_reset);
  109. // Enable debug text.
  110. bgfx::setDebug(m_debug);
  111. // Set view 0 clear state.
  112. bgfx::setViewClear(0
  113. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  114. , 0x303030ff
  115. , 1.0f
  116. , 0
  117. );
  118. // Create vertex stream declaration.
  119. PosTexcoordVertex::init();
  120. m_textures[0] = loadTexture("texture_compression_bc1.dds", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
  121. m_textures[1] = loadTexture("texture_compression_bc2.dds", BGFX_TEXTURE_U_CLAMP);
  122. m_textures[2] = loadTexture("texture_compression_bc3.dds", BGFX_TEXTURE_V_CLAMP);
  123. m_textures[3] = loadTexture("texture_compression_etc1.ktx", BGFX_TEXTURE_U_BORDER|BGFX_TEXTURE_V_BORDER|BGFX_TEXTURE_BORDER_COLOR(1) );
  124. m_textures[4] = loadTexture("texture_compression_etc2.ktx");
  125. m_textures[5] = loadTexture("texture_compression_ptc12.pvr");
  126. m_textures[6] = loadTexture("texture_compression_ptc14.pvr");
  127. m_textures[7] = loadTexture("texture_compression_ptc22.pvr");
  128. m_textures[8] = loadTexture("texture_compression_ptc24.pvr");
  129. const bgfx::Caps* caps = bgfx::getCaps();
  130. m_texture3DSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_3D);
  131. m_blitSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_BLIT);
  132. m_numm_textures3d = 0;
  133. if (m_texture3DSupported)
  134. {
  135. const bgfx::Memory* mem8 = bgfx::alloc(32*32*32);
  136. const bgfx::Memory* mem16f = bgfx::alloc(32*32*32*2);
  137. const bgfx::Memory* mem32f = bgfx::alloc(32*32*32*4);
  138. for (uint8_t zz = 0; zz < 32; ++zz)
  139. {
  140. for (uint8_t yy = 0; yy < 32; ++yy)
  141. {
  142. for (uint8_t xx = 0; xx < 32; ++xx)
  143. {
  144. const uint32_t offset = ( (zz*32+yy)*32+xx);
  145. const uint32_t val = xx ^ yy ^ zz;
  146. mem8->data[offset] = val<<3;
  147. *(uint16_t*)&mem16f->data[offset*2] = bx::halfFromFloat( (float)val/32.0f);
  148. *(float*)&mem32f->data[offset*4] = (float)val/32.0f;
  149. }
  150. }
  151. }
  152. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R8]) )
  153. {
  154. m_textures3d[m_numm_textures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R8, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem8);
  155. }
  156. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R16F]) )
  157. {
  158. m_textures3d[m_numm_textures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R16F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem16f);
  159. }
  160. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R32F]) )
  161. {
  162. m_textures3d[m_numm_textures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R32F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem32f);
  163. }
  164. }
  165. // Create static vertex buffer.
  166. m_vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_m_cubeVertices, sizeof(s_m_cubeVertices) ), PosTexcoordVertex::ms_decl);
  167. // Create static index buffer.
  168. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_m_cubeIndices, sizeof(s_m_cubeIndices) ) );
  169. // Create programs.
  170. m_program = loadProgram("vs_update", "fs_update");
  171. m_programCmp = loadProgram("vs_update", "fs_update_cmp");
  172. m_program3d.idx = bgfx::invalidHandle;
  173. if (m_texture3DSupported)
  174. {
  175. m_program3d = loadProgram("vs_update", "fs_update_3d");
  176. }
  177. // Create texture sampler uniforms.
  178. s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  179. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  180. // Create time uniform.
  181. u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
  182. m_textureCube[0] = bgfx::createTextureCube(m_textureside, 1
  183. , bgfx::TextureFormat::BGRA8
  184. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  185. );
  186. if (m_blitSupported)
  187. {
  188. m_textureCube[1] = bgfx::createTextureCube(m_textureside, 1
  189. , bgfx::TextureFormat::BGRA8
  190. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT|BGFX_TEXTURE_BLIT_DST
  191. );
  192. }
  193. m_texture2d = bgfx::createTexture2D(m_texture2dSize, m_texture2dSize, 1
  194. , bgfx::TextureFormat::BGRA8
  195. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  196. );
  197. m_texture2dData = (uint8_t*)malloc(m_texture2dSize*m_texture2dSize*4);
  198. m_rr = rand()%255;
  199. m_gg = rand()%255;
  200. m_bb = rand()%255;
  201. m_hit = 0;
  202. m_miss = 0;
  203. m_updateTime = 0;
  204. m_timeOffset = bx::getHPCounter();
  205. }
  206. virtual int shutdown() BX_OVERRIDE
  207. {
  208. // m_texture2dData is managed from main thread, and it's passed to renderer
  209. // just as MemoryRef. At this point render might be using it. We must wait
  210. // previous frame to finish before we can free it.
  211. bgfx::frame();
  212. // Cleanup.
  213. free(m_texture2dData);
  214. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  215. {
  216. bgfx::destroyTexture(m_textures[ii]);
  217. }
  218. for (uint32_t ii = 0; ii < m_numm_textures3d; ++ii)
  219. {
  220. bgfx::destroyTexture(m_textures3d[ii]);
  221. }
  222. bgfx::destroyTexture(m_texture2d);
  223. bgfx::destroyTexture(m_textureCube[0]);
  224. if (m_blitSupported)
  225. {
  226. bgfx::destroyTexture(m_textureCube[1]);
  227. }
  228. bgfx::destroyIndexBuffer(m_ibh);
  229. bgfx::destroyVertexBuffer(m_vbh);
  230. if (bgfx::isValid(m_program3d) )
  231. {
  232. bgfx::destroyProgram(m_program3d);
  233. }
  234. bgfx::destroyProgram(m_programCmp);
  235. bgfx::destroyProgram(m_program);
  236. bgfx::destroyUniform(u_time);
  237. bgfx::destroyUniform(s_texColor);
  238. bgfx::destroyUniform(s_texCube);
  239. // Shutdown bgfx.
  240. bgfx::shutdown();
  241. return 0;
  242. }
  243. bool update() BX_OVERRIDE
  244. {
  245. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  246. {
  247. float borderColor[4] = { float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f };
  248. bgfx::setPaletteColor(1, borderColor);
  249. // Set view 0 and 1 viewport.
  250. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  251. bgfx::setViewRect(1, 0, 0, m_width, m_height);
  252. // This dummy draw call is here to make sure that view 0 is cleared
  253. // if no other draw calls are submitted to view 0.
  254. bgfx::touch(0);
  255. int64_t now = bx::getHPCounter();
  256. static int64_t last = now;
  257. const int64_t frameTime = now - last;
  258. last = now;
  259. const int64_t freq = bx::getHPFrequency();
  260. const double toMs = 1000.0/double(freq);
  261. float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
  262. bgfx::setUniform(u_time, &time);
  263. // Use debug font to print information about this example.
  264. bgfx::dbgTextClear();
  265. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  266. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating m_textures.");
  267. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  268. if (now > m_updateTime)
  269. {
  270. PackCube face;
  271. uint32_t bw = bx::uint16_max(1, rand()%(m_textureside/4) );
  272. uint32_t bh = bx::uint16_max(1, rand()%(m_textureside/4) );
  273. if (m_cube.find(bw, bh, face) )
  274. {
  275. m_quads.push_back(face);
  276. ++m_hit;
  277. const Pack2D& rect = face.m_rect;
  278. updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, m_rr, m_gg, m_bb);
  279. if (m_blitSupported)
  280. {
  281. bgfx::blit(0, m_textureCube[1], 0, rect.m_x, rect.m_y, face.m_side, m_textureCube[0], 0, rect.m_x, rect.m_y, face.m_side, rect.m_width, rect.m_height);
  282. }
  283. m_rr = rand()%255;
  284. m_gg = rand()%255;
  285. m_bb = rand()%255;
  286. }
  287. else
  288. {
  289. ++m_miss;
  290. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
  291. {
  292. face = m_quads.front();
  293. const Pack2D& rect = face.m_rect;
  294. updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
  295. if (m_blitSupported)
  296. {
  297. bgfx::blit(0, m_textureCube[1], 0, rect.m_x, rect.m_y, face.m_side, m_textureCube[0], 0, rect.m_x, rect.m_y, face.m_side, rect.m_width, rect.m_height);
  298. }
  299. m_cube.clear(face);
  300. m_quads.pop_front();
  301. }
  302. }
  303. {
  304. // Fill rect.
  305. const uint32_t pitch = m_texture2dSize*4;
  306. const uint16_t tw = rand()%m_texture2dSize;
  307. const uint16_t th = rand()%m_texture2dSize;
  308. const uint16_t tx = rand()%(m_texture2dSize-tw);
  309. const uint16_t ty = rand()%(m_texture2dSize-th);
  310. uint8_t* dst = &m_texture2dData[(ty*m_texture2dSize+tx)*4];
  311. uint8_t* next = dst + pitch;
  312. // Using makeRef to pass texture memory without copying.
  313. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  314. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  315. {
  316. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  317. {
  318. dst[0] = m_bb;
  319. dst[1] = m_gg;
  320. dst[2] = m_rr;
  321. dst[3] = 255;
  322. }
  323. }
  324. // Pitch here makes possible to pass data from source to destination
  325. // without need for m_textures and allocated memory to be the same size.
  326. bgfx::updateTexture2D(m_texture2d, 0, tx, ty, tw, th, mem, pitch);
  327. }
  328. }
  329. bgfx::dbgTextPrintf(0, 4, 0x0f, "m_hit: %d, m_miss %d", m_hit, m_miss);
  330. float at[3] = { 0.0f, 0.0f, 0.0f };
  331. float eye[3] = { 0.0f, 0.0f, -5.0f };
  332. float view[16];
  333. float proj[16];
  334. bx::mtxLookAt(view, eye, at);
  335. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  336. // Set view and projection matrix for view 0.
  337. bgfx::setViewTransform(0, view, proj);
  338. for (uint32_t ii = 0; ii < 1 + uint32_t(m_blitSupported); ++ii)
  339. {
  340. float mtx[16];
  341. bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, time, time*0.37f, 0.0f, -1.5f*m_blitSupported + ii*3.0f, 0.0f, 0.0f);
  342. // Set model matrix for rendering.
  343. bgfx::setTransform(mtx);
  344. // Set vertex and index buffer.
  345. bgfx::setVertexBuffer(m_vbh);
  346. bgfx::setIndexBuffer(m_ibh);
  347. // Bind texture.
  348. bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
  349. // Set render states.
  350. bgfx::setState(BGFX_STATE_DEFAULT);
  351. // Submit primitive for rendering to view 0.
  352. bgfx::submit(0, m_program);
  353. }
  354. // Set view and projection matrix for view 1.
  355. const float aspectRatio = float(m_height)/float(m_width);
  356. const float size = 11.0f;
  357. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  358. bgfx::setViewTransform(1, NULL, proj);
  359. float mtx[16];
  360. bx::mtxTranslate(mtx, -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f, 1.9f, 0.0f);
  361. // Set model matrix for rendering.
  362. bgfx::setTransform(mtx);
  363. // Set vertex and index buffer.
  364. bgfx::setVertexBuffer(m_vbh);
  365. bgfx::setIndexBuffer(m_ibh);
  366. // Bind texture.
  367. bgfx::setTexture(0, s_texColor, m_texture2d);
  368. // Set render states.
  369. bgfx::setState(BGFX_STATE_DEFAULT);
  370. // Submit primitive for rendering to view 1.
  371. bgfx::submit(1, m_programCmp);
  372. const float xpos = -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f;
  373. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  374. {
  375. bx::mtxTranslate(mtx, xpos + ii*2.1f, size-6.5f, 0.0f);
  376. // Set model matrix for rendering.
  377. bgfx::setTransform(mtx);
  378. // Set vertex and index buffer.
  379. bgfx::setVertexBuffer(m_vbh);
  380. bgfx::setIndexBuffer(m_ibh, 0, 6);
  381. // Bind texture.
  382. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  383. // Set render states.
  384. bgfx::setState(BGFX_STATE_DEFAULT);
  385. // Submit primitive for rendering to view 1.
  386. bgfx::submit(1, m_programCmp);
  387. }
  388. for (uint32_t ii = 0; ii < m_numm_textures3d; ++ii)
  389. {
  390. bx::mtxTranslate(mtx, xpos + ii*2.1f, -size+6.5f, 0.0f);
  391. // Set model matrix for rendering.
  392. bgfx::setTransform(mtx);
  393. // Set vertex and index buffer.
  394. bgfx::setVertexBuffer(m_vbh);
  395. bgfx::setIndexBuffer(m_ibh, 0, 6);
  396. // Bind texture.
  397. bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
  398. // Set render states.
  399. bgfx::setState(BGFX_STATE_DEFAULT);
  400. // Submit primitive for rendering to view 1.
  401. bgfx::submit(1, m_program3d);
  402. }
  403. for (uint32_t ii = 0; ii < 4; ++ii)
  404. {
  405. bx::mtxTranslate(mtx, xpos + (size-2.0f)*2.1f, -size+6.5f + ii*2.1f, 0.0f);
  406. // Set model matrix for rendering.
  407. bgfx::setTransform(mtx);
  408. // Set vertex and index buffer.
  409. bgfx::setVertexBuffer(m_vbh, 24, 4);
  410. bgfx::setIndexBuffer(m_ibh, 0, 6);
  411. // Bind texture.
  412. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  413. // Set render states.
  414. bgfx::setState(BGFX_STATE_DEFAULT);
  415. // Submit primitive for rendering to view 1.
  416. bgfx::submit(1, m_programCmp);
  417. }
  418. // Advance to next frame. Rendering thread will be kicked to
  419. // process submitted rendering primitives.
  420. bgfx::frame();
  421. return true;
  422. }
  423. return false;
  424. }
  425. uint8_t* m_texture2dData;
  426. uint32_t m_width;
  427. uint32_t m_height;
  428. uint32_t m_debug;
  429. uint32_t m_reset;
  430. uint32_t m_numm_textures3d;
  431. bool m_texture3DSupported;
  432. bool m_blitSupported;
  433. std::list<PackCube> m_quads;
  434. RectPackCubeT<256> m_cube;
  435. int64_t m_updateTime;
  436. int64_t m_timeOffset;
  437. uint32_t m_hit;
  438. uint32_t m_miss;
  439. uint8_t m_rr;
  440. uint8_t m_gg;
  441. uint8_t m_bb;
  442. bgfx::TextureHandle m_textures[9];
  443. bgfx::TextureHandle m_textures3d[3];
  444. bgfx::TextureHandle m_texture2d;
  445. bgfx::TextureHandle m_textureCube[2];
  446. bgfx::IndexBufferHandle m_ibh;
  447. bgfx::VertexBufferHandle m_vbh;
  448. bgfx::ProgramHandle m_program3d;
  449. bgfx::ProgramHandle m_programCmp;
  450. bgfx::ProgramHandle m_program;
  451. bgfx::UniformHandle u_time;
  452. bgfx::UniformHandle s_texColor;
  453. bgfx::UniformHandle s_texCube;
  454. };
  455. ENTRY_IMPLEMENT_MAIN(ExampleUpdate);