update.cpp 18 KB

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