update.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * Copyright 2011-2020 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 "entry/entry.h"
  10. #include <bimg/decode.h>
  11. #include <bx/rng.h>
  12. #include <list>
  13. namespace
  14. {
  15. struct PosTexcoordVertex
  16. {
  17. float m_x;
  18. float m_y;
  19. float m_z;
  20. float m_u;
  21. float m_v;
  22. float m_w;
  23. static void init()
  24. {
  25. ms_layout
  26. .begin()
  27. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  28. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  29. .end();
  30. };
  31. static bgfx::VertexLayout ms_layout;
  32. };
  33. bgfx::VertexLayout PosTexcoordVertex::ms_layout;
  34. static PosTexcoordVertex s_cubeVertices[] =
  35. {
  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, 1.0f, -1.0f, 1.0f },
  59. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.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. {-1.0f, -1.0f, 1.0f, -2.0f, -2.0f, 2.0f },
  63. { 1.0f, -1.0f, 1.0f, 2.0f, -2.0f, 2.0f },
  64. };
  65. BX_STATIC_ASSERT(BX_COUNTOF(s_cubeVertices) == 28);
  66. static const uint16_t s_cubeIndices[] =
  67. {
  68. 0, 1, 2, // 0
  69. 1, 3, 2,
  70. 4, 6, 5, // 2
  71. 5, 6, 7,
  72. 8, 10, 9, // 4
  73. 9, 10, 11,
  74. 12, 14, 13, // 6
  75. 14, 15, 13,
  76. 16, 18, 17, // 8
  77. 18, 19, 17,
  78. 20, 22, 21, // 10
  79. 21, 22, 23,
  80. };
  81. BX_STATIC_ASSERT(BX_COUNTOF(s_cubeIndices) == 36);
  82. bx::Vec3 s_faceColors[] =
  83. {
  84. { 0.75f, 0.0f, 0.0f },
  85. { 0.75f, 0.75f, 0.0f },
  86. { 0.75f, 0.0f, 0.75f },
  87. { 0.0f, 0.75f, 0.0f },
  88. { 0.0f, 0.75f, 0.75f },
  89. { 0.0f, 0.0f, 0.75f },
  90. };
  91. static void updateTextureCubeRectBgra8(
  92. bgfx::TextureHandle _handle
  93. , uint8_t _side
  94. , uint16_t _x
  95. , uint16_t _y
  96. , uint16_t _width
  97. , uint16_t _height
  98. , uint8_t _r
  99. , uint8_t _g
  100. , uint8_t _b
  101. , uint8_t _a = 0xff
  102. )
  103. {
  104. bgfx::TextureInfo ti;
  105. bgfx::calcTextureSize(ti, _width, _height, 1, false, false, 1, bgfx::TextureFormat::BGRA8);
  106. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  107. uint8_t* data = (uint8_t*)mem->data;
  108. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  109. {
  110. data[0] = _b;
  111. data[1] = _g;
  112. data[2] = _r;
  113. data[3] = _a;
  114. data += 4;
  115. }
  116. bgfx::updateTextureCube(_handle, 0, _side, 0, _x, _y, _width, _height, mem);
  117. }
  118. bgfx::TextureHandle loadTextureWithUpdate(const char* _filePath, uint64_t _flags = BGFX_TEXTURE_NONE | BGFX_SAMPLER_NONE)
  119. {
  120. bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
  121. uint32_t size;
  122. void* data = load(_filePath, &size);
  123. if (NULL != data)
  124. {
  125. bimg::ImageContainer* imageContainer = bimg::imageParse(entry::getAllocator(), data, size);
  126. if (NULL != imageContainer)
  127. {
  128. BX_ASSERT(!imageContainer->m_cubeMap, "Cubemap Texture loading not supported");
  129. BX_ASSERT(1 >= imageContainer->m_depth, "3D Texture loading not supported");
  130. BX_ASSERT(1 == imageContainer->m_numLayers, "Texture Layer loading not supported");
  131. if (!imageContainer->m_cubeMap
  132. && 1 >= imageContainer->m_depth
  133. && 1 == imageContainer->m_numLayers
  134. && bgfx::isTextureValid(0, false, imageContainer->m_numLayers, bgfx::TextureFormat::Enum(imageContainer->m_format), _flags)
  135. )
  136. {
  137. handle = bgfx::createTexture2D(
  138. uint16_t(imageContainer->m_width)
  139. , uint16_t(imageContainer->m_height)
  140. , 1 < imageContainer->m_numMips
  141. , imageContainer->m_numLayers
  142. , bgfx::TextureFormat::Enum(imageContainer->m_format)
  143. , _flags
  144. , NULL
  145. );
  146. uint32_t width = imageContainer->m_width;
  147. uint32_t height = imageContainer->m_height;
  148. for (uint8_t lod = 0, num = imageContainer->m_numMips; lod < num; ++lod)
  149. {
  150. if (width < 4 || height < 4)
  151. {
  152. break;
  153. }
  154. width = bx::max(1u, width);
  155. height = bx::max(1u, height);
  156. bimg::ImageMip mip;
  157. if (bimg::imageGetRawData(*imageContainer, 0, lod, imageContainer->m_data, imageContainer->m_size, mip))
  158. {
  159. const uint8_t* mipData = mip.m_data;
  160. uint32_t mipDataSize = mip.m_size;
  161. bgfx::updateTexture2D(
  162. handle
  163. , 0
  164. , lod
  165. , 0
  166. , 0
  167. , uint16_t(width)
  168. , uint16_t(height)
  169. , bgfx::copy(mipData, mipDataSize)
  170. );
  171. }
  172. width >>= 1;
  173. height >>= 1;
  174. }
  175. unload(data);
  176. }
  177. if (bgfx::isValid(handle))
  178. {
  179. bgfx::setName(handle, _filePath);
  180. }
  181. }
  182. }
  183. return handle;
  184. }
  185. static const uint16_t kTextureSide = 512;
  186. static const uint32_t kTexture2dSize = 256;
  187. class ExampleUpdate : public entry::AppI
  188. {
  189. public:
  190. ExampleUpdate(const char* _name, const char* _description, const char* _url)
  191. : entry::AppI(_name, _description, _url)
  192. , m_cube(kTextureSide)
  193. {
  194. }
  195. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  196. {
  197. Args args(_argc, _argv);
  198. m_width = _width;
  199. m_height = _height;
  200. m_debug = BGFX_DEBUG_NONE;
  201. m_reset = BGFX_RESET_VSYNC;
  202. bgfx::Init init;
  203. init.type = args.m_type;
  204. init.vendorId = args.m_pciId;
  205. init.resolution.width = m_width;
  206. init.resolution.height = m_height;
  207. init.resolution.reset = m_reset;
  208. bgfx::init(init);
  209. // Enable debug text.
  210. bgfx::setDebug(m_debug);
  211. // Set view 0 clear state.
  212. bgfx::setViewClear(0
  213. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  214. , 0x303030ff
  215. , 1.0f
  216. , 0
  217. );
  218. m_showDescriptions = true;
  219. // Create vertex stream declaration.
  220. PosTexcoordVertex::init();
  221. m_textures[ 0] = loadTexture("textures/texture_compression_bc1.ktx", BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  222. m_textures[ 1] = loadTexture("textures/texture_compression_bc2.ktx", BGFX_SAMPLER_U_CLAMP);
  223. m_textures[ 2] = loadTexture("textures/texture_compression_bc3.ktx", BGFX_SAMPLER_V_CLAMP);
  224. m_textures[ 3] = loadTexture("textures/texture_compression_etc1.ktx", BGFX_SAMPLER_U_BORDER | BGFX_SAMPLER_V_BORDER | BGFX_SAMPLER_BORDER_COLOR(1));
  225. m_textures[ 4] = loadTexture("textures/texture_compression_etc2.ktx");
  226. m_textures[ 5] = loadTexture("textures/texture_compression_ptc12.pvr");
  227. m_textures[ 6] = loadTexture("textures/texture_compression_ptc14.pvr");
  228. m_textures[ 7] = loadTexture("textures/texture_compression_ptc22.pvr");
  229. m_textures[ 8] = loadTexture("textures/texture_compression_ptc24.pvr");
  230. m_textures[ 9] = loadTexture("textures/texture_compression_atc.dds");
  231. m_textures[10] = loadTexture("textures/texture_compression_atci.dds");
  232. m_textures[11] = loadTexture("textures/texture_compression_atce.dds");
  233. m_textures[12] = loadTextureWithUpdate("textures/texture_compression_bc1.ktx", BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP);
  234. m_textures[13] = loadTextureWithUpdate("textures/texture_compression_bc2.ktx", BGFX_SAMPLER_U_CLAMP);
  235. m_textures[14] = loadTextureWithUpdate("textures/texture_compression_bc3.ktx", BGFX_SAMPLER_V_CLAMP);
  236. m_textures[15] = loadTextureWithUpdate("textures/texture_compression_etc1.ktx", BGFX_SAMPLER_U_BORDER | BGFX_SAMPLER_V_BORDER | BGFX_SAMPLER_BORDER_COLOR(1));
  237. m_textures[16] = loadTextureWithUpdate("textures/texture_compression_etc2.ktx");
  238. m_textures[17] = loadTextureWithUpdate("textures/texture_compression_ptc12.pvr");
  239. m_textures[18] = loadTextureWithUpdate("textures/texture_compression_ptc14.pvr");
  240. m_textures[19] = loadTextureWithUpdate("textures/texture_compression_ptc22.pvr");
  241. m_textures[20] = loadTextureWithUpdate("textures/texture_compression_ptc24.pvr");
  242. m_textures[21] = loadTextureWithUpdate("textures/texture_compression_atc.dds");
  243. m_textures[22] = loadTextureWithUpdate("textures/texture_compression_atci.dds");
  244. m_textures[23] = loadTextureWithUpdate("textures/texture_compression_atce.dds");
  245. BX_STATIC_ASSERT(24 == BX_COUNTOF(m_textures));
  246. const bgfx::Caps* caps = bgfx::getCaps();
  247. m_texture3DSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_3D);
  248. m_blitSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_BLIT);
  249. m_computeSupported = !!(caps->supported & BGFX_CAPS_COMPUTE);
  250. m_numTextures3d = 0;
  251. if (m_texture3DSupported)
  252. {
  253. const bgfx::Memory* mem8 = bgfx::alloc(32*32*32);
  254. const bgfx::Memory* mem16f = bgfx::alloc(32*32*32*2);
  255. const bgfx::Memory* mem32f = bgfx::alloc(32*32*32*4);
  256. for (uint8_t zz = 0; zz < 32; ++zz)
  257. {
  258. for (uint8_t yy = 0; yy < 32; ++yy)
  259. {
  260. for (uint8_t xx = 0; xx < 32; ++xx)
  261. {
  262. const uint32_t offset = ( (zz*32+yy)*32+xx);
  263. const uint32_t val = xx ^ yy ^ zz;
  264. mem8->data[offset] = uint8_t(val<<3);
  265. *(uint16_t*)&mem16f->data[offset*2] = bx::halfFromFloat( (float)val/32.0f);
  266. *(float*)&mem32f->data[offset*4] = (float)val/32.0f;
  267. }
  268. }
  269. }
  270. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R8]) )
  271. {
  272. 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);
  273. }
  274. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R16F]) )
  275. {
  276. 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);
  277. }
  278. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_3D & caps->formats[bgfx::TextureFormat::R32F]) )
  279. {
  280. 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);
  281. }
  282. }
  283. // Create static vertex buffer.
  284. m_vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ), PosTexcoordVertex::ms_layout);
  285. // Create static index buffer.
  286. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  287. // Create programs.
  288. m_program = loadProgram("vs_update", "fs_update");
  289. m_programCmp = loadProgram("vs_update", "fs_update_cmp");
  290. m_program3d.idx = bgfx::kInvalidHandle;
  291. if (m_texture3DSupported)
  292. {
  293. m_program3d = loadProgram("vs_update", "fs_update_3d");
  294. }
  295. m_programCompute.idx = bgfx::kInvalidHandle;
  296. if (m_computeSupported)
  297. {
  298. m_programCompute = bgfx::createProgram( loadShader( "cs_update" ), true );
  299. }
  300. // Create texture sampler uniforms.
  301. s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Sampler);
  302. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  303. // Create time uniform.
  304. u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
  305. for(uint32_t ii = 0; ii<BX_COUNTOF( m_textureCube ); ++ii)
  306. {
  307. m_textureCube[ii].idx = bgfx::kInvalidHandle;
  308. }
  309. m_textureCube[0] = bgfx::createTextureCube(
  310. kTextureSide
  311. , false
  312. , 1
  313. , bgfx::TextureFormat::BGRA8
  314. , BGFX_SAMPLER_MIN_POINT|BGFX_SAMPLER_MAG_POINT|BGFX_SAMPLER_MIP_POINT
  315. );
  316. if (m_blitSupported)
  317. {
  318. m_textureCube[1] = bgfx::createTextureCube(
  319. kTextureSide
  320. , false
  321. , 1
  322. , bgfx::TextureFormat::BGRA8
  323. , BGFX_SAMPLER_MIN_POINT|BGFX_SAMPLER_MAG_POINT|BGFX_SAMPLER_MIP_POINT|BGFX_TEXTURE_BLIT_DST
  324. );
  325. }
  326. if (m_computeSupported)
  327. {
  328. m_textureCube[2] = bgfx::createTextureCube(
  329. kTextureSide
  330. , false
  331. , 1
  332. , bgfx::TextureFormat::RGBA8
  333. , BGFX_TEXTURE_COMPUTE_WRITE
  334. );
  335. }
  336. {
  337. m_textureCube[3] = bgfx::createTextureCube(kTextureSide, false, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT);
  338. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCubeFaceFb); ++ii)
  339. {
  340. bgfx::Attachment at;
  341. at.init(m_textureCube[3], bgfx::Access::Write, uint16_t(ii));
  342. m_textureCubeFaceFb[ii] = bgfx::createFrameBuffer(1, &at);
  343. }
  344. }
  345. m_texture2d = bgfx::createTexture2D(
  346. kTexture2dSize
  347. , kTexture2dSize
  348. , false
  349. , 1
  350. , bgfx::TextureFormat::BGRA8
  351. , BGFX_SAMPLER_MIN_POINT|BGFX_SAMPLER_MAG_POINT|BGFX_SAMPLER_MIP_POINT
  352. );
  353. m_texture2dData = (uint8_t*)malloc(kTexture2dSize*kTexture2dSize*4);
  354. m_rr = m_rng.gen()%255;
  355. m_gg = m_rng.gen()%255;
  356. m_bb = m_rng.gen()%255;
  357. m_hit = 0;
  358. m_miss = 0;
  359. m_updateTime = 0;
  360. m_timeOffset = bx::getHPCounter();
  361. imguiCreate();
  362. }
  363. virtual int shutdown() override
  364. {
  365. imguiDestroy();
  366. // m_texture2dData is managed from main thread, and it's passed to renderer
  367. // just as MemoryRef. At this point render might be using it. We must wait
  368. // previous frame to finish before we can free it.
  369. bgfx::frame();
  370. // Cleanup.
  371. free(m_texture2dData);
  372. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  373. {
  374. bgfx::destroy(m_textures[ii]);
  375. }
  376. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  377. {
  378. bgfx::destroy(m_textures3d[ii]);
  379. }
  380. bgfx::destroy(m_texture2d);
  381. for (uint32_t ii = 0; ii<BX_COUNTOF(m_textureCube); ++ii)
  382. {
  383. if (bgfx::isValid(m_textureCube[ii]))
  384. {
  385. bgfx::destroy(m_textureCube[ii]);
  386. }
  387. }
  388. for (uint32_t ii = 0; ii<BX_COUNTOF(m_textureCubeFaceFb); ++ii)
  389. {
  390. if (bgfx::isValid(m_textureCubeFaceFb[ii]))
  391. {
  392. bgfx::destroy(m_textureCubeFaceFb[ii]);
  393. }
  394. }
  395. bgfx::destroy(m_ibh);
  396. bgfx::destroy(m_vbh);
  397. if (bgfx::isValid(m_program3d) )
  398. {
  399. bgfx::destroy(m_program3d);
  400. }
  401. bgfx::destroy(m_programCmp);
  402. if (bgfx::isValid(m_programCompute) )
  403. {
  404. bgfx::destroy(m_programCompute);
  405. }
  406. bgfx::destroy(m_program);
  407. bgfx::destroy(u_time);
  408. bgfx::destroy(s_texColor);
  409. bgfx::destroy(s_texCube);
  410. // Shutdown bgfx.
  411. bgfx::shutdown();
  412. return 0;
  413. }
  414. void ImGuiDescription(float _x, float _y, float _z, const float* _worldToScreen, const char* _text)
  415. {
  416. if (m_showDescriptions)
  417. {
  418. float worldPos[4] = { _x, _y, _z, 1.0f };
  419. float screenPos[4];
  420. bx::vec4MulMtx(screenPos, worldPos, _worldToScreen);
  421. ImGui::SetNextWindowPos(ImVec2(screenPos[0] / screenPos[3], screenPos[1] / screenPos[3]), 0, ImVec2(0.5f, 0.5f));
  422. ImGuiWindowFlags flags = ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize;
  423. ImGui::SetNextWindowBgAlpha(0.5f);
  424. ImGui::Begin(_text, NULL, flags);
  425. ImGui::Text("%s", _text);
  426. ImGui::End();
  427. }
  428. }
  429. bool update() override
  430. {
  431. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  432. {
  433. imguiBeginFrame(m_mouseState.m_mx
  434. , m_mouseState.m_my
  435. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  436. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  437. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  438. , m_mouseState.m_mz
  439. , uint16_t(m_width)
  440. , uint16_t(m_height)
  441. );
  442. showExampleDialog(this);
  443. ImGui::SetNextWindowPos (ImVec2( 10.0f, 270.0f), ImGuiCond_FirstUseEver);
  444. ImGui::SetNextWindowSize(ImVec2(150.0f, 70.0f), ImGuiCond_FirstUseEver);
  445. ImGui::Begin(
  446. "Show descriptions"
  447. , NULL
  448. , ImGuiWindowFlags_NoResize
  449. );
  450. if (ImGui::Button(m_showDescriptions ? "On" : "Off"))
  451. {
  452. m_showDescriptions = !m_showDescriptions;
  453. }
  454. ImGui::End();
  455. float borderColor[4] =
  456. {
  457. float(m_rng.gen()%255)/255.0f,
  458. float(m_rng.gen()%255)/255.0f,
  459. float(m_rng.gen()%255)/255.0f,
  460. float(m_rng.gen()%255)/255.0f,
  461. };
  462. bgfx::setPaletteColor(1, borderColor);
  463. // Set view 0 and 1 viewport.
  464. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  465. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  466. // This dummy draw call is here to make sure that view 0 is cleared
  467. // if no other draw calls are submitted to view 0.
  468. bgfx::touch(0);
  469. int64_t now = bx::getHPCounter();
  470. float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
  471. bgfx::setUniform(u_time, &time);
  472. if (now > m_updateTime)
  473. {
  474. PackCube face;
  475. uint16_t bw = bx::max<uint16_t>(1, m_rng.gen()%(kTextureSide/4) );
  476. uint16_t bh = bx::max<uint16_t>(1, m_rng.gen()%(kTextureSide/4) );
  477. if (m_cube.find(bw, bh, face) )
  478. {
  479. m_quads.push_back(face);
  480. ++m_hit;
  481. const Pack2D& rect = face.m_rect;
  482. 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);
  483. if (m_blitSupported)
  484. {
  485. 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);
  486. }
  487. m_rr = m_rng.gen()%255;
  488. m_gg = m_rng.gen()%255;
  489. m_bb = m_rng.gen()%255;
  490. }
  491. else
  492. {
  493. ++m_miss;
  494. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
  495. {
  496. face = m_quads.front();
  497. const Pack2D& rect = face.m_rect;
  498. updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
  499. if (m_blitSupported)
  500. {
  501. 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);
  502. }
  503. m_cube.clear(face);
  504. m_quads.pop_front();
  505. }
  506. }
  507. {
  508. // Fill rect.
  509. const uint32_t pitch = kTexture2dSize*4;
  510. const uint16_t tw = m_rng.gen()% kTexture2dSize;
  511. const uint16_t th = m_rng.gen()% kTexture2dSize;
  512. const uint16_t tx = m_rng.gen()%(kTexture2dSize-tw);
  513. const uint16_t ty = m_rng.gen()%(kTexture2dSize-th);
  514. uint8_t* dst = &m_texture2dData[(ty*kTexture2dSize+tx)*4];
  515. uint8_t* next = dst + pitch;
  516. // Using makeRef to pass texture memory without copying.
  517. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  518. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  519. {
  520. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  521. {
  522. dst[0] = m_bb;
  523. dst[1] = m_gg;
  524. dst[2] = m_rr;
  525. dst[3] = 255;
  526. }
  527. }
  528. // Pitch here makes possible to pass data from source to destination
  529. // without need for m_textures and allocated memory to be the same size.
  530. bgfx::updateTexture2D(m_texture2d, 0, 0, tx, ty, tw, th, mem, pitch);
  531. }
  532. }
  533. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  534. const bx::Vec3 eye = { 0.0f, 0.0f, -5.0f };
  535. float view[16];
  536. bx::mtxLookAt(view, eye, at);
  537. float proj[16];
  538. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  539. // Set view and projection matrix for view 0.
  540. bgfx::setViewTransform(0, view, proj);
  541. float projToScreen[16];
  542. bx::mtxSRT(projToScreen, float(m_width) * 0.5f, -float(m_height) * 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, float(m_width) * 0.5f, float(m_height) * 0.5f, 0.0f);
  543. float viewProj[16];
  544. bx::mtxMul(viewProj, view, proj);
  545. float worldToScreen[16];
  546. bx::mtxMul(worldToScreen, viewProj, projToScreen);
  547. // Update texturecube using compute shader
  548. if (bgfx::isValid(m_programCompute) )
  549. {
  550. bgfx::setImage(0, m_textureCube[2], 0, bgfx::Access::Write);
  551. bgfx::dispatch(0, m_programCompute, kTextureSide/16, kTextureSide/16);
  552. }
  553. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCubeFaceFb); ++ii)
  554. {
  555. bgfx::ViewId viewId = bgfx::ViewId(ii+2);
  556. bgfx::setViewFrameBuffer(viewId, m_textureCubeFaceFb[ii]);
  557. bx::Vec3 color = bx::add(s_faceColors[ii], bx::sin(time*4.0f)*0.25f);
  558. uint32_t colorRGB8 = 0
  559. | uint32_t(bx::toUnorm(color.x, 255.0f) ) << 24
  560. | uint32_t(bx::toUnorm(color.y, 255.0f) ) << 16
  561. | uint32_t(bx::toUnorm(color.z, 255.0f) ) << 8
  562. ;
  563. bgfx::setViewClear(viewId, BGFX_CLEAR_COLOR, colorRGB8);
  564. bgfx::setViewRect(viewId, 0,0,512,512);
  565. bgfx::touch(viewId);
  566. }
  567. static const char* descTextureCube[BX_COUNTOF(m_textureCube)] =
  568. {
  569. "updateTextureCube",
  570. "blit",
  571. "compute",
  572. "frameBuffer",
  573. };
  574. BX_STATIC_ASSERT(BX_COUNTOF(descTextureCube) == BX_COUNTOF(m_textureCube));
  575. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
  576. {
  577. if (bgfx::isValid(m_textureCube[ii]))
  578. {
  579. float mtx[16];
  580. bx::mtxSRT(mtx, 0.65f, 0.65f, 0.65f, time, time*0.37f, 0.0f, -2.5f +ii*1.8f, 0.0f, 0.0f);
  581. // Set model matrix for rendering.
  582. bgfx::setTransform(mtx);
  583. // Set vertex and index buffer.
  584. bgfx::setVertexBuffer(0, m_vbh);
  585. bgfx::setIndexBuffer(m_ibh);
  586. // Bind texture.
  587. bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
  588. // Set render states.
  589. bgfx::setState(BGFX_STATE_DEFAULT);
  590. // Submit primitive for rendering to view 0.
  591. bgfx::submit(0, m_program);
  592. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextureCube[ii]);
  593. }
  594. }
  595. // Set view and projection matrix for view 1.
  596. const uint32_t numColumns = BX_COUNTOF(m_textures) / 2;
  597. const float aspectRatio = float(m_height)/float(m_width);
  598. const float margin = 0.7f;
  599. const float sizeX = 0.5f * numColumns * 2.3f + margin;
  600. const float sizeY = sizeX * aspectRatio;
  601. const bgfx::Caps* caps = bgfx::getCaps();
  602. bx::mtxOrtho(proj, -sizeX, sizeX, sizeY, -sizeY, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
  603. bgfx::setViewTransform(1, NULL, proj);
  604. bx::mtxMul(worldToScreen, proj, projToScreen);
  605. float mtx[16];
  606. bx::mtxTranslate(mtx, -sizeX + margin + 1.0f, 1.9f, 0.0f);
  607. // Set model matrix for rendering.
  608. bgfx::setTransform(mtx);
  609. // Set vertex and index buffer.
  610. bgfx::setVertexBuffer(0, m_vbh);
  611. bgfx::setIndexBuffer(m_ibh);
  612. // Bind texture.
  613. bgfx::setTexture(0, s_texColor, m_texture2d);
  614. // Set render states.
  615. bgfx::setState(BGFX_STATE_DEFAULT);
  616. // Submit primitive for rendering to view 1.
  617. bgfx::submit(1, m_programCmp);
  618. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, "updateTexture2D");
  619. const float xpos = -sizeX + margin + 1.0f;
  620. static const char* descTextures[] =
  621. {
  622. "create\nbc1",
  623. "create\nbc2",
  624. "create\nbc3",
  625. "create\netc1",
  626. "create\netc2",
  627. "create\nptc12",
  628. "create\nptc14",
  629. "create\nptc22",
  630. "create\nptc24",
  631. "create\natc",
  632. "create\natci",
  633. "create\natce",
  634. "update\nbc1",
  635. "update\nbc2",
  636. "update\nbc3",
  637. "update\netc1",
  638. "update\netc2",
  639. "update\nptc12",
  640. "update\nptc14",
  641. "update\nptc22",
  642. "update\nptc24",
  643. "update\natc",
  644. "update\natci",
  645. "update\natce",
  646. };
  647. BX_STATIC_ASSERT(BX_COUNTOF(descTextures) == BX_COUNTOF(m_textures));
  648. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  649. {
  650. bx::mtxTranslate(mtx, xpos + (ii%numColumns) * 2.3f, sizeY - margin - 2.8f + (ii/numColumns) * 2.3f, 0.0f);
  651. // Set model matrix for rendering.
  652. bgfx::setTransform(mtx);
  653. // Set vertex and index buffer.
  654. bgfx::setVertexBuffer(0, m_vbh);
  655. bgfx::setIndexBuffer(m_ibh, 0, 6);
  656. // Bind texture.
  657. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  658. // Set render states.
  659. bgfx::setState(BGFX_STATE_DEFAULT);
  660. // Submit primitive for rendering to view 1.
  661. bgfx::submit(1, m_programCmp);
  662. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextures[ii]);
  663. }
  664. static const char* descTextures3d[] =
  665. {
  666. "Tex3D R8",
  667. "Tex3D R16F",
  668. "Tex3D R32F",
  669. };
  670. BX_STATIC_ASSERT(BX_COUNTOF(descTextures3d) == BX_COUNTOF(m_textures3d));
  671. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  672. {
  673. bx::mtxTranslate(mtx, xpos + (ii+(numColumns - m_numTextures3d)*0.5f)*2.3f, -sizeY + margin + 1.0f, 0.0f);
  674. // Set model matrix for rendering.
  675. bgfx::setTransform(mtx);
  676. // Set vertex and index buffer.
  677. bgfx::setVertexBuffer(0, m_vbh);
  678. bgfx::setIndexBuffer(m_ibh, 0, 6);
  679. // Bind texture.
  680. bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
  681. // Set render states.
  682. bgfx::setState(BGFX_STATE_DEFAULT);
  683. // Submit primitive for rendering to view 1.
  684. bgfx::submit(1, m_program3d);
  685. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextures3d[ii]);
  686. }
  687. static const char* descSampler[] =
  688. {
  689. "U_CLAMP\nV_CLAMP",
  690. "U_CLAMP\nV_WRAP",
  691. "U_WRAP\nV_CLAMP",
  692. "U_BORDER\nV_BORDER",
  693. "U_WRAP\nV_WRAP",
  694. };
  695. for (uint32_t ii = 0; ii < 5; ++ii)
  696. {
  697. bx::mtxTranslate(mtx, sizeX - margin - 1.0f, -sizeY + margin + 1.0f + ii*2.1f, 0.0f);
  698. // Set model matrix for rendering.
  699. bgfx::setTransform(mtx);
  700. // Set vertex and index buffer.
  701. bgfx::setVertexBuffer(0, m_vbh, 24, 4);
  702. bgfx::setIndexBuffer(m_ibh, 0, 6);
  703. // Bind texture.
  704. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  705. // Set render states.
  706. bgfx::setState(BGFX_STATE_DEFAULT);
  707. // Submit primitive for rendering to view 1.
  708. bgfx::submit(1, m_programCmp);
  709. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descSampler[ii]);
  710. }
  711. imguiEndFrame();
  712. // Advance to next frame. Rendering thread will be kicked to
  713. // process submitted rendering primitives.
  714. bgfx::frame();
  715. return true;
  716. }
  717. return false;
  718. }
  719. entry::MouseState m_mouseState;
  720. uint32_t m_width;
  721. uint32_t m_height;
  722. uint32_t m_debug;
  723. uint32_t m_reset;
  724. uint8_t* m_texture2dData;
  725. uint32_t m_numTextures3d;
  726. bool m_texture3DSupported;
  727. bool m_blitSupported;
  728. bool m_computeSupported;
  729. bool m_showDescriptions;
  730. std::list<PackCube> m_quads;
  731. RectPackCubeT<256> m_cube;
  732. int64_t m_updateTime;
  733. int64_t m_timeOffset;
  734. bx::RngMwc m_rng;
  735. uint32_t m_hit;
  736. uint32_t m_miss;
  737. uint8_t m_rr;
  738. uint8_t m_gg;
  739. uint8_t m_bb;
  740. bgfx::TextureHandle m_textures[24];
  741. bgfx::TextureHandle m_textures3d[3];
  742. bgfx::TextureHandle m_texture2d;
  743. bgfx::TextureHandle m_textureCube[4];
  744. bgfx::FrameBufferHandle m_textureCubeFaceFb[6];
  745. bgfx::IndexBufferHandle m_ibh;
  746. bgfx::VertexBufferHandle m_vbh;
  747. bgfx::ProgramHandle m_program3d;
  748. bgfx::ProgramHandle m_programCmp;
  749. bgfx::ProgramHandle m_programCompute;
  750. bgfx::ProgramHandle m_program;
  751. bgfx::UniformHandle u_time;
  752. bgfx::UniformHandle s_texColor;
  753. bgfx::UniformHandle s_texCube;
  754. };
  755. } // namespace
  756. ENTRY_IMPLEMENT_MAIN(
  757. ExampleUpdate
  758. , "08-update"
  759. , "Updating textures."
  760. , "https://bkaradzic.github.io/bgfx/examples.html#update"
  761. );