update.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * Copyright 2011-2017 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_computeSupported = !!(caps->supported & BGFX_CAPS_COMPUTE);
  133. m_numTextures3d = 0;
  134. if (m_texture3DSupported)
  135. {
  136. const bgfx::Memory* mem8 = bgfx::alloc(32*32*32);
  137. const bgfx::Memory* mem16f = bgfx::alloc(32*32*32*2);
  138. const bgfx::Memory* mem32f = bgfx::alloc(32*32*32*4);
  139. for (uint8_t zz = 0; zz < 32; ++zz)
  140. {
  141. for (uint8_t yy = 0; yy < 32; ++yy)
  142. {
  143. for (uint8_t xx = 0; xx < 32; ++xx)
  144. {
  145. const uint32_t offset = ( (zz*32+yy)*32+xx);
  146. const uint32_t val = xx ^ yy ^ zz;
  147. mem8->data[offset] = val<<3;
  148. *(uint16_t*)&mem16f->data[offset*2] = bx::halfFromFloat( (float)val/32.0f);
  149. *(float*)&mem32f->data[offset*4] = (float)val/32.0f;
  150. }
  151. }
  152. }
  153. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R8]) )
  154. {
  155. m_textures3d[m_numTextures3d++] = bgfx::createTexture3D(32, 32, 32, false, bgfx::TextureFormat::R8, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem8);
  156. }
  157. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R16F]) )
  158. {
  159. m_textures3d[m_numTextures3d++] = bgfx::createTexture3D(32, 32, 32, false, bgfx::TextureFormat::R16F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem16f);
  160. }
  161. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_2D & caps->formats[bgfx::TextureFormat::R32F]) )
  162. {
  163. m_textures3d[m_numTextures3d++] = bgfx::createTexture3D(32, 32, 32, false, bgfx::TextureFormat::R32F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem32f);
  164. }
  165. }
  166. // Create static vertex buffer.
  167. m_vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_m_cubeVertices, sizeof(s_m_cubeVertices) ), PosTexcoordVertex::ms_decl);
  168. // Create static index buffer.
  169. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_m_cubeIndices, sizeof(s_m_cubeIndices) ) );
  170. // Create programs.
  171. m_program = loadProgram("vs_update", "fs_update");
  172. m_programCmp = loadProgram("vs_update", "fs_update_cmp");
  173. m_program3d.idx = bgfx::invalidHandle;
  174. if (m_texture3DSupported)
  175. {
  176. m_program3d = loadProgram("vs_update", "fs_update_3d");
  177. }
  178. m_programCompute.idx = bgfx::invalidHandle;
  179. if (m_computeSupported)
  180. {
  181. m_programCompute = bgfx::createProgram( loadShader( "cs_update" ), true );
  182. }
  183. // Create texture sampler uniforms.
  184. s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  185. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  186. // Create time uniform.
  187. u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
  188. for(uint32_t ii = 0; ii<BX_COUNTOF( m_textureCube ); ++ii)
  189. {
  190. m_textureCube[ii].idx = bgfx::invalidHandle;
  191. }
  192. m_textureCube[0] = bgfx::createTextureCube(
  193. textureside
  194. , false
  195. , 1
  196. , bgfx::TextureFormat::BGRA8
  197. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  198. );
  199. if (m_blitSupported)
  200. {
  201. m_textureCube[1] = bgfx::createTextureCube(
  202. textureside
  203. , false
  204. , 1
  205. , bgfx::TextureFormat::BGRA8
  206. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT|BGFX_TEXTURE_BLIT_DST
  207. );
  208. }
  209. if (m_computeSupported)
  210. {
  211. m_textureCube[2] = bgfx::createTextureCube(
  212. textureside
  213. , false
  214. , 1
  215. , bgfx::TextureFormat::RGBA8
  216. , BGFX_TEXTURE_COMPUTE_WRITE
  217. );
  218. }
  219. m_texture2d = bgfx::createTexture2D(
  220. texture2dSize
  221. , texture2dSize
  222. , false
  223. , 1
  224. , bgfx::TextureFormat::BGRA8
  225. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  226. );
  227. m_texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4);
  228. m_rr = rand()%255;
  229. m_gg = rand()%255;
  230. m_bb = rand()%255;
  231. m_hit = 0;
  232. m_miss = 0;
  233. m_updateTime = 0;
  234. m_timeOffset = bx::getHPCounter();
  235. }
  236. virtual int shutdown() BX_OVERRIDE
  237. {
  238. // m_texture2dData is managed from main thread, and it's passed to renderer
  239. // just as MemoryRef. At this point render might be using it. We must wait
  240. // previous frame to finish before we can free it.
  241. bgfx::frame();
  242. // Cleanup.
  243. free(m_texture2dData);
  244. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  245. {
  246. bgfx::destroyTexture(m_textures[ii]);
  247. }
  248. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  249. {
  250. bgfx::destroyTexture(m_textures3d[ii]);
  251. }
  252. bgfx::destroyTexture(m_texture2d);
  253. for (uint32_t ii = 0; ii<BX_COUNTOF(m_textureCube); ++ii)
  254. {
  255. if (bgfx::isValid(m_textureCube[ii]))
  256. {
  257. bgfx::destroyTexture(m_textureCube[ii]);
  258. }
  259. }
  260. bgfx::destroyIndexBuffer(m_ibh);
  261. bgfx::destroyVertexBuffer(m_vbh);
  262. if (bgfx::isValid(m_program3d) )
  263. {
  264. bgfx::destroyProgram(m_program3d);
  265. }
  266. bgfx::destroyProgram(m_programCmp);
  267. if (bgfx::isValid(m_programCompute) )
  268. {
  269. bgfx::destroyProgram(m_programCompute);
  270. }
  271. bgfx::destroyProgram(m_program);
  272. bgfx::destroyUniform(u_time);
  273. bgfx::destroyUniform(s_texColor);
  274. bgfx::destroyUniform(s_texCube);
  275. // Shutdown bgfx.
  276. bgfx::shutdown();
  277. return 0;
  278. }
  279. bool update() BX_OVERRIDE
  280. {
  281. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  282. {
  283. float borderColor[4] = { float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f };
  284. bgfx::setPaletteColor(1, borderColor);
  285. // Set view 0 and 1 viewport.
  286. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  287. bgfx::setViewRect(1, 0, 0, m_width, m_height);
  288. // This dummy draw call is here to make sure that view 0 is cleared
  289. // if no other draw calls are submitted to view 0.
  290. bgfx::touch(0);
  291. int64_t now = bx::getHPCounter();
  292. static int64_t last = now;
  293. const int64_t frameTime = now - last;
  294. last = now;
  295. const int64_t freq = bx::getHPFrequency();
  296. const double toMs = 1000.0/double(freq);
  297. float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
  298. bgfx::setUniform(u_time, &time);
  299. // Use debug font to print information about this example.
  300. bgfx::dbgTextClear();
  301. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  302. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating m_textures.");
  303. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  304. if (now > m_updateTime)
  305. {
  306. PackCube face;
  307. uint32_t bw = bx::uint16_max(1, rand()%(textureside/4) );
  308. uint32_t bh = bx::uint16_max(1, rand()%(textureside/4) );
  309. if (m_cube.find(bw, bh, face) )
  310. {
  311. m_quads.push_back(face);
  312. ++m_hit;
  313. const Pack2D& rect = face.m_rect;
  314. 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);
  315. if (m_blitSupported)
  316. {
  317. 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);
  318. }
  319. m_rr = rand()%255;
  320. m_gg = rand()%255;
  321. m_bb = rand()%255;
  322. }
  323. else
  324. {
  325. ++m_miss;
  326. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
  327. {
  328. face = m_quads.front();
  329. const Pack2D& rect = face.m_rect;
  330. updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
  331. if (m_blitSupported)
  332. {
  333. 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);
  334. }
  335. m_cube.clear(face);
  336. m_quads.pop_front();
  337. }
  338. }
  339. {
  340. // Fill rect.
  341. const uint32_t pitch = texture2dSize*4;
  342. const uint16_t tw = rand()% texture2dSize;
  343. const uint16_t th = rand()% texture2dSize;
  344. const uint16_t tx = rand()%(texture2dSize-tw);
  345. const uint16_t ty = rand()%(texture2dSize-th);
  346. uint8_t* dst = &m_texture2dData[(ty*texture2dSize+tx)*4];
  347. uint8_t* next = dst + pitch;
  348. // Using makeRef to pass texture memory without copying.
  349. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  350. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  351. {
  352. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  353. {
  354. dst[0] = m_bb;
  355. dst[1] = m_gg;
  356. dst[2] = m_rr;
  357. dst[3] = 255;
  358. }
  359. }
  360. // Pitch here makes possible to pass data from source to destination
  361. // without need for m_textures and allocated memory to be the same size.
  362. bgfx::updateTexture2D(m_texture2d, 0, 0, tx, ty, tw, th, mem, pitch);
  363. }
  364. }
  365. bgfx::dbgTextPrintf(0, 4, 0x0f, "m_hit: %d, m_miss %d", m_hit, m_miss);
  366. float at[3] = { 0.0f, 0.0f, 0.0f };
  367. float eye[3] = { 0.0f, 0.0f, -5.0f };
  368. float view[16];
  369. float proj[16];
  370. bx::mtxLookAt(view, eye, at);
  371. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  372. // Set view and projection matrix for view 0.
  373. bgfx::setViewTransform(0, view, proj);
  374. // Update texturecube using compute shader
  375. if (bgfx::isValid(m_programCompute) )
  376. {
  377. bgfx::setImage(0, s_texCube, m_textureCube[2], 0, bgfx::Access::Write);
  378. bgfx::dispatch(0, m_programCompute, textureside/16, textureside/16);
  379. }
  380. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
  381. {
  382. if (bgfx::isValid(m_textureCube[ii]))
  383. {
  384. float mtx[16];
  385. bx::mtxSRT( mtx, 0.7f, 0.7f, 0.7f, time, time*0.37f, 0.0f, -2.0f +ii*2.0f, 0.0f, 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 );
  391. // Bind texture.
  392. bgfx::setTexture( 0, s_texCube, m_textureCube[ii] );
  393. // Set render states.
  394. bgfx::setState( BGFX_STATE_DEFAULT );
  395. // Submit primitive for rendering to view 0.
  396. bgfx::submit( 0, m_program );
  397. }
  398. }
  399. // Set view and projection matrix for view 1.
  400. const float aspectRatio = float(m_height)/float(m_width);
  401. const float size = 11.0f;
  402. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  403. bgfx::setViewTransform(1, NULL, proj);
  404. float mtx[16];
  405. bx::mtxTranslate(mtx, -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f, 1.9f, 0.0f);
  406. // Set model matrix for rendering.
  407. bgfx::setTransform(mtx);
  408. // Set vertex and index buffer.
  409. bgfx::setVertexBuffer(m_vbh);
  410. bgfx::setIndexBuffer(m_ibh);
  411. // Bind texture.
  412. bgfx::setTexture(0, s_texColor, m_texture2d);
  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. const float xpos = -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f;
  418. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  419. {
  420. bx::mtxTranslate(mtx, xpos + ii*2.1f, size-6.5f, 0.0f);
  421. // Set model matrix for rendering.
  422. bgfx::setTransform(mtx);
  423. // Set vertex and index buffer.
  424. bgfx::setVertexBuffer(m_vbh);
  425. bgfx::setIndexBuffer(m_ibh, 0, 6);
  426. // Bind texture.
  427. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  428. // Set render states.
  429. bgfx::setState(BGFX_STATE_DEFAULT);
  430. // Submit primitive for rendering to view 1.
  431. bgfx::submit(1, m_programCmp);
  432. }
  433. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  434. {
  435. bx::mtxTranslate(mtx, xpos + ii*2.1f, -size+6.5f, 0.0f);
  436. // Set model matrix for rendering.
  437. bgfx::setTransform(mtx);
  438. // Set vertex and index buffer.
  439. bgfx::setVertexBuffer(m_vbh);
  440. bgfx::setIndexBuffer(m_ibh, 0, 6);
  441. // Bind texture.
  442. bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
  443. // Set render states.
  444. bgfx::setState(BGFX_STATE_DEFAULT);
  445. // Submit primitive for rendering to view 1.
  446. bgfx::submit(1, m_program3d);
  447. }
  448. for (uint32_t ii = 0; ii < 4; ++ii)
  449. {
  450. bx::mtxTranslate(mtx, xpos + (size-2.0f)*2.1f, -size+6.5f + ii*2.1f, 0.0f);
  451. // Set model matrix for rendering.
  452. bgfx::setTransform(mtx);
  453. // Set vertex and index buffer.
  454. bgfx::setVertexBuffer(m_vbh, 24, 4);
  455. bgfx::setIndexBuffer(m_ibh, 0, 6);
  456. // Bind texture.
  457. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  458. // Set render states.
  459. bgfx::setState(BGFX_STATE_DEFAULT);
  460. // Submit primitive for rendering to view 1.
  461. bgfx::submit(1, m_programCmp);
  462. }
  463. // Advance to next frame. Rendering thread will be kicked to
  464. // process submitted rendering primitives.
  465. bgfx::frame();
  466. return true;
  467. }
  468. return false;
  469. }
  470. uint8_t* m_texture2dData;
  471. uint32_t m_width;
  472. uint32_t m_height;
  473. uint32_t m_debug;
  474. uint32_t m_reset;
  475. uint32_t m_numTextures3d;
  476. bool m_texture3DSupported;
  477. bool m_blitSupported;
  478. bool m_computeSupported;
  479. std::list<PackCube> m_quads;
  480. RectPackCubeT<256> m_cube;
  481. int64_t m_updateTime;
  482. int64_t m_timeOffset;
  483. uint32_t m_hit;
  484. uint32_t m_miss;
  485. uint8_t m_rr;
  486. uint8_t m_gg;
  487. uint8_t m_bb;
  488. bgfx::TextureHandle m_textures[9];
  489. bgfx::TextureHandle m_textures3d[3];
  490. bgfx::TextureHandle m_texture2d;
  491. bgfx::TextureHandle m_textureCube[3];
  492. bgfx::IndexBufferHandle m_ibh;
  493. bgfx::VertexBufferHandle m_vbh;
  494. bgfx::ProgramHandle m_program3d;
  495. bgfx::ProgramHandle m_programCmp;
  496. bgfx::ProgramHandle m_programCompute;
  497. bgfx::ProgramHandle m_program;
  498. bgfx::UniformHandle u_time;
  499. bgfx::UniformHandle s_texColor;
  500. bgfx::UniformHandle s_texCube;
  501. };
  502. ENTRY_IMPLEMENT_MAIN(ExampleUpdate);