update.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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, false, false, 1, 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, 0, _side, 0, _x, _y, _width, _height, mem);
  90. }
  91. static const uint16_t textureside = 512;
  92. static const uint32_t texture2dSize = 256;
  93. class ExampleUpdate : public entry::AppI
  94. {
  95. public:
  96. ExampleUpdate()
  97. : m_cube(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("textures/texture_compression_bc1.ktx", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
  121. m_textures[1] = loadTexture("textures/texture_compression_bc2.ktx", BGFX_TEXTURE_U_CLAMP);
  122. m_textures[2] = loadTexture("textures/texture_compression_bc3.ktx", BGFX_TEXTURE_V_CLAMP);
  123. m_textures[3] = loadTexture("textures/texture_compression_etc1.ktx", BGFX_TEXTURE_U_BORDER|BGFX_TEXTURE_V_BORDER|BGFX_TEXTURE_BORDER_COLOR(1) );
  124. m_textures[4] = loadTexture("textures/texture_compression_etc2.ktx");
  125. m_textures[5] = loadTexture("textures/texture_compression_ptc12.pvr");
  126. m_textures[6] = loadTexture("textures/texture_compression_ptc14.pvr");
  127. m_textures[7] = loadTexture("textures/texture_compression_ptc22.pvr");
  128. m_textures[8] = loadTexture("textures/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, false, 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, false, 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, false, 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(
  183. textureside
  184. , false
  185. , 1
  186. , bgfx::TextureFormat::BGRA8
  187. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  188. );
  189. if (m_blitSupported)
  190. {
  191. m_textureCube[1] = bgfx::createTextureCube(
  192. textureside
  193. , false
  194. , 1
  195. , bgfx::TextureFormat::BGRA8
  196. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT|BGFX_TEXTURE_BLIT_DST
  197. );
  198. }
  199. m_texture2d = bgfx::createTexture2D(
  200. texture2dSize
  201. , texture2dSize
  202. , false
  203. , 1
  204. , bgfx::TextureFormat::BGRA8
  205. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  206. );
  207. m_texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4);
  208. m_rr = rand()%255;
  209. m_gg = rand()%255;
  210. m_bb = rand()%255;
  211. m_hit = 0;
  212. m_miss = 0;
  213. m_updateTime = 0;
  214. m_timeOffset = bx::getHPCounter();
  215. }
  216. virtual int shutdown() BX_OVERRIDE
  217. {
  218. // m_texture2dData is managed from main thread, and it's passed to renderer
  219. // just as MemoryRef. At this point render might be using it. We must wait
  220. // previous frame to finish before we can free it.
  221. bgfx::frame();
  222. // Cleanup.
  223. free(m_texture2dData);
  224. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  225. {
  226. bgfx::destroyTexture(m_textures[ii]);
  227. }
  228. for (uint32_t ii = 0; ii < m_numm_textures3d; ++ii)
  229. {
  230. bgfx::destroyTexture(m_textures3d[ii]);
  231. }
  232. bgfx::destroyTexture(m_texture2d);
  233. bgfx::destroyTexture(m_textureCube[0]);
  234. if (m_blitSupported)
  235. {
  236. bgfx::destroyTexture(m_textureCube[1]);
  237. }
  238. bgfx::destroyIndexBuffer(m_ibh);
  239. bgfx::destroyVertexBuffer(m_vbh);
  240. if (bgfx::isValid(m_program3d) )
  241. {
  242. bgfx::destroyProgram(m_program3d);
  243. }
  244. bgfx::destroyProgram(m_programCmp);
  245. bgfx::destroyProgram(m_program);
  246. bgfx::destroyUniform(u_time);
  247. bgfx::destroyUniform(s_texColor);
  248. bgfx::destroyUniform(s_texCube);
  249. // Shutdown bgfx.
  250. bgfx::shutdown();
  251. return 0;
  252. }
  253. bool update() BX_OVERRIDE
  254. {
  255. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  256. {
  257. float borderColor[4] = { float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f };
  258. bgfx::setPaletteColor(1, borderColor);
  259. // Set view 0 and 1 viewport.
  260. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  261. bgfx::setViewRect(1, 0, 0, m_width, m_height);
  262. // This dummy draw call is here to make sure that view 0 is cleared
  263. // if no other draw calls are submitted to view 0.
  264. bgfx::touch(0);
  265. int64_t now = bx::getHPCounter();
  266. static int64_t last = now;
  267. const int64_t frameTime = now - last;
  268. last = now;
  269. const int64_t freq = bx::getHPFrequency();
  270. const double toMs = 1000.0/double(freq);
  271. float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
  272. bgfx::setUniform(u_time, &time);
  273. // Use debug font to print information about this example.
  274. bgfx::dbgTextClear();
  275. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  276. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating m_textures.");
  277. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  278. if (now > m_updateTime)
  279. {
  280. PackCube face;
  281. uint32_t bw = bx::uint16_max(1, rand()%(textureside/4) );
  282. uint32_t bh = bx::uint16_max(1, rand()%(textureside/4) );
  283. if (m_cube.find(bw, bh, face) )
  284. {
  285. m_quads.push_back(face);
  286. ++m_hit;
  287. const Pack2D& rect = face.m_rect;
  288. 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);
  289. if (m_blitSupported)
  290. {
  291. 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);
  292. }
  293. m_rr = rand()%255;
  294. m_gg = rand()%255;
  295. m_bb = rand()%255;
  296. }
  297. else
  298. {
  299. ++m_miss;
  300. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
  301. {
  302. face = m_quads.front();
  303. const Pack2D& rect = face.m_rect;
  304. updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
  305. if (m_blitSupported)
  306. {
  307. 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);
  308. }
  309. m_cube.clear(face);
  310. m_quads.pop_front();
  311. }
  312. }
  313. {
  314. // Fill rect.
  315. const uint32_t pitch = texture2dSize*4;
  316. const uint16_t tw = rand()% texture2dSize;
  317. const uint16_t th = rand()% texture2dSize;
  318. const uint16_t tx = rand()%(texture2dSize-tw);
  319. const uint16_t ty = rand()%(texture2dSize-th);
  320. uint8_t* dst = &m_texture2dData[(ty*texture2dSize+tx)*4];
  321. uint8_t* next = dst + pitch;
  322. // Using makeRef to pass texture memory without copying.
  323. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  324. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  325. {
  326. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  327. {
  328. dst[0] = m_bb;
  329. dst[1] = m_gg;
  330. dst[2] = m_rr;
  331. dst[3] = 255;
  332. }
  333. }
  334. // Pitch here makes possible to pass data from source to destination
  335. // without need for m_textures and allocated memory to be the same size.
  336. bgfx::updateTexture2D(m_texture2d, 0, 0, tx, ty, tw, th, mem, pitch);
  337. }
  338. }
  339. bgfx::dbgTextPrintf(0, 4, 0x0f, "m_hit: %d, m_miss %d", m_hit, m_miss);
  340. float at[3] = { 0.0f, 0.0f, 0.0f };
  341. float eye[3] = { 0.0f, 0.0f, -5.0f };
  342. float view[16];
  343. float proj[16];
  344. bx::mtxLookAt(view, eye, at);
  345. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  346. // Set view and projection matrix for view 0.
  347. bgfx::setViewTransform(0, view, proj);
  348. for (uint32_t ii = 0; ii < 1 + uint32_t(m_blitSupported); ++ii)
  349. {
  350. float mtx[16];
  351. 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);
  352. // Set model matrix for rendering.
  353. bgfx::setTransform(mtx);
  354. // Set vertex and index buffer.
  355. bgfx::setVertexBuffer(m_vbh);
  356. bgfx::setIndexBuffer(m_ibh);
  357. // Bind texture.
  358. bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
  359. // Set render states.
  360. bgfx::setState(BGFX_STATE_DEFAULT);
  361. // Submit primitive for rendering to view 0.
  362. bgfx::submit(0, m_program);
  363. }
  364. // Set view and projection matrix for view 1.
  365. const float aspectRatio = float(m_height)/float(m_width);
  366. const float size = 11.0f;
  367. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  368. bgfx::setViewTransform(1, NULL, proj);
  369. float mtx[16];
  370. bx::mtxTranslate(mtx, -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f, 1.9f, 0.0f);
  371. // Set model matrix for rendering.
  372. bgfx::setTransform(mtx);
  373. // Set vertex and index buffer.
  374. bgfx::setVertexBuffer(m_vbh);
  375. bgfx::setIndexBuffer(m_ibh);
  376. // Bind texture.
  377. bgfx::setTexture(0, s_texColor, m_texture2d);
  378. // Set render states.
  379. bgfx::setState(BGFX_STATE_DEFAULT);
  380. // Submit primitive for rendering to view 1.
  381. bgfx::submit(1, m_programCmp);
  382. const float xpos = -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f;
  383. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  384. {
  385. bx::mtxTranslate(mtx, xpos + ii*2.1f, size-6.5f, 0.0f);
  386. // Set model matrix for rendering.
  387. bgfx::setTransform(mtx);
  388. // Set vertex and index buffer.
  389. bgfx::setVertexBuffer(m_vbh);
  390. bgfx::setIndexBuffer(m_ibh, 0, 6);
  391. // Bind texture.
  392. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  393. // Set render states.
  394. bgfx::setState(BGFX_STATE_DEFAULT);
  395. // Submit primitive for rendering to view 1.
  396. bgfx::submit(1, m_programCmp);
  397. }
  398. for (uint32_t ii = 0; ii < m_numm_textures3d; ++ii)
  399. {
  400. bx::mtxTranslate(mtx, xpos + ii*2.1f, -size+6.5f, 0.0f);
  401. // Set model matrix for rendering.
  402. bgfx::setTransform(mtx);
  403. // Set vertex and index buffer.
  404. bgfx::setVertexBuffer(m_vbh);
  405. bgfx::setIndexBuffer(m_ibh, 0, 6);
  406. // Bind texture.
  407. bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
  408. // Set render states.
  409. bgfx::setState(BGFX_STATE_DEFAULT);
  410. // Submit primitive for rendering to view 1.
  411. bgfx::submit(1, m_program3d);
  412. }
  413. for (uint32_t ii = 0; ii < 4; ++ii)
  414. {
  415. bx::mtxTranslate(mtx, xpos + (size-2.0f)*2.1f, -size+6.5f + ii*2.1f, 0.0f);
  416. // Set model matrix for rendering.
  417. bgfx::setTransform(mtx);
  418. // Set vertex and index buffer.
  419. bgfx::setVertexBuffer(m_vbh, 24, 4);
  420. bgfx::setIndexBuffer(m_ibh, 0, 6);
  421. // Bind texture.
  422. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  423. // Set render states.
  424. bgfx::setState(BGFX_STATE_DEFAULT);
  425. // Submit primitive for rendering to view 1.
  426. bgfx::submit(1, m_programCmp);
  427. }
  428. // Advance to next frame. Rendering thread will be kicked to
  429. // process submitted rendering primitives.
  430. bgfx::frame();
  431. return true;
  432. }
  433. return false;
  434. }
  435. uint8_t* m_texture2dData;
  436. uint32_t m_width;
  437. uint32_t m_height;
  438. uint32_t m_debug;
  439. uint32_t m_reset;
  440. uint32_t m_numm_textures3d;
  441. bool m_texture3DSupported;
  442. bool m_blitSupported;
  443. std::list<PackCube> m_quads;
  444. RectPackCubeT<256> m_cube;
  445. int64_t m_updateTime;
  446. int64_t m_timeOffset;
  447. uint32_t m_hit;
  448. uint32_t m_miss;
  449. uint8_t m_rr;
  450. uint8_t m_gg;
  451. uint8_t m_bb;
  452. bgfx::TextureHandle m_textures[9];
  453. bgfx::TextureHandle m_textures3d[3];
  454. bgfx::TextureHandle m_texture2d;
  455. bgfx::TextureHandle m_textureCube[2];
  456. bgfx::IndexBufferHandle m_ibh;
  457. bgfx::VertexBufferHandle m_vbh;
  458. bgfx::ProgramHandle m_program3d;
  459. bgfx::ProgramHandle m_programCmp;
  460. bgfx::ProgramHandle m_program;
  461. bgfx::UniformHandle u_time;
  462. bgfx::UniformHandle s_texColor;
  463. bgfx::UniformHandle s_texCube;
  464. };
  465. ENTRY_IMPLEMENT_MAIN(ExampleUpdate);