update.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * Copyright 2011-2021 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_blitTestA = bgfx::createTexture2D(16, 16, false, 1, bgfx::TextureFormat::Enum::RGBA8, BGFX_TEXTURE_BLIT_DST);
  355. m_blitTestB = bgfx::createTexture2D(16, 16, false, 1, bgfx::TextureFormat::Enum::RGBA8, BGFX_TEXTURE_BLIT_DST);
  356. m_blitTestC = bgfx::createTexture2D(16, 16, false, 1, bgfx::TextureFormat::Enum::RGBA8, BGFX_TEXTURE_BLIT_DST);
  357. m_rr = m_rng.gen()%255;
  358. m_gg = m_rng.gen()%255;
  359. m_bb = m_rng.gen()%255;
  360. m_hit = 0;
  361. m_miss = 0;
  362. m_updateTime = 0;
  363. m_timeOffset = bx::getHPCounter();
  364. imguiCreate();
  365. }
  366. virtual int shutdown() override
  367. {
  368. imguiDestroy();
  369. // m_texture2dData is managed from main thread, and it's passed to renderer
  370. // just as MemoryRef. At this point render might be using it. We must wait
  371. // previous frame to finish before we can free it.
  372. bgfx::frame();
  373. // Cleanup.
  374. free(m_texture2dData);
  375. bgfx::destroy(m_blitTestA);
  376. bgfx::destroy(m_blitTestB);
  377. bgfx::destroy(m_blitTestC);
  378. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  379. {
  380. bgfx::destroy(m_textures[ii]);
  381. }
  382. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  383. {
  384. bgfx::destroy(m_textures3d[ii]);
  385. }
  386. bgfx::destroy(m_texture2d);
  387. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
  388. {
  389. if (bgfx::isValid(m_textureCube[ii]))
  390. {
  391. bgfx::destroy(m_textureCube[ii]);
  392. }
  393. }
  394. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCubeFaceFb); ++ii)
  395. {
  396. if (bgfx::isValid(m_textureCubeFaceFb[ii]))
  397. {
  398. bgfx::destroy(m_textureCubeFaceFb[ii]);
  399. }
  400. }
  401. bgfx::destroy(m_ibh);
  402. bgfx::destroy(m_vbh);
  403. if (bgfx::isValid(m_program3d) )
  404. {
  405. bgfx::destroy(m_program3d);
  406. }
  407. bgfx::destroy(m_programCmp);
  408. if (bgfx::isValid(m_programCompute) )
  409. {
  410. bgfx::destroy(m_programCompute);
  411. }
  412. bgfx::destroy(m_program);
  413. bgfx::destroy(u_time);
  414. bgfx::destroy(s_texColor);
  415. bgfx::destroy(s_texCube);
  416. // Shutdown bgfx.
  417. bgfx::shutdown();
  418. return 0;
  419. }
  420. void ImGuiDescription(float _x, float _y, float _z, const float* _worldToScreen, const char* _text)
  421. {
  422. if (m_showDescriptions)
  423. {
  424. float worldPos[4] = { _x, _y, _z, 1.0f };
  425. float screenPos[4];
  426. bx::vec4MulMtx(screenPos, worldPos, _worldToScreen);
  427. ImGui::SetNextWindowPos(ImVec2(screenPos[0] / screenPos[3], screenPos[1] / screenPos[3]), 0, ImVec2(0.5f, 0.5f));
  428. ImGuiWindowFlags flags = ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize;
  429. ImGui::SetNextWindowBgAlpha(0.5f);
  430. ImGui::Begin(_text, NULL, flags);
  431. ImGui::Text("%s", _text);
  432. ImGui::End();
  433. }
  434. }
  435. bool update() override
  436. {
  437. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  438. {
  439. imguiBeginFrame(m_mouseState.m_mx
  440. , m_mouseState.m_my
  441. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  442. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  443. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  444. , m_mouseState.m_mz
  445. , uint16_t(m_width)
  446. , uint16_t(m_height)
  447. );
  448. showExampleDialog(this);
  449. ImGui::SetNextWindowPos (ImVec2( 10.0f, 270.0f), ImGuiCond_FirstUseEver);
  450. ImGui::SetNextWindowSize(ImVec2(150.0f, 70.0f), ImGuiCond_FirstUseEver);
  451. ImGui::Begin(
  452. "Show descriptions"
  453. , NULL
  454. , ImGuiWindowFlags_NoResize
  455. );
  456. if (ImGui::Button(m_showDescriptions ? "On" : "Off"))
  457. {
  458. m_showDescriptions = !m_showDescriptions;
  459. }
  460. ImGui::End();
  461. float borderColor[4] =
  462. {
  463. float(m_rng.gen()%255)/255.0f,
  464. float(m_rng.gen()%255)/255.0f,
  465. float(m_rng.gen()%255)/255.0f,
  466. float(m_rng.gen()%255)/255.0f,
  467. };
  468. bgfx::setPaletteColor(1, borderColor);
  469. // Set view 0 and 1 viewport.
  470. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  471. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  472. // This dummy draw call is here to make sure that view 0 is cleared
  473. // if no other draw calls are submitted to view 0.
  474. bgfx::touch(0);
  475. int64_t now = bx::getHPCounter();
  476. float time = (float)( (now - m_timeOffset)/double(bx::getHPFrequency() ) );
  477. bgfx::setUniform(u_time, &time);
  478. if (now > m_updateTime)
  479. {
  480. PackCube face;
  481. uint16_t bw = bx::max<uint16_t>(1, m_rng.gen()%(kTextureSide/4) );
  482. uint16_t bh = bx::max<uint16_t>(1, m_rng.gen()%(kTextureSide/4) );
  483. if (m_cube.find(bw, bh, face) )
  484. {
  485. m_quads.push_back(face);
  486. ++m_hit;
  487. const Pack2D& rect = face.m_rect;
  488. 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);
  489. if (m_blitSupported)
  490. {
  491. bgfx::blit(
  492. 0
  493. , m_textureCube[1]
  494. , 0
  495. , rect.m_x
  496. , rect.m_y
  497. , face.m_side
  498. , m_textureCube[0]
  499. , 0
  500. , rect.m_x
  501. , rect.m_y
  502. , face.m_side
  503. , rect.m_width
  504. , rect.m_height
  505. );
  506. }
  507. m_rr = m_rng.gen()%255;
  508. m_gg = m_rng.gen()%255;
  509. m_bb = m_rng.gen()%255;
  510. }
  511. else
  512. {
  513. ++m_miss;
  514. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)m_quads.size() ); ii < num; ++ii)
  515. {
  516. face = m_quads.front();
  517. const Pack2D& rect = face.m_rect;
  518. updateTextureCubeRectBgra8(m_textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0, 0);
  519. if (m_blitSupported)
  520. {
  521. bgfx::blit(
  522. 0
  523. , m_textureCube[1]
  524. , 0
  525. , rect.m_x
  526. , rect.m_y
  527. , face.m_side
  528. , m_textureCube[0]
  529. , 0
  530. , rect.m_x
  531. , rect.m_y
  532. , face.m_side
  533. , rect.m_width
  534. , rect.m_height
  535. );
  536. }
  537. m_cube.clear(face);
  538. m_quads.pop_front();
  539. }
  540. }
  541. {
  542. // Fill rect.
  543. const uint32_t pitch = kTexture2dSize*4;
  544. const uint16_t tw = m_rng.gen()% kTexture2dSize;
  545. const uint16_t th = m_rng.gen()% kTexture2dSize;
  546. const uint16_t tx = m_rng.gen()%(kTexture2dSize-tw);
  547. const uint16_t ty = m_rng.gen()%(kTexture2dSize-th);
  548. uint8_t* dst = &m_texture2dData[(ty*kTexture2dSize+tx)*4];
  549. uint8_t* next = dst + pitch;
  550. // Using makeRef to pass texture memory without copying.
  551. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  552. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  553. {
  554. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  555. {
  556. dst[0] = m_bb;
  557. dst[1] = m_gg;
  558. dst[2] = m_rr;
  559. dst[3] = 255;
  560. }
  561. }
  562. // Pitch here makes possible to pass data from source to destination
  563. // without need for m_textures and allocated memory to be the same size.
  564. bgfx::updateTexture2D(m_texture2d, 0, 0, tx, ty, tw, th, mem, pitch);
  565. }
  566. }
  567. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  568. const bx::Vec3 eye = { 0.0f, 0.0f, -5.0f };
  569. float view[16];
  570. bx::mtxLookAt(view, eye, at);
  571. float proj[16];
  572. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  573. // Set view and projection matrix for view 0.
  574. bgfx::setViewTransform(0, view, proj);
  575. float projToScreen[16];
  576. 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);
  577. float viewProj[16];
  578. bx::mtxMul(viewProj, view, proj);
  579. float worldToScreen[16];
  580. bx::mtxMul(worldToScreen, viewProj, projToScreen);
  581. // Update texturecube using compute shader
  582. if (bgfx::isValid(m_programCompute) )
  583. {
  584. bgfx::setImage(0, m_textureCube[2], 0, bgfx::Access::Write);
  585. bgfx::dispatch(0, m_programCompute, kTextureSide/16, kTextureSide/16);
  586. }
  587. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCubeFaceFb); ++ii)
  588. {
  589. bgfx::ViewId viewId = bgfx::ViewId(ii+2);
  590. bgfx::setViewFrameBuffer(viewId, m_textureCubeFaceFb[ii]);
  591. bx::Vec3 color = bx::add(s_faceColors[ii], bx::sin(time*4.0f)*0.25f);
  592. uint32_t colorRGB8 = 0
  593. | uint32_t(bx::toUnorm(color.x, 255.0f) ) << 24
  594. | uint32_t(bx::toUnorm(color.y, 255.0f) ) << 16
  595. | uint32_t(bx::toUnorm(color.z, 255.0f) ) << 8
  596. ;
  597. bgfx::setViewClear(viewId, BGFX_CLEAR_COLOR, colorRGB8);
  598. bgfx::setViewRect(viewId, 0,0,512,512);
  599. bgfx::touch(viewId);
  600. }
  601. static const char* descTextureCube[BX_COUNTOF(m_textureCube)] =
  602. {
  603. "updateTextureCube",
  604. "blit",
  605. "compute",
  606. "frameBuffer",
  607. };
  608. BX_STATIC_ASSERT(BX_COUNTOF(descTextureCube) == BX_COUNTOF(m_textureCube));
  609. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
  610. {
  611. if (bgfx::isValid(m_textureCube[ii]))
  612. {
  613. float mtx[16];
  614. bx::mtxSRT(mtx, 0.65f, 0.65f, 0.65f, time, time*0.37f, 0.0f, -2.5f +ii*1.8f, 0.0f, 0.0f);
  615. // Set model matrix for rendering.
  616. bgfx::setTransform(mtx);
  617. // Set vertex and index buffer.
  618. bgfx::setVertexBuffer(0, m_vbh);
  619. bgfx::setIndexBuffer(m_ibh);
  620. // Bind texture.
  621. bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
  622. // Set render states.
  623. bgfx::setState(BGFX_STATE_DEFAULT);
  624. // Submit primitive for rendering to view 0.
  625. bgfx::submit(0, m_program);
  626. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextureCube[ii]);
  627. }
  628. }
  629. // Set view and projection matrix for view 1.
  630. const uint32_t numColumns = BX_COUNTOF(m_textures) / 2;
  631. const float aspectRatio = float(m_height)/float(m_width);
  632. const float margin = 0.7f;
  633. const float sizeX = 0.5f * numColumns * 2.3f + margin;
  634. const float sizeY = sizeX * aspectRatio;
  635. const bgfx::Caps* caps = bgfx::getCaps();
  636. bx::mtxOrtho(proj, -sizeX, sizeX, sizeY, -sizeY, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
  637. bgfx::setViewTransform(1, NULL, proj);
  638. bx::mtxMul(worldToScreen, proj, projToScreen);
  639. float mtx[16];
  640. bx::mtxTranslate(mtx, -sizeX + margin + 1.0f, 1.9f, 0.0f);
  641. // Set model matrix for rendering.
  642. bgfx::setTransform(mtx);
  643. // Set vertex and index buffer.
  644. bgfx::setVertexBuffer(0, m_vbh);
  645. bgfx::setIndexBuffer(m_ibh);
  646. // Bind texture.
  647. bgfx::setTexture(0, s_texColor, m_texture2d);
  648. // Set render states.
  649. bgfx::setState(BGFX_STATE_DEFAULT);
  650. // Submit primitive for rendering to view 1.
  651. bgfx::submit(1, m_programCmp);
  652. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, "updateTexture2D");
  653. const float xpos = -sizeX + margin + 1.0f;
  654. static const char* descTextures[] =
  655. {
  656. "create\nbc1",
  657. "create\nbc2",
  658. "create\nbc3",
  659. "create\netc1",
  660. "create\netc2",
  661. "create\nptc12",
  662. "create\nptc14",
  663. "create\nptc22",
  664. "create\nptc24",
  665. "create\natc",
  666. "create\natci",
  667. "create\natce",
  668. "update\nbc1",
  669. "update\nbc2",
  670. "update\nbc3",
  671. "update\netc1",
  672. "update\netc2",
  673. "update\nptc12",
  674. "update\nptc14",
  675. "update\nptc22",
  676. "update\nptc24",
  677. "update\natc",
  678. "update\natci",
  679. "update\natce",
  680. };
  681. BX_STATIC_ASSERT(BX_COUNTOF(descTextures) == BX_COUNTOF(m_textures));
  682. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  683. {
  684. bx::mtxTranslate(mtx, xpos + (ii%numColumns) * 2.3f, sizeY - margin - 2.8f + (ii/numColumns) * 2.3f, 0.0f);
  685. // Set model matrix for rendering.
  686. bgfx::setTransform(mtx);
  687. // Set vertex and index buffer.
  688. bgfx::setVertexBuffer(0, m_vbh);
  689. bgfx::setIndexBuffer(m_ibh, 0, 6);
  690. // Bind texture.
  691. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  692. // Set render states.
  693. bgfx::setState(BGFX_STATE_DEFAULT);
  694. // Submit primitive for rendering to view 1.
  695. bgfx::submit(1, m_programCmp);
  696. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextures[ii]);
  697. }
  698. static const char* descTextures3d[] =
  699. {
  700. "Tex3D R8",
  701. "Tex3D R16F",
  702. "Tex3D R32F",
  703. };
  704. BX_STATIC_ASSERT(BX_COUNTOF(descTextures3d) == BX_COUNTOF(m_textures3d));
  705. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  706. {
  707. bx::mtxTranslate(mtx, xpos + (ii+(numColumns - m_numTextures3d)*0.5f)*2.3f, -sizeY + margin + 1.0f, 0.0f);
  708. // Set model matrix for rendering.
  709. bgfx::setTransform(mtx);
  710. // Set vertex and index buffer.
  711. bgfx::setVertexBuffer(0, m_vbh);
  712. bgfx::setIndexBuffer(m_ibh, 0, 6);
  713. // Bind texture.
  714. bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
  715. // Set render states.
  716. bgfx::setState(BGFX_STATE_DEFAULT);
  717. // Submit primitive for rendering to view 1.
  718. bgfx::submit(1, m_program3d);
  719. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextures3d[ii]);
  720. }
  721. static const char* descSampler[] =
  722. {
  723. "U_CLAMP\nV_CLAMP",
  724. "U_CLAMP\nV_WRAP",
  725. "U_WRAP\nV_CLAMP",
  726. "U_BORDER\nV_BORDER",
  727. "U_WRAP\nV_WRAP",
  728. };
  729. for (uint32_t ii = 0; ii < 5; ++ii)
  730. {
  731. bx::mtxTranslate(mtx, sizeX - margin - 1.0f, -sizeY + margin + 1.0f + ii*2.1f, 0.0f);
  732. // Set model matrix for rendering.
  733. bgfx::setTransform(mtx);
  734. // Set vertex and index buffer.
  735. bgfx::setVertexBuffer(0, m_vbh, 24, 4);
  736. bgfx::setIndexBuffer(m_ibh, 0, 6);
  737. // Bind texture.
  738. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  739. // Set render states.
  740. bgfx::setState(BGFX_STATE_DEFAULT);
  741. // Submit primitive for rendering to view 1.
  742. bgfx::submit(1, m_programCmp);
  743. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descSampler[ii]);
  744. }
  745. bgfx::blit(1, m_blitTestA, 0, 0, m_blitTestB, 0, 0);
  746. bgfx::blit(1, m_blitTestC, 0, 0, m_blitTestA, 0, 0);
  747. bgfx::blit(1, m_blitTestA, 0, 0, m_blitTestB, 0, 0);
  748. bgfx::blit(1, m_blitTestB, 0, 0, m_blitTestC, 0, 0);
  749. bgfx::blit(1, m_blitTestA, 0, 0, m_blitTestB, 0, 0);
  750. bgfx::blit(1, m_blitTestB, 0, 0, m_blitTestA, 0, 0);
  751. bgfx::blit(1, m_blitTestB, 0, 0, m_blitTestA, 0, 0);
  752. bgfx::blit(1, m_blitTestC, 0, 0, m_blitTestB, 0, 0);
  753. imguiEndFrame();
  754. // Advance to next frame. Rendering thread will be kicked to
  755. // process submitted rendering primitives.
  756. bgfx::frame();
  757. return true;
  758. }
  759. return false;
  760. }
  761. entry::MouseState m_mouseState;
  762. uint32_t m_width;
  763. uint32_t m_height;
  764. uint32_t m_debug;
  765. uint32_t m_reset;
  766. uint8_t* m_texture2dData;
  767. uint32_t m_numTextures3d;
  768. bool m_texture3DSupported;
  769. bool m_blitSupported;
  770. bool m_computeSupported;
  771. bool m_showDescriptions;
  772. std::list<PackCube> m_quads;
  773. RectPackCubeT<256> m_cube;
  774. int64_t m_updateTime;
  775. int64_t m_timeOffset;
  776. bx::RngMwc m_rng;
  777. uint32_t m_hit;
  778. uint32_t m_miss;
  779. uint8_t m_rr;
  780. uint8_t m_gg;
  781. uint8_t m_bb;
  782. bgfx::TextureHandle m_textures[24];
  783. bgfx::TextureHandle m_textures3d[3];
  784. bgfx::TextureHandle m_texture2d;
  785. bgfx::TextureHandle m_textureCube[4];
  786. bgfx::TextureHandle m_blitTestA;
  787. bgfx::TextureHandle m_blitTestB;
  788. bgfx::TextureHandle m_blitTestC;
  789. bgfx::FrameBufferHandle m_textureCubeFaceFb[6];
  790. bgfx::IndexBufferHandle m_ibh;
  791. bgfx::VertexBufferHandle m_vbh;
  792. bgfx::ProgramHandle m_program3d;
  793. bgfx::ProgramHandle m_programCmp;
  794. bgfx::ProgramHandle m_programCompute;
  795. bgfx::ProgramHandle m_program;
  796. bgfx::UniformHandle u_time;
  797. bgfx::UniformHandle s_texColor;
  798. bgfx::UniformHandle s_texCube;
  799. };
  800. } // namespace
  801. ENTRY_IMPLEMENT_MAIN(
  802. ExampleUpdate
  803. , "08-update"
  804. , "Updating textures."
  805. , "https://bkaradzic.github.io/bgfx/examples.html#update"
  806. );