cube_atlas.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include <bgfx/bgfx.h>
  7. #include <limits.h> // INT_MAX
  8. #include <memory.h> // memset
  9. #include <vector>
  10. #include "cube_atlas.h"
  11. class RectanglePacker
  12. {
  13. public:
  14. RectanglePacker();
  15. RectanglePacker(uint32_t _width, uint32_t _height);
  16. /// non constructor initialization
  17. void init(uint32_t _width, uint32_t _height);
  18. /// find a suitable position for the given rectangle
  19. /// @return true if the rectangle can be added, false otherwise
  20. bool addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY);
  21. /// return the used surface in squared unit
  22. uint32_t getUsedSurface()
  23. {
  24. return m_usedSpace;
  25. }
  26. /// return the total available surface in squared unit
  27. uint32_t getTotalSurface()
  28. {
  29. return m_width * m_height;
  30. }
  31. /// return the usage ratio of the available surface [0:1]
  32. float getUsageRatio();
  33. /// reset to initial state
  34. void clear();
  35. private:
  36. int32_t fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height);
  37. /// Merges all skyline nodes that are at the same level.
  38. void merge();
  39. struct Node
  40. {
  41. Node(int16_t _x, int16_t _y, int16_t _width) : x(_x), y(_y), width(_width)
  42. {
  43. }
  44. int16_t x; //< The starting x-coordinate (leftmost).
  45. int16_t y; //< The y-coordinate of the skyline level line.
  46. int32_t width; //< The line _width. The ending coordinate (inclusive) will be x+width-1.
  47. };
  48. uint32_t m_width; //< width (in pixels) of the underlying texture
  49. uint32_t m_height; //< height (in pixels) of the underlying texture
  50. uint32_t m_usedSpace; //< Surface used in squared pixel
  51. std::vector<Node> m_skyline; //< node of the skyline algorithm
  52. };
  53. RectanglePacker::RectanglePacker()
  54. : m_width(0)
  55. , m_height(0)
  56. , m_usedSpace(0)
  57. {
  58. }
  59. RectanglePacker::RectanglePacker(uint32_t _width, uint32_t _height)
  60. : m_width(_width)
  61. , m_height(_height)
  62. , m_usedSpace(0)
  63. {
  64. // We want a one pixel border around the whole atlas to avoid any artefact when
  65. // sampling texture
  66. m_skyline.push_back(Node(1, 1, uint16_t(_width - 2) ) );
  67. }
  68. void RectanglePacker::init(uint32_t _width, uint32_t _height)
  69. {
  70. BX_CHECK(_width > 2, "_width must be > 2");
  71. BX_CHECK(_height > 2, "_height must be > 2");
  72. m_width = _width;
  73. m_height = _height;
  74. m_usedSpace = 0;
  75. m_skyline.clear();
  76. // We want a one pixel border around the whole atlas to avoid any artifact when
  77. // sampling texture
  78. m_skyline.push_back(Node(1, 1, uint16_t(_width - 2) ) );
  79. }
  80. bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY)
  81. {
  82. int best_height, best_index;
  83. int32_t best_width;
  84. Node* node;
  85. Node* prev;
  86. _outX = 0;
  87. _outY = 0;
  88. best_height = INT_MAX;
  89. best_index = -1;
  90. best_width = INT_MAX;
  91. for (uint16_t ii = 0, num = uint16_t(m_skyline.size() ); ii < num; ++ii)
  92. {
  93. int32_t yy = fit(ii, _width, _height);
  94. if (yy >= 0)
  95. {
  96. node = &m_skyline[ii];
  97. if ( ( (yy + _height) < best_height)
  98. || ( ( (yy + _height) == best_height) && (node->width < best_width) ) )
  99. {
  100. best_height = uint16_t(yy) + _height;
  101. best_index = ii;
  102. best_width = node->width;
  103. _outX = node->x;
  104. _outY = uint16_t(yy);
  105. }
  106. }
  107. }
  108. if (best_index == -1)
  109. {
  110. return false;
  111. }
  112. Node newNode(_outX, _outY + _height, _width);
  113. m_skyline.insert(m_skyline.begin() + best_index, newNode);
  114. for (uint16_t ii = uint16_t(best_index + 1), num = uint16_t(m_skyline.size() ); ii < num; ++ii)
  115. {
  116. node = &m_skyline[ii];
  117. prev = &m_skyline[ii - 1];
  118. if (node->x < (prev->x + prev->width) )
  119. {
  120. uint16_t shrink = uint16_t(prev->x + prev->width - node->x);
  121. node->x += shrink;
  122. node->width -= shrink;
  123. if (node->width <= 0)
  124. {
  125. m_skyline.erase(m_skyline.begin() + ii);
  126. --ii;
  127. --num;
  128. }
  129. else
  130. {
  131. break;
  132. }
  133. }
  134. else
  135. {
  136. break;
  137. }
  138. }
  139. merge();
  140. m_usedSpace += _width * _height;
  141. return true;
  142. }
  143. float RectanglePacker::getUsageRatio()
  144. {
  145. uint32_t total = m_width * m_height;
  146. if (total > 0)
  147. {
  148. return (float)m_usedSpace / (float)total;
  149. }
  150. return 0.0f;
  151. }
  152. void RectanglePacker::clear()
  153. {
  154. m_skyline.clear();
  155. m_usedSpace = 0;
  156. // We want a one pixel border around the whole atlas to avoid any artefact when
  157. // sampling texture
  158. m_skyline.push_back(Node(1, 1, uint16_t(m_width - 2) ) );
  159. }
  160. int32_t RectanglePacker::fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height)
  161. {
  162. int32_t width = _width;
  163. int32_t height = _height;
  164. const Node& baseNode = m_skyline[_skylineNodeIndex];
  165. int32_t xx = baseNode.x, yy;
  166. int32_t widthLeft = width;
  167. int32_t ii = _skylineNodeIndex;
  168. if ( (xx + width) > (int32_t)(m_width - 1) )
  169. {
  170. return -1;
  171. }
  172. yy = baseNode.y;
  173. while (widthLeft > 0)
  174. {
  175. const Node& node = m_skyline[ii];
  176. if (node.y > yy)
  177. {
  178. yy = node.y;
  179. }
  180. if ( (yy + height) > (int32_t)(m_height - 1) )
  181. {
  182. return -1;
  183. }
  184. widthLeft -= node.width;
  185. ++ii;
  186. }
  187. return yy;
  188. }
  189. void RectanglePacker::merge()
  190. {
  191. Node* node;
  192. Node* next;
  193. uint32_t ii;
  194. for (ii = 0; ii < m_skyline.size() - 1; ++ii)
  195. {
  196. node = (Node*) &m_skyline[ii];
  197. next = (Node*) &m_skyline[ii + 1];
  198. if (node->y == next->y)
  199. {
  200. node->width += next->width;
  201. m_skyline.erase(m_skyline.begin() + ii + 1);
  202. --ii;
  203. }
  204. }
  205. }
  206. struct Atlas::PackedLayer
  207. {
  208. RectanglePacker packer;
  209. AtlasRegion faceRegion;
  210. };
  211. Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount)
  212. : m_usedLayers(0)
  213. , m_usedFaces(0)
  214. , m_textureSize(_textureSize)
  215. , m_regionCount(0)
  216. , m_maxRegionCount(_maxRegionsCount)
  217. {
  218. BX_CHECK(_textureSize >= 64 && _textureSize <= 4096, "Invalid _textureSize %d.", _textureSize);
  219. BX_CHECK(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000, "Invalid _maxRegionsCount %d.", _maxRegionsCount);
  220. init();
  221. m_layers = new PackedLayer[24];
  222. for (int ii = 0; ii < 24; ++ii)
  223. {
  224. m_layers[ii].packer.init(_textureSize, _textureSize);
  225. }
  226. m_regions = new AtlasRegion[_maxRegionsCount];
  227. m_textureBuffer = new uint8_t[ _textureSize * _textureSize * 6 * 4 ];
  228. memset(m_textureBuffer, 0, _textureSize * _textureSize * 6 * 4);
  229. m_textureHandle = bgfx::createTextureCube(_textureSize
  230. , 1
  231. , bgfx::TextureFormat::BGRA8
  232. );
  233. }
  234. Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer, uint16_t _regionCount, const uint8_t* _regionBuffer, uint16_t _maxRegionsCount)
  235. : m_usedLayers(24)
  236. , m_usedFaces(6)
  237. , m_textureSize(_textureSize)
  238. , m_regionCount(_regionCount)
  239. , m_maxRegionCount(_regionCount < _maxRegionsCount ? _regionCount : _maxRegionsCount)
  240. {
  241. BX_CHECK(_regionCount <= 64 && _maxRegionsCount <= 4096, "_regionCount %d, _maxRegionsCount %d", _regionCount, _maxRegionsCount);
  242. init();
  243. m_regions = new AtlasRegion[_regionCount];
  244. m_textureBuffer = new uint8_t[getTextureBufferSize()];
  245. memcpy(m_regions, _regionBuffer, _regionCount * sizeof(AtlasRegion) );
  246. memcpy(m_textureBuffer, _textureBuffer, getTextureBufferSize() );
  247. m_textureHandle = bgfx::createTextureCube(_textureSize
  248. , 1
  249. , bgfx::TextureFormat::BGRA8
  250. , BGFX_TEXTURE_NONE
  251. , bgfx::makeRef(m_textureBuffer, getTextureBufferSize() )
  252. );
  253. }
  254. Atlas::~Atlas()
  255. {
  256. bgfx::destroyTexture(m_textureHandle);
  257. delete [] m_layers;
  258. delete [] m_regions;
  259. delete [] m_textureBuffer;
  260. }
  261. void Atlas::init()
  262. {
  263. m_texelSize = float(UINT16_MAX) / float(m_textureSize);
  264. float texelHalf = m_texelSize/2.0f;
  265. switch (bgfx::getRendererType() )
  266. {
  267. case bgfx::RendererType::Direct3D9:
  268. m_texelOffset[0] = 0.0f;
  269. m_texelOffset[1] = 0.0f;
  270. break;
  271. case bgfx::RendererType::Direct3D11:
  272. case bgfx::RendererType::Direct3D12:
  273. m_texelOffset[0] = texelHalf;
  274. m_texelOffset[1] = texelHalf;
  275. break;
  276. default:
  277. m_texelOffset[0] = texelHalf;
  278. m_texelOffset[1] = -texelHalf;
  279. break;
  280. }
  281. }
  282. uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type, uint16_t outline)
  283. {
  284. if (m_regionCount >= m_maxRegionCount)
  285. {
  286. return UINT16_MAX;
  287. }
  288. uint16_t xx = 0;
  289. uint16_t yy = 0;
  290. uint32_t idx = 0;
  291. while (idx < m_usedLayers)
  292. {
  293. if (m_layers[idx].faceRegion.getType() == _type
  294. && m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
  295. {
  296. break;
  297. }
  298. idx++;
  299. }
  300. if (idx >= m_usedLayers)
  301. {
  302. if ( (idx + _type) > 24
  303. || m_usedFaces >= 6)
  304. {
  305. return UINT16_MAX;
  306. }
  307. for (int ii = 0; ii < _type; ++ii)
  308. {
  309. AtlasRegion& region = m_layers[idx + ii].faceRegion;
  310. region.x = 0;
  311. region.y = 0;
  312. region.width = m_textureSize;
  313. region.height = m_textureSize;
  314. region.setMask(_type, m_usedFaces, ii);
  315. }
  316. m_usedLayers += _type;
  317. m_usedFaces++;
  318. if (!m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
  319. {
  320. return UINT16_MAX;
  321. }
  322. }
  323. AtlasRegion& region = m_regions[m_regionCount];
  324. region.x = xx;
  325. region.y = yy;
  326. region.width = _width;
  327. region.height = _height;
  328. region.mask = m_layers[idx].faceRegion.mask;
  329. updateRegion(region, _bitmapBuffer);
  330. region.x += outline;
  331. region.y += outline;
  332. region.width -= (outline * 2);
  333. region.height -= (outline * 2);
  334. return m_regionCount++;
  335. }
  336. void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer)
  337. {
  338. uint32_t size = _region.width * _region.height * 4;
  339. if (0 < size)
  340. {
  341. const bgfx::Memory* mem = bgfx::alloc(size);
  342. memset(mem->data, 0, mem->size);
  343. if (_region.getType() == AtlasRegion::TYPE_BGRA8)
  344. {
  345. const uint8_t* inLineBuffer = _bitmapBuffer;
  346. uint8_t* outLineBuffer = m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4);
  347. for (int yy = 0; yy < _region.height; ++yy)
  348. {
  349. memcpy(outLineBuffer, inLineBuffer, _region.width * 4);
  350. inLineBuffer += _region.width * 4;
  351. outLineBuffer += m_textureSize * 4;
  352. }
  353. memcpy(mem->data, _bitmapBuffer, mem->size);
  354. }
  355. else
  356. {
  357. uint32_t layer = _region.getComponentIndex();
  358. const uint8_t* inLineBuffer = _bitmapBuffer;
  359. uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4) );
  360. for (int yy = 0; yy < _region.height; ++yy)
  361. {
  362. for (int xx = 0; xx < _region.width; ++xx)
  363. {
  364. outLineBuffer[(xx * 4) + layer] = inLineBuffer[xx];
  365. }
  366. memcpy(mem->data + yy * _region.width * 4, outLineBuffer, _region.width * 4);
  367. inLineBuffer += _region.width;
  368. outLineBuffer += m_textureSize * 4;
  369. }
  370. }
  371. bgfx::updateTextureCube(m_textureHandle, (uint8_t)_region.getFaceIndex(), 0, _region.x, _region.y, _region.width, _region.height, mem);
  372. }
  373. }
  374. void Atlas::packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  375. {
  376. packUV(m_layers[_idx].faceRegion, _vertexBuffer, _offset, _stride);
  377. }
  378. void Atlas::packUV(uint16_t _regionHandle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  379. {
  380. const AtlasRegion& region = m_regions[_regionHandle];
  381. packUV(region, _vertexBuffer, _offset, _stride);
  382. }
  383. static void writeUV(uint8_t* _vertexBuffer, int16_t _x, int16_t _y, int16_t _z, int16_t _w)
  384. {
  385. uint16_t* xyzw = (uint16_t*)_vertexBuffer;
  386. xyzw[0] = _x;
  387. xyzw[1] = _y;
  388. xyzw[2] = _z;
  389. xyzw[3] = _w;
  390. }
  391. void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  392. {
  393. int16_t x0 = (int16_t)( ( (float)_region.x * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) );
  394. int16_t y0 = (int16_t)( ( (float)_region.y * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) );
  395. int16_t x1 = (int16_t)( ( ( (float)_region.x + _region.width) * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) );
  396. int16_t y1 = (int16_t)( ( ( (float)_region.y + _region.height) * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) );
  397. int16_t ww = (int16_t)( (float(INT16_MAX) / 4.0f) * (float)_region.getComponentIndex() );
  398. _vertexBuffer += _offset;
  399. switch (_region.getFaceIndex() )
  400. {
  401. case 0: // +X
  402. x0 = -x0;
  403. x1 = -x1;
  404. y0 = -y0;
  405. y1 = -y1;
  406. writeUV(_vertexBuffer, INT16_MAX, y0, x0, ww); _vertexBuffer += _stride;
  407. writeUV(_vertexBuffer, INT16_MAX, y1, x0, ww); _vertexBuffer += _stride;
  408. writeUV(_vertexBuffer, INT16_MAX, y1, x1, ww); _vertexBuffer += _stride;
  409. writeUV(_vertexBuffer, INT16_MAX, y0, x1, ww); _vertexBuffer += _stride;
  410. break;
  411. case 1: // -X
  412. y0 = -y0;
  413. y1 = -y1;
  414. writeUV(_vertexBuffer, INT16_MIN, y0, x0, ww); _vertexBuffer += _stride;
  415. writeUV(_vertexBuffer, INT16_MIN, y1, x0, ww); _vertexBuffer += _stride;
  416. writeUV(_vertexBuffer, INT16_MIN, y1, x1, ww); _vertexBuffer += _stride;
  417. writeUV(_vertexBuffer, INT16_MIN, y0, x1, ww); _vertexBuffer += _stride;
  418. break;
  419. case 2: // +Y
  420. writeUV(_vertexBuffer, x0, INT16_MAX, y0, ww); _vertexBuffer += _stride;
  421. writeUV(_vertexBuffer, x0, INT16_MAX, y1, ww); _vertexBuffer += _stride;
  422. writeUV(_vertexBuffer, x1, INT16_MAX, y1, ww); _vertexBuffer += _stride;
  423. writeUV(_vertexBuffer, x1, INT16_MAX, y0, ww); _vertexBuffer += _stride;
  424. break;
  425. case 3: // -Y
  426. y0 = -y0;
  427. y1 = -y1;
  428. writeUV(_vertexBuffer, x0, INT16_MIN, y0, ww); _vertexBuffer += _stride;
  429. writeUV(_vertexBuffer, x0, INT16_MIN, y1, ww); _vertexBuffer += _stride;
  430. writeUV(_vertexBuffer, x1, INT16_MIN, y1, ww); _vertexBuffer += _stride;
  431. writeUV(_vertexBuffer, x1, INT16_MIN, y0, ww); _vertexBuffer += _stride;
  432. break;
  433. case 4: // +Z
  434. y0 = -y0;
  435. y1 = -y1;
  436. writeUV(_vertexBuffer, x0, y0, INT16_MAX, ww); _vertexBuffer += _stride;
  437. writeUV(_vertexBuffer, x0, y1, INT16_MAX, ww); _vertexBuffer += _stride;
  438. writeUV(_vertexBuffer, x1, y1, INT16_MAX, ww); _vertexBuffer += _stride;
  439. writeUV(_vertexBuffer, x1, y0, INT16_MAX, ww); _vertexBuffer += _stride;
  440. break;
  441. case 5: // -Z
  442. x0 = -x0;
  443. x1 = -x1;
  444. y0 = -y0;
  445. y1 = -y1;
  446. writeUV(_vertexBuffer, x0, y0, INT16_MIN, ww); _vertexBuffer += _stride;
  447. writeUV(_vertexBuffer, x0, y1, INT16_MIN, ww); _vertexBuffer += _stride;
  448. writeUV(_vertexBuffer, x1, y1, INT16_MIN, ww); _vertexBuffer += _stride;
  449. writeUV(_vertexBuffer, x1, y0, INT16_MIN, ww); _vertexBuffer += _stride;
  450. break;
  451. }
  452. }