update.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * Copyright 2011-2018 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 "packrect.h"
  8. #include "imgui/imgui.h"
  9. #include <bx/uint32_t.h>
  10. #include <list>
  11. namespace
  12. {
  13. struct PosTexcoordVertex
  14. {
  15. float m_x;
  16. float m_y;
  17. float m_z;
  18. float m_u;
  19. float m_v;
  20. float m_w;
  21. static void init()
  22. {
  23. ms_decl
  24. .begin()
  25. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  26. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  27. .end();
  28. };
  29. static bgfx::VertexDecl ms_decl;
  30. };
  31. bgfx::VertexDecl PosTexcoordVertex::ms_decl;
  32. static PosTexcoordVertex s_cubeVertices[] =
  33. {
  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, -1.0f, -1.0f, -1.0f },
  56. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  57. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  58. {-1.0f, 1.0f, 1.0f, -2.0f, 2.0f, 2.0f },
  59. { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f },
  60. {-1.0f, -1.0f, 1.0f, -2.0f, -2.0f, 2.0f },
  61. { 1.0f, -1.0f, 1.0f, 2.0f, -2.0f, 2.0f },
  62. };
  63. BX_STATIC_ASSERT(BX_COUNTOF(s_cubeVertices) == 28);
  64. static const uint16_t s_cubeIndices[] =
  65. {
  66. 0, 1, 2, // 0
  67. 1, 3, 2,
  68. 4, 6, 5, // 2
  69. 5, 6, 7,
  70. 8, 10, 9, // 4
  71. 9, 10, 11,
  72. 12, 14, 13, // 6
  73. 14, 15, 13,
  74. 16, 18, 17, // 8
  75. 18, 19, 17,
  76. 20, 22, 21, // 10
  77. 21, 22, 23,
  78. };
  79. BX_STATIC_ASSERT(BX_COUNTOF(s_cubeIndices) == 36);
  80. static void updateTextureCubeRectBgra8(
  81. bgfx::TextureHandle _handle
  82. , uint8_t _side
  83. , uint16_t _x
  84. , uint16_t _y
  85. , uint16_t _width
  86. , uint16_t _height
  87. , uint8_t _r
  88. , uint8_t _g
  89. , uint8_t _b
  90. , uint8_t _a = 0xff
  91. )
  92. {
  93. bgfx::TextureInfo ti;
  94. bgfx::calcTextureSize(ti, _width, _height, 1, false, false, 1, bgfx::TextureFormat::BGRA8);
  95. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  96. uint8_t* data = (uint8_t*)mem->data;
  97. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  98. {
  99. data[0] = _b;
  100. data[1] = _g;
  101. data[2] = _r;
  102. data[3] = _a;
  103. data += 4;
  104. }
  105. bgfx::updateTextureCube(_handle, 0, _side, 0, _x, _y, _width, _height, mem);
  106. }
  107. static const uint16_t kTextureSide = 512;
  108. static const uint32_t kTexture2dSize = 256;
  109. class ExampleUpdate : public entry::AppI
  110. {
  111. public:
  112. ExampleUpdate(const char* _name, const char* _description)
  113. : entry::AppI(_name, _description)
  114. , m_cube(kTextureSide)
  115. {
  116. }
  117. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  118. {
  119. Args args(_argc, _argv);
  120. m_width = _width;
  121. m_height = _height;
  122. m_debug = BGFX_DEBUG_NONE;
  123. m_reset = BGFX_RESET_VSYNC;
  124. bgfx::Init init;
  125. init.type = args.m_type;
  126. init.vendorId = args.m_pciId;
  127. init.resolution.width = m_width;
  128. init.resolution.height = m_height;
  129. init.resolution.reset = m_reset;
  130. bgfx::init(init);
  131. // Enable debug text.
  132. bgfx::setDebug(m_debug);
  133. // Set view 0 clear state.
  134. bgfx::setViewClear(0
  135. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  136. , 0x303030ff
  137. , 1.0f
  138. , 0
  139. );
  140. // Create vertex stream declaration.
  141. PosTexcoordVertex::init();
  142. m_textures[0] = loadTexture("textures/texture_compression_bc1.ktx", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP);
  143. m_textures[1] = loadTexture("textures/texture_compression_bc2.ktx", BGFX_TEXTURE_U_CLAMP);
  144. m_textures[2] = loadTexture("textures/texture_compression_bc3.ktx", BGFX_TEXTURE_V_CLAMP);
  145. m_textures[3] = loadTexture("textures/texture_compression_etc1.ktx", BGFX_TEXTURE_U_BORDER|BGFX_TEXTURE_V_BORDER|BGFX_TEXTURE_BORDER_COLOR(1) );
  146. m_textures[4] = loadTexture("textures/texture_compression_etc2.ktx");
  147. m_textures[5] = loadTexture("textures/texture_compression_ptc12.pvr");
  148. m_textures[6] = loadTexture("textures/texture_compression_ptc14.pvr");
  149. m_textures[7] = loadTexture("textures/texture_compression_ptc22.pvr");
  150. m_textures[8] = loadTexture("textures/texture_compression_ptc24.pvr");
  151. const bgfx::Caps* caps = bgfx::getCaps();
  152. m_texture3DSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_3D);
  153. m_blitSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_BLIT);
  154. m_computeSupported = !!(caps->supported & BGFX_CAPS_COMPUTE);
  155. m_numTextures3d = 0;
  156. if (m_texture3DSupported)
  157. {
  158. const bgfx::Memory* mem8 = bgfx::alloc(32*32*32);
  159. const bgfx::Memory* mem16f = bgfx::alloc(32*32*32*2);
  160. const bgfx::Memory* mem32f = bgfx::alloc(32*32*32*4);
  161. for (uint8_t zz = 0; zz < 32; ++zz)
  162. {
  163. for (uint8_t yy = 0; yy < 32; ++yy)
  164. {
  165. for (uint8_t xx = 0; xx < 32; ++xx)
  166. {
  167. const uint32_t offset = ( (zz*32+yy)*32+xx);
  168. const uint32_t val = xx ^ yy ^ zz;
  169. mem8->data[offset] = uint8_t(val<<3);
  170. *(uint16_t*)&mem16f->data[offset*2] = bx::halfFromFloat( (float)val/32.0f);
  171. *(float*)&mem32f->data[offset*4] = (float)val/32.0f;
  172. }
  173. }
  174. }
  175. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R8]) )
  176. {
  177. 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);
  178. }
  179. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R16F]) )
  180. {
  181. 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);
  182. }
  183. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R32F]) )
  184. {
  185. 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);
  186. }
  187. }
  188. // Create static vertex buffer.
  189. m_vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ), PosTexcoordVertex::ms_decl);
  190. // Create static index buffer.
  191. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  192. // Create programs.
  193. m_program = loadProgram("vs_update", "fs_update");
  194. m_programCmp = loadProgram("vs_update", "fs_update_cmp");
  195. m_program3d.idx = bgfx::kInvalidHandle;
  196. if (m_texture3DSupported)
  197. {
  198. m_program3d = loadProgram("vs_update", "fs_update_3d");
  199. }
  200. m_programCompute.idx = bgfx::kInvalidHandle;
  201. if (m_computeSupported)
  202. {
  203. m_programCompute = bgfx::createProgram( loadShader( "cs_update" ), true );
  204. }
  205. // Create texture sampler uniforms.
  206. s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  207. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  208. // Create time uniform.
  209. u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
  210. for(uint32_t ii = 0; ii<BX_COUNTOF( m_textureCube ); ++ii)
  211. {
  212. m_textureCube[ii].idx = bgfx::kInvalidHandle;
  213. }
  214. m_textureCube[0] = bgfx::createTextureCube(
  215. kTextureSide
  216. , false
  217. , 1
  218. , bgfx::TextureFormat::BGRA8
  219. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  220. );
  221. if (m_blitSupported)
  222. {
  223. m_textureCube[1] = bgfx::createTextureCube(
  224. kTextureSide
  225. , false
  226. , 1
  227. , bgfx::TextureFormat::BGRA8
  228. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT|BGFX_TEXTURE_BLIT_DST
  229. );
  230. }
  231. if (m_computeSupported)
  232. {
  233. m_textureCube[2] = bgfx::createTextureCube(
  234. kTextureSide
  235. , false
  236. , 1
  237. , bgfx::TextureFormat::RGBA8
  238. , BGFX_TEXTURE_COMPUTE_WRITE
  239. );
  240. }
  241. m_texture2d = bgfx::createTexture2D(
  242. kTexture2dSize
  243. , kTexture2dSize
  244. , false
  245. , 1
  246. , bgfx::TextureFormat::BGRA8
  247. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  248. );
  249. m_texture2dData = (uint8_t*)malloc(kTexture2dSize*kTexture2dSize*4);
  250. m_rr = rand()%255;
  251. m_gg = rand()%255;
  252. m_bb = rand()%255;
  253. m_hit = 0;
  254. m_miss = 0;
  255. m_updateTime = 0;
  256. m_timeOffset = bx::getHPCounter();
  257. imguiCreate();
  258. }
  259. virtual int shutdown() override
  260. {
  261. imguiDestroy();
  262. // m_texture2dData is managed from main thread, and it's passed to renderer
  263. // just as MemoryRef. At this point render might be using it. We must wait
  264. // previous frame to finish before we can free it.
  265. bgfx::frame();
  266. // Cleanup.
  267. free(m_texture2dData);
  268. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  269. {
  270. bgfx::destroy(m_textures[ii]);
  271. }
  272. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  273. {
  274. bgfx::destroy(m_textures3d[ii]);
  275. }
  276. bgfx::destroy(m_texture2d);
  277. for (uint32_t ii = 0; ii<BX_COUNTOF(m_textureCube); ++ii)
  278. {
  279. if (bgfx::isValid(m_textureCube[ii]))
  280. {
  281. bgfx::destroy(m_textureCube[ii]);
  282. }
  283. }
  284. bgfx::destroy(m_ibh);
  285. bgfx::destroy(m_vbh);
  286. if (bgfx::isValid(m_program3d) )
  287. {
  288. bgfx::destroy(m_program3d);
  289. }
  290. bgfx::destroy(m_programCmp);
  291. if (bgfx::isValid(m_programCompute) )
  292. {
  293. bgfx::destroy(m_programCompute);
  294. }
  295. bgfx::destroy(m_program);
  296. bgfx::destroy(u_time);
  297. bgfx::destroy(s_texColor);
  298. bgfx::destroy(s_texCube);
  299. // Shutdown bgfx.
  300. bgfx::shutdown();
  301. return 0;
  302. }
  303. bool update() override
  304. {
  305. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  306. {
  307. imguiBeginFrame(m_mouseState.m_mx
  308. , m_mouseState.m_my
  309. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  310. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  311. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  312. , m_mouseState.m_mz
  313. , uint16_t(m_width)
  314. , uint16_t(m_height)
  315. );
  316. showExampleDialog(this);
  317. imguiEndFrame();
  318. float borderColor[4] = { float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f };
  319. bgfx::setPaletteColor(1, borderColor);
  320. // Set view 0 and 1 viewport.
  321. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  322. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  323. // This dummy draw call is here to make sure that view 0 is cleared
  324. // if no other draw calls are submitted to view 0.
  325. bgfx::touch(0);
  326. int64_t now = bx::getHPCounter();
  327. float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
  328. bgfx::setUniform(u_time, &time);
  329. if (now > m_updateTime)
  330. {
  331. PackCube face;
  332. uint16_t bw = bx::uint16_max(1, rand()%(kTextureSide/4) );
  333. uint16_t bh = bx::uint16_max(1, rand()%(kTextureSide/4) );
  334. if (m_cube.find(bw, bh, face) )
  335. {
  336. m_quads.push_back(face);
  337. ++m_hit;
  338. const Pack2D& rect = face.m_rect;
  339. 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);
  340. if (m_blitSupported)
  341. {
  342. 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);
  343. }
  344. m_rr = rand()%255;
  345. m_gg = rand()%255;
  346. m_bb = rand()%255;
  347. }
  348. else
  349. {
  350. ++m_miss;
  351. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
  352. {
  353. face = m_quads.front();
  354. const Pack2D& rect = face.m_rect;
  355. updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
  356. if (m_blitSupported)
  357. {
  358. 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);
  359. }
  360. m_cube.clear(face);
  361. m_quads.pop_front();
  362. }
  363. }
  364. {
  365. // Fill rect.
  366. const uint32_t pitch = kTexture2dSize*4;
  367. const uint16_t tw = rand()% kTexture2dSize;
  368. const uint16_t th = rand()% kTexture2dSize;
  369. const uint16_t tx = rand()%(kTexture2dSize-tw);
  370. const uint16_t ty = rand()%(kTexture2dSize-th);
  371. uint8_t* dst = &m_texture2dData[(ty*kTexture2dSize+tx)*4];
  372. uint8_t* next = dst + pitch;
  373. // Using makeRef to pass texture memory without copying.
  374. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  375. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  376. {
  377. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  378. {
  379. dst[0] = m_bb;
  380. dst[1] = m_gg;
  381. dst[2] = m_rr;
  382. dst[3] = 255;
  383. }
  384. }
  385. // Pitch here makes possible to pass data from source to destination
  386. // without need for m_textures and allocated memory to be the same size.
  387. bgfx::updateTexture2D(m_texture2d, 0, 0, tx, ty, tw, th, mem, pitch);
  388. }
  389. }
  390. float at[3] = { 0.0f, 0.0f, 0.0f };
  391. float eye[3] = { 0.0f, 0.0f, -5.0f };
  392. float view[16];
  393. bx::mtxLookAt(view, eye, at);
  394. float proj[16];
  395. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  396. // Set view and projection matrix for view 0.
  397. bgfx::setViewTransform(0, view, proj);
  398. // Update texturecube using compute shader
  399. if (bgfx::isValid(m_programCompute) )
  400. {
  401. bgfx::setImage(0, m_textureCube[2], 0, bgfx::Access::Write);
  402. bgfx::dispatch(0, m_programCompute, kTextureSide/16, kTextureSide/16);
  403. }
  404. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
  405. {
  406. if (bgfx::isValid(m_textureCube[ii]))
  407. {
  408. float mtx[16];
  409. bx::mtxSRT(mtx, 0.7f, 0.7f, 0.7f, time, time*0.37f, 0.0f, -2.0f +ii*2.0f, 0.0f, 0.0f);
  410. // Set model matrix for rendering.
  411. bgfx::setTransform(mtx);
  412. // Set vertex and index buffer.
  413. bgfx::setVertexBuffer(0, m_vbh);
  414. bgfx::setIndexBuffer(m_ibh);
  415. // Bind texture.
  416. bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
  417. // Set render states.
  418. bgfx::setState(BGFX_STATE_DEFAULT);
  419. // Submit primitive for rendering to view 0.
  420. bgfx::submit(0, m_program);
  421. }
  422. }
  423. // Set view and projection matrix for view 1.
  424. const float aspectRatio = float(m_height)/float(m_width);
  425. const float size = 11.0f;
  426. const bgfx::Caps* caps = bgfx::getCaps();
  427. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
  428. bgfx::setViewTransform(1, NULL, proj);
  429. float mtx[16];
  430. bx::mtxTranslate(mtx, -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f, 1.9f, 0.0f);
  431. // Set model matrix for rendering.
  432. bgfx::setTransform(mtx);
  433. // Set vertex and index buffer.
  434. bgfx::setVertexBuffer(0, m_vbh);
  435. bgfx::setIndexBuffer(m_ibh);
  436. // Bind texture.
  437. bgfx::setTexture(0, s_texColor, m_texture2d);
  438. // Set render states.
  439. bgfx::setState(BGFX_STATE_DEFAULT);
  440. // Submit primitive for rendering to view 1.
  441. bgfx::submit(1, m_programCmp);
  442. const float xpos = -size+2.0f - BX_COUNTOF(m_textures)*0.1f*0.5f;
  443. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  444. {
  445. bx::mtxTranslate(mtx, xpos + ii*2.1f, size-6.5f, 0.0f);
  446. // Set model matrix for rendering.
  447. bgfx::setTransform(mtx);
  448. // Set vertex and index buffer.
  449. bgfx::setVertexBuffer(0, m_vbh);
  450. bgfx::setIndexBuffer(m_ibh, 0, 6);
  451. // Bind texture.
  452. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  453. // Set render states.
  454. bgfx::setState(BGFX_STATE_DEFAULT);
  455. // Submit primitive for rendering to view 1.
  456. bgfx::submit(1, m_programCmp);
  457. }
  458. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  459. {
  460. bx::mtxTranslate(mtx, xpos + (ii+3)*2.1f, -size+6.5f, 0.0f);
  461. // Set model matrix for rendering.
  462. bgfx::setTransform(mtx);
  463. // Set vertex and index buffer.
  464. bgfx::setVertexBuffer(0, m_vbh);
  465. bgfx::setIndexBuffer(m_ibh, 0, 6);
  466. // Bind texture.
  467. bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
  468. // Set render states.
  469. bgfx::setState(BGFX_STATE_DEFAULT);
  470. // Submit primitive for rendering to view 1.
  471. bgfx::submit(1, m_program3d);
  472. }
  473. for (uint32_t ii = 0; ii < 4; ++ii)
  474. {
  475. bx::mtxTranslate(mtx, xpos + (size-2.0f)*2.1f, -size+6.5f + ii*2.1f, 0.0f);
  476. // Set model matrix for rendering.
  477. bgfx::setTransform(mtx);
  478. // Set vertex and index buffer.
  479. bgfx::setVertexBuffer(0, m_vbh, 24, 4);
  480. bgfx::setIndexBuffer(m_ibh, 0, 6);
  481. // Bind texture.
  482. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  483. // Set render states.
  484. bgfx::setState(BGFX_STATE_DEFAULT);
  485. // Submit primitive for rendering to view 1.
  486. bgfx::submit(1, m_programCmp);
  487. }
  488. // Advance to next frame. Rendering thread will be kicked to
  489. // process submitted rendering primitives.
  490. bgfx::frame();
  491. return true;
  492. }
  493. return false;
  494. }
  495. entry::MouseState m_mouseState;
  496. uint32_t m_width;
  497. uint32_t m_height;
  498. uint32_t m_debug;
  499. uint32_t m_reset;
  500. uint8_t* m_texture2dData;
  501. uint32_t m_numTextures3d;
  502. bool m_texture3DSupported;
  503. bool m_blitSupported;
  504. bool m_computeSupported;
  505. std::list<PackCube> m_quads;
  506. RectPackCubeT<256> m_cube;
  507. int64_t m_updateTime;
  508. int64_t m_timeOffset;
  509. uint32_t m_hit;
  510. uint32_t m_miss;
  511. uint8_t m_rr;
  512. uint8_t m_gg;
  513. uint8_t m_bb;
  514. bgfx::TextureHandle m_textures[9];
  515. bgfx::TextureHandle m_textures3d[3];
  516. bgfx::TextureHandle m_texture2d;
  517. bgfx::TextureHandle m_textureCube[3];
  518. bgfx::IndexBufferHandle m_ibh;
  519. bgfx::VertexBufferHandle m_vbh;
  520. bgfx::ProgramHandle m_program3d;
  521. bgfx::ProgramHandle m_programCmp;
  522. bgfx::ProgramHandle m_programCompute;
  523. bgfx::ProgramHandle m_program;
  524. bgfx::UniformHandle u_time;
  525. bgfx::UniformHandle s_texColor;
  526. bgfx::UniformHandle s_texCube;
  527. };
  528. } // namespace
  529. ENTRY_IMPLEMENT_MAIN(ExampleUpdate, "08-update", "Updating textures.");