update.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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_layout
  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::VertexLayout ms_layout;
  30. };
  31. bgfx::VertexLayout PosTexcoordVertex::ms_layout;
  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_layout);
  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(kTextureSide, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT);
  256. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCubeFaceFb); ++ii)
  257. {
  258. bgfx::Attachment at;
  259. at.init(m_textureCube[3], bgfx::Access::Write, uint16_t(ii));
  260. m_textureCubeFaceFb[ii] = bgfx::createFrameBuffer(1, &at);
  261. }
  262. }
  263. m_texture2d = bgfx::createTexture2D(
  264. kTexture2dSize
  265. , kTexture2dSize
  266. , false
  267. , 1
  268. , bgfx::TextureFormat::BGRA8
  269. , BGFX_SAMPLER_MIN_POINT|BGFX_SAMPLER_MAG_POINT|BGFX_SAMPLER_MIP_POINT
  270. );
  271. m_texture2dData = (uint8_t*)malloc(kTexture2dSize*kTexture2dSize*4);
  272. m_rr = m_rng.gen()%255;
  273. m_gg = m_rng.gen()%255;
  274. m_bb = m_rng.gen()%255;
  275. m_hit = 0;
  276. m_miss = 0;
  277. m_updateTime = 0;
  278. m_timeOffset = bx::getHPCounter();
  279. imguiCreate();
  280. }
  281. virtual int shutdown() override
  282. {
  283. imguiDestroy();
  284. // m_texture2dData is managed from main thread, and it's passed to renderer
  285. // just as MemoryRef. At this point render might be using it. We must wait
  286. // previous frame to finish before we can free it.
  287. bgfx::frame();
  288. // Cleanup.
  289. free(m_texture2dData);
  290. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  291. {
  292. bgfx::destroy(m_textures[ii]);
  293. }
  294. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  295. {
  296. bgfx::destroy(m_textures3d[ii]);
  297. }
  298. bgfx::destroy(m_texture2d);
  299. for (uint32_t ii = 0; ii<BX_COUNTOF(m_textureCube); ++ii)
  300. {
  301. if (bgfx::isValid(m_textureCube[ii]))
  302. {
  303. bgfx::destroy(m_textureCube[ii]);
  304. }
  305. }
  306. for (uint32_t ii = 0; ii<BX_COUNTOF(m_textureCubeFaceFb); ++ii)
  307. {
  308. if (bgfx::isValid(m_textureCubeFaceFb[ii]))
  309. {
  310. bgfx::destroy(m_textureCubeFaceFb[ii]);
  311. }
  312. }
  313. bgfx::destroy(m_ibh);
  314. bgfx::destroy(m_vbh);
  315. if (bgfx::isValid(m_program3d) )
  316. {
  317. bgfx::destroy(m_program3d);
  318. }
  319. bgfx::destroy(m_programCmp);
  320. if (bgfx::isValid(m_programCompute) )
  321. {
  322. bgfx::destroy(m_programCompute);
  323. }
  324. bgfx::destroy(m_program);
  325. bgfx::destroy(u_time);
  326. bgfx::destroy(s_texColor);
  327. bgfx::destroy(s_texCube);
  328. // Shutdown bgfx.
  329. bgfx::shutdown();
  330. return 0;
  331. }
  332. bool update() override
  333. {
  334. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  335. {
  336. imguiBeginFrame(m_mouseState.m_mx
  337. , m_mouseState.m_my
  338. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  339. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  340. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  341. , m_mouseState.m_mz
  342. , uint16_t(m_width)
  343. , uint16_t(m_height)
  344. );
  345. showExampleDialog(this);
  346. imguiEndFrame();
  347. float borderColor[4] =
  348. {
  349. float(m_rng.gen()%255)/255.0f,
  350. float(m_rng.gen()%255)/255.0f,
  351. float(m_rng.gen()%255)/255.0f,
  352. float(m_rng.gen()%255)/255.0f,
  353. };
  354. bgfx::setPaletteColor(1, borderColor);
  355. // Set view 0 and 1 viewport.
  356. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  357. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  358. // This dummy draw call is here to make sure that view 0 is cleared
  359. // if no other draw calls are submitted to view 0.
  360. bgfx::touch(0);
  361. int64_t now = bx::getHPCounter();
  362. float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
  363. bgfx::setUniform(u_time, &time);
  364. if (now > m_updateTime)
  365. {
  366. PackCube face;
  367. uint16_t bw = bx::max<uint16_t>(1, m_rng.gen()%(kTextureSide/4) );
  368. uint16_t bh = bx::max<uint16_t>(1, m_rng.gen()%(kTextureSide/4) );
  369. if (m_cube.find(bw, bh, face) )
  370. {
  371. m_quads.push_back(face);
  372. ++m_hit;
  373. const Pack2D& rect = face.m_rect;
  374. 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);
  375. if (m_blitSupported)
  376. {
  377. 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);
  378. }
  379. m_rr = m_rng.gen()%255;
  380. m_gg = m_rng.gen()%255;
  381. m_bb = m_rng.gen()%255;
  382. }
  383. else
  384. {
  385. ++m_miss;
  386. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
  387. {
  388. face = m_quads.front();
  389. const Pack2D& rect = face.m_rect;
  390. updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
  391. if (m_blitSupported)
  392. {
  393. 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);
  394. }
  395. m_cube.clear(face);
  396. m_quads.pop_front();
  397. }
  398. }
  399. {
  400. // Fill rect.
  401. const uint32_t pitch = kTexture2dSize*4;
  402. const uint16_t tw = m_rng.gen()% kTexture2dSize;
  403. const uint16_t th = m_rng.gen()% kTexture2dSize;
  404. const uint16_t tx = m_rng.gen()%(kTexture2dSize-tw);
  405. const uint16_t ty = m_rng.gen()%(kTexture2dSize-th);
  406. uint8_t* dst = &m_texture2dData[(ty*kTexture2dSize+tx)*4];
  407. uint8_t* next = dst + pitch;
  408. // Using makeRef to pass texture memory without copying.
  409. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  410. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  411. {
  412. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  413. {
  414. dst[0] = m_bb;
  415. dst[1] = m_gg;
  416. dst[2] = m_rr;
  417. dst[3] = 255;
  418. }
  419. }
  420. // Pitch here makes possible to pass data from source to destination
  421. // without need for m_textures and allocated memory to be the same size.
  422. bgfx::updateTexture2D(m_texture2d, 0, 0, tx, ty, tw, th, mem, pitch);
  423. }
  424. }
  425. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  426. const bx::Vec3 eye = { 0.0f, 0.0f, -5.0f };
  427. float view[16];
  428. bx::mtxLookAt(view, eye, at);
  429. float proj[16];
  430. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  431. // Set view and projection matrix for view 0.
  432. bgfx::setViewTransform(0, view, proj);
  433. // Update texturecube using compute shader
  434. if (bgfx::isValid(m_programCompute) )
  435. {
  436. bgfx::setImage(0, m_textureCube[2], 0, bgfx::Access::Write);
  437. bgfx::dispatch(0, m_programCompute, kTextureSide/16, kTextureSide/16);
  438. }
  439. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCubeFaceFb); ++ii)
  440. {
  441. bgfx::ViewId viewId = bgfx::ViewId(ii+2);
  442. bgfx::setViewFrameBuffer(viewId, m_textureCubeFaceFb[ii]);
  443. bx::Vec3 color = bx::add(s_faceColors[ii], bx::sin(time*4.0f)*0.25f);
  444. uint32_t colorRGB8 =
  445. uint32_t(bx::toUnorm(color.x, 255.0f) ) << 24
  446. | uint32_t(bx::toUnorm(color.y, 255.0f) ) << 16
  447. | uint32_t(bx::toUnorm(color.z, 255.0f) ) << 8;
  448. bgfx::setViewClear(viewId, BGFX_CLEAR_COLOR, colorRGB8);
  449. bgfx::setViewRect(viewId, 0,0,512,512);
  450. bgfx::touch(viewId);
  451. }
  452. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
  453. {
  454. if (bgfx::isValid(m_textureCube[ii]))
  455. {
  456. float mtx[16];
  457. bx::mtxSRT(mtx, 0.65f, 0.65f, 0.65f, time, time*0.37f, 0.0f, -2.5f +ii*1.8f, 0.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);
  463. // Bind texture.
  464. bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
  465. // Set render states.
  466. bgfx::setState(BGFX_STATE_DEFAULT);
  467. // Submit primitive for rendering to view 0.
  468. bgfx::submit(0, m_program);
  469. }
  470. }
  471. // Set view and projection matrix for view 1.
  472. const float aspectRatio = float(m_height)/float(m_width);
  473. const float margin = 0.7f;
  474. const float sizeX = 0.5f * BX_COUNTOF(m_textures) * 2.1f + margin;
  475. const float sizeY = sizeX * aspectRatio;
  476. const bgfx::Caps* caps = bgfx::getCaps();
  477. bx::mtxOrtho(proj, -sizeX, sizeX, sizeY, -sizeY, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
  478. bgfx::setViewTransform(1, NULL, proj);
  479. float mtx[16];
  480. bx::mtxTranslate(mtx, -sizeX + margin + 1.0f, 1.9f, 0.0f);
  481. // Set model matrix for rendering.
  482. bgfx::setTransform(mtx);
  483. // Set vertex and index buffer.
  484. bgfx::setVertexBuffer(0, m_vbh);
  485. bgfx::setIndexBuffer(m_ibh);
  486. // Bind texture.
  487. bgfx::setTexture(0, s_texColor, m_texture2d);
  488. // Set render states.
  489. bgfx::setState(BGFX_STATE_DEFAULT);
  490. // Submit primitive for rendering to view 1.
  491. bgfx::submit(1, m_programCmp);
  492. const float xpos = -sizeX + margin + 1.0f;
  493. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  494. {
  495. bx::mtxTranslate(mtx, xpos + ii*2.1f, sizeY - margin - 1.0f, 0.0f);
  496. // Set model matrix for rendering.
  497. bgfx::setTransform(mtx);
  498. // Set vertex and index buffer.
  499. bgfx::setVertexBuffer(0, m_vbh);
  500. bgfx::setIndexBuffer(m_ibh, 0, 6);
  501. // Bind texture.
  502. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  503. // Set render states.
  504. bgfx::setState(BGFX_STATE_DEFAULT);
  505. // Submit primitive for rendering to view 1.
  506. bgfx::submit(1, m_programCmp);
  507. }
  508. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  509. {
  510. bx::mtxTranslate(mtx, xpos + (ii+(BX_COUNTOF(m_textures) - m_numTextures3d)*0.5f)*2.1f, -sizeY + margin + 1.0f, 0.0f);
  511. // Set model matrix for rendering.
  512. bgfx::setTransform(mtx);
  513. // Set vertex and index buffer.
  514. bgfx::setVertexBuffer(0, m_vbh);
  515. bgfx::setIndexBuffer(m_ibh, 0, 6);
  516. // Bind texture.
  517. bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
  518. // Set render states.
  519. bgfx::setState(BGFX_STATE_DEFAULT);
  520. // Submit primitive for rendering to view 1.
  521. bgfx::submit(1, m_program3d);
  522. }
  523. for (uint32_t ii = 0; ii < 5; ++ii)
  524. {
  525. bx::mtxTranslate(mtx, sizeX - margin - 1.0f, -sizeY + margin + 1.0f + ii*2.1f, 0.0f);
  526. // Set model matrix for rendering.
  527. bgfx::setTransform(mtx);
  528. // Set vertex and index buffer.
  529. bgfx::setVertexBuffer(0, m_vbh, 24, 4);
  530. bgfx::setIndexBuffer(m_ibh, 0, 6);
  531. // Bind texture.
  532. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  533. // Set render states.
  534. bgfx::setState(BGFX_STATE_DEFAULT);
  535. // Submit primitive for rendering to view 1.
  536. bgfx::submit(1, m_programCmp);
  537. }
  538. // Advance to next frame. Rendering thread will be kicked to
  539. // process submitted rendering primitives.
  540. bgfx::frame();
  541. return true;
  542. }
  543. return false;
  544. }
  545. entry::MouseState m_mouseState;
  546. uint32_t m_width;
  547. uint32_t m_height;
  548. uint32_t m_debug;
  549. uint32_t m_reset;
  550. uint8_t* m_texture2dData;
  551. uint32_t m_numTextures3d;
  552. bool m_texture3DSupported;
  553. bool m_blitSupported;
  554. bool m_computeSupported;
  555. std::list<PackCube> m_quads;
  556. RectPackCubeT<256> m_cube;
  557. int64_t m_updateTime;
  558. int64_t m_timeOffset;
  559. bx::RngMwc m_rng;
  560. uint32_t m_hit;
  561. uint32_t m_miss;
  562. uint8_t m_rr;
  563. uint8_t m_gg;
  564. uint8_t m_bb;
  565. bgfx::TextureHandle m_textures[12];
  566. bgfx::TextureHandle m_textures3d[3];
  567. bgfx::TextureHandle m_texture2d;
  568. bgfx::TextureHandle m_textureCube[4];
  569. bgfx::FrameBufferHandle m_textureCubeFaceFb[6];
  570. bgfx::IndexBufferHandle m_ibh;
  571. bgfx::VertexBufferHandle m_vbh;
  572. bgfx::ProgramHandle m_program3d;
  573. bgfx::ProgramHandle m_programCmp;
  574. bgfx::ProgramHandle m_programCompute;
  575. bgfx::ProgramHandle m_program;
  576. bgfx::UniformHandle u_time;
  577. bgfx::UniformHandle s_texColor;
  578. bgfx::UniformHandle s_texCube;
  579. };
  580. } // namespace
  581. ENTRY_IMPLEMENT_MAIN(ExampleUpdate, "08-update", "Updating textures.");