update.cpp 28 KB

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