update.cpp 20 KB

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