update.cpp 18 KB

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