update.cpp 18 KB

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