update.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * Copyright 2011-2025 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. 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. 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();
  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. 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::UniformFreq::Frame, 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. imguiCreate();
  371. m_frameTime.reset();
  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. m_frameTime.frame();
  450. const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
  451. bgfx::setFrameUniform(u_time, &time);
  452. imguiBeginFrame(m_mouseState.m_mx
  453. , m_mouseState.m_my
  454. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  455. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  456. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  457. , m_mouseState.m_mz
  458. , uint16_t(m_width)
  459. , uint16_t(m_height)
  460. );
  461. showExampleDialog(this);
  462. ImGui::SetNextWindowPos (ImVec2( 10.0f, 270.0f), ImGuiCond_FirstUseEver);
  463. ImGui::SetNextWindowSize(ImVec2(150.0f, 70.0f), ImGuiCond_FirstUseEver);
  464. ImGui::Begin(
  465. "Show descriptions"
  466. , NULL
  467. , ImGuiWindowFlags_NoResize
  468. );
  469. if (ImGui::Button(m_showDescriptions ? "On" : "Off"))
  470. {
  471. m_showDescriptions = !m_showDescriptions;
  472. }
  473. ImGui::End();
  474. float borderColor[4] =
  475. {
  476. float(m_rng.gen()%255)/255.0f,
  477. float(m_rng.gen()%255)/255.0f,
  478. float(m_rng.gen()%255)/255.0f,
  479. float(m_rng.gen()%255)/255.0f,
  480. };
  481. bgfx::setPaletteColor(1, borderColor);
  482. // Set view 0 and 1 viewport.
  483. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  484. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  485. // This dummy draw call is here to make sure that view 0 is cleared
  486. // if no other draw calls are submitted to view 0.
  487. bgfx::touch(0);
  488. if (bx::getNow() > 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. const float maxBorder = 64.0f;
  609. const uint16_t border = uint16_t(bx::abs(bx::sin(time * 4.0f)*0.5f+0.5f)*maxBorder);
  610. bgfx::setViewRect(viewId, border, border, 512-border*2, 512-border*2);
  611. bgfx::touch(viewId);
  612. }
  613. static const char* descTextureCube[BX_COUNTOF(m_textureCube)] =
  614. {
  615. "updateTextureCube",
  616. "blit",
  617. "compute",
  618. "frameBuffer",
  619. };
  620. static_assert(BX_COUNTOF(descTextureCube) == BX_COUNTOF(m_textureCube));
  621. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textureCube); ++ii)
  622. {
  623. if (bgfx::isValid(m_textureCube[ii]))
  624. {
  625. float mtx[16];
  626. bx::mtxSRT(mtx, 0.65f, 0.65f, 0.65f, time, time*0.37f, 0.0f, -2.5f +ii*1.8f, 0.0f, 0.0f);
  627. // Set model matrix for rendering.
  628. bgfx::setTransform(mtx);
  629. // Set vertex and index buffer.
  630. bgfx::setVertexBuffer(0, m_vbh);
  631. bgfx::setIndexBuffer(m_ibh);
  632. // Bind texture.
  633. bgfx::setTexture(0, s_texCube, m_textureCube[ii]);
  634. // Set render states.
  635. bgfx::setState(BGFX_STATE_DEFAULT);
  636. // Submit primitive for rendering to view 0.
  637. bgfx::submit(0, m_program);
  638. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextureCube[ii]);
  639. }
  640. }
  641. // Set view and projection matrix for view 1.
  642. const uint32_t numColumns = BX_COUNTOF(m_textures) / 2;
  643. const float aspectRatio = float(m_height)/float(m_width);
  644. const float margin = 0.7f;
  645. const float sizeX = 0.5f * numColumns * 2.3f + margin;
  646. const float sizeY = sizeX * aspectRatio;
  647. const bgfx::Caps* caps = bgfx::getCaps();
  648. bx::mtxOrtho(proj, -sizeX, sizeX, sizeY, -sizeY, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
  649. bgfx::setViewTransform(1, NULL, proj);
  650. bx::mtxMul(worldToScreen, proj, projToScreen);
  651. float mtx[16];
  652. bx::mtxTranslate(mtx, -sizeX + margin + 1.0f, 1.9f, 0.0f);
  653. // Set model matrix for rendering.
  654. bgfx::setTransform(mtx);
  655. // Set vertex and index buffer.
  656. bgfx::setVertexBuffer(0, m_vbh);
  657. bgfx::setIndexBuffer(m_ibh);
  658. // Bind texture.
  659. bgfx::setTexture(0, s_texColor, m_texture2d);
  660. // Set render states.
  661. bgfx::setState(BGFX_STATE_DEFAULT);
  662. // Submit primitive for rendering to view 1.
  663. bgfx::submit(1, m_programCmp);
  664. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, "updateTexture2D");
  665. const float xpos = -sizeX + margin + 1.0f;
  666. static const char* descTextures[] =
  667. {
  668. "create\nbc1",
  669. "create\nbc2",
  670. "create\nbc3",
  671. "create\netc1",
  672. "create\netc2",
  673. "create\nptc12",
  674. "create\nptc14",
  675. "create\nptc22",
  676. "create\nptc24",
  677. "create\natc",
  678. "create\natci",
  679. "create\natce",
  680. "update\nbc1",
  681. "update\nbc2",
  682. "update\nbc3",
  683. "update\netc1",
  684. "update\netc2",
  685. "update\nptc12",
  686. "update\nptc14",
  687. "update\nptc22",
  688. "update\nptc24",
  689. "update\natc",
  690. "update\natci",
  691. "update\natce",
  692. };
  693. static_assert(BX_COUNTOF(descTextures) == BX_COUNTOF(m_textures));
  694. for (uint32_t ii = 0; ii < BX_COUNTOF(m_textures); ++ii)
  695. {
  696. bx::mtxTranslate(mtx, xpos + (ii%numColumns) * 2.3f, sizeY - margin - 2.8f + (ii/numColumns) * 2.3f, 0.0f);
  697. // Set model matrix for rendering.
  698. bgfx::setTransform(mtx);
  699. // Set vertex and index buffer.
  700. bgfx::setVertexBuffer(0, m_vbh);
  701. bgfx::setIndexBuffer(m_ibh, 0, 6);
  702. // Bind texture.
  703. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  704. // Set render states.
  705. bgfx::setState(BGFX_STATE_DEFAULT);
  706. // Submit primitive for rendering to view 1.
  707. bgfx::submit(1, m_programCmp);
  708. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextures[ii]);
  709. }
  710. static const char* descTextures3d[] =
  711. {
  712. "Tex3D R8",
  713. "Tex3D R16F",
  714. "Tex3D R32F",
  715. };
  716. static_assert(BX_COUNTOF(descTextures3d) == BX_COUNTOF(m_textures3d));
  717. for (uint32_t ii = 0; ii < m_numTextures3d; ++ii)
  718. {
  719. bx::mtxTranslate(mtx, xpos + (ii+(numColumns - m_numTextures3d)*0.5f)*2.3f, -sizeY + margin + 1.0f, 0.0f);
  720. // Set model matrix for rendering.
  721. bgfx::setTransform(mtx);
  722. // Set vertex and index buffer.
  723. bgfx::setVertexBuffer(0, m_vbh);
  724. bgfx::setIndexBuffer(m_ibh, 0, 6);
  725. // Bind texture.
  726. bgfx::setTexture(0, s_texColor, m_textures3d[ii]);
  727. // Set render states.
  728. bgfx::setState(BGFX_STATE_DEFAULT);
  729. // Submit primitive for rendering to view 1.
  730. bgfx::submit(1, m_program3d);
  731. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descTextures3d[ii]);
  732. }
  733. static const char* descSampler[] =
  734. {
  735. "U_CLAMP\nV_CLAMP",
  736. "U_CLAMP\nV_WRAP",
  737. "U_WRAP\nV_CLAMP",
  738. "U_BORDER\nV_BORDER",
  739. "U_WRAP\nV_WRAP",
  740. };
  741. for (uint32_t ii = 0; ii < 5; ++ii)
  742. {
  743. bx::mtxTranslate(mtx, sizeX - margin - 1.0f, -sizeY + margin + 1.0f + ii*2.1f, 0.0f);
  744. // Set model matrix for rendering.
  745. bgfx::setTransform(mtx);
  746. // Set vertex and index buffer.
  747. bgfx::setVertexBuffer(0, m_vbh, 24, 4);
  748. bgfx::setIndexBuffer(m_ibh, 0, 6);
  749. // Bind texture.
  750. bgfx::setTexture(0, s_texColor, m_textures[ii]);
  751. // Set render states.
  752. bgfx::setState(BGFX_STATE_DEFAULT);
  753. // Submit primitive for rendering to view 1.
  754. bgfx::submit(1, m_programCmp);
  755. ImGuiDescription(mtx[12], mtx[13], mtx[14], worldToScreen, descSampler[ii]);
  756. }
  757. if (m_blitSupported)
  758. {
  759. bgfx::blit(1, m_blitTestA, 0, 0, m_blitTestB, 0, 0);
  760. bgfx::blit(1, m_blitTestC, 0, 0, m_blitTestA, 0, 0);
  761. bgfx::blit(1, m_blitTestA, 0, 0, m_blitTestB, 0, 0);
  762. bgfx::blit(1, m_blitTestB, 0, 0, m_blitTestC, 0, 0);
  763. bgfx::blit(1, m_blitTestA, 0, 0, m_blitTestB, 0, 0);
  764. bgfx::blit(1, m_blitTestB, 0, 0, m_blitTestA, 0, 0);
  765. bgfx::blit(1, m_blitTestB, 0, 0, m_blitTestA, 0, 0);
  766. bgfx::blit(1, m_blitTestC, 0, 0, m_blitTestB, 0, 0);
  767. }
  768. imguiEndFrame();
  769. // Advance to next frame. Rendering thread will be kicked to
  770. // process submitted rendering primitives.
  771. bgfx::frame();
  772. return true;
  773. }
  774. return false;
  775. }
  776. entry::MouseState m_mouseState;
  777. uint32_t m_width;
  778. uint32_t m_height;
  779. uint32_t m_debug;
  780. uint32_t m_reset;
  781. uint8_t* m_texture2dData;
  782. uint32_t m_numTextures3d;
  783. bool m_texture3DSupported;
  784. bool m_blitSupported;
  785. bool m_computeSupported;
  786. bool m_showDescriptions;
  787. std::list<PackCube> m_quads;
  788. RectPackCubeT<256> m_cube;
  789. bx::Ticks m_updateTime = bx::InitZero;
  790. bx::RngMwc m_rng;
  791. uint32_t m_hit;
  792. uint32_t m_miss;
  793. uint8_t m_rr;
  794. uint8_t m_gg;
  795. uint8_t m_bb;
  796. bgfx::TextureHandle m_textures[24];
  797. bgfx::TextureHandle m_textures3d[3];
  798. bgfx::TextureHandle m_texture2d;
  799. bgfx::TextureHandle m_textureCube[4];
  800. bgfx::TextureHandle m_blitTestA;
  801. bgfx::TextureHandle m_blitTestB;
  802. bgfx::TextureHandle m_blitTestC;
  803. bgfx::FrameBufferHandle m_textureCubeFaceFb[6];
  804. bgfx::IndexBufferHandle m_ibh;
  805. bgfx::VertexBufferHandle m_vbh;
  806. bgfx::ProgramHandle m_program3d;
  807. bgfx::ProgramHandle m_programCmp;
  808. bgfx::ProgramHandle m_programCompute;
  809. bgfx::ProgramHandle m_program;
  810. bgfx::UniformHandle u_time;
  811. bgfx::UniformHandle s_texColor;
  812. bgfx::UniformHandle s_texCube;
  813. FrameTime m_frameTime;
  814. };
  815. } // namespace
  816. ENTRY_IMPLEMENT_MAIN(
  817. ExampleUpdate
  818. , "08-update"
  819. , "Updating textures."
  820. , "https://bkaradzic.github.io/bgfx/examples.html#update"
  821. );