update.cpp 28 KB

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