update.cpp 28 KB

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