cube_atlas.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. uint16_t yy = (uint16_t)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 = yy + _height;
  101. best_index = ii;
  102. best_width = node->width;
  103. _outX = node->x;
  104. _outY = 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. }
  128. else
  129. {
  130. break;
  131. }
  132. }
  133. else
  134. {
  135. break;
  136. }
  137. }
  138. merge();
  139. m_usedSpace += _width * _height;
  140. return true;
  141. }
  142. float RectanglePacker::getUsageRatio()
  143. {
  144. uint32_t total = m_width * m_height;
  145. if (total > 0)
  146. {
  147. return (float)m_usedSpace / (float)total;
  148. }
  149. return 0.0f;
  150. }
  151. void RectanglePacker::clear()
  152. {
  153. m_skyline.clear();
  154. m_usedSpace = 0;
  155. // We want a one pixel border around the whole atlas to avoid any artefact when
  156. // sampling texture
  157. m_skyline.push_back(Node(1, 1, uint16_t(m_width - 2) ) );
  158. }
  159. int32_t RectanglePacker::fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height)
  160. {
  161. int32_t width = _width;
  162. int32_t height = _height;
  163. const Node& baseNode = m_skyline[_skylineNodeIndex];
  164. int32_t xx = baseNode.x, yy;
  165. int32_t widthLeft = width;
  166. int32_t ii = _skylineNodeIndex;
  167. if ( (xx + width) > (int32_t)(m_width - 1) )
  168. {
  169. return -1;
  170. }
  171. yy = baseNode.y;
  172. while (widthLeft > 0)
  173. {
  174. const Node& node = m_skyline[ii];
  175. if (node.y > yy)
  176. {
  177. yy = node.y;
  178. }
  179. if ( (yy + height) > (int32_t)(m_height - 1) )
  180. {
  181. return -1;
  182. }
  183. widthLeft -= node.width;
  184. ++ii;
  185. }
  186. return yy;
  187. }
  188. void RectanglePacker::merge()
  189. {
  190. Node* node;
  191. Node* next;
  192. uint32_t ii;
  193. for (ii = 0; ii < m_skyline.size() - 1; ++ii)
  194. {
  195. node = (Node*) &m_skyline[ii];
  196. next = (Node*) &m_skyline[ii + 1];
  197. if (node->y == next->y)
  198. {
  199. node->width += next->width;
  200. m_skyline.erase(m_skyline.begin() + ii + 1);
  201. --ii;
  202. }
  203. }
  204. }
  205. struct Atlas::PackedLayer
  206. {
  207. RectanglePacker packer;
  208. AtlasRegion faceRegion;
  209. };
  210. Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount)
  211. : m_usedLayers(0)
  212. , m_usedFaces(0)
  213. , m_textureSize(_textureSize)
  214. , m_regionCount(0)
  215. , m_maxRegionCount(_maxRegionsCount)
  216. {
  217. BX_CHECK(_textureSize >= 64 && _textureSize <= 4096, "Invalid _textureSize %d.", _textureSize);
  218. BX_CHECK(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000, "Invalid _maxRegionsCount %d.", _maxRegionsCount);
  219. init();
  220. m_layers = new PackedLayer[24];
  221. for (int ii = 0; ii < 24; ++ii)
  222. {
  223. m_layers[ii].packer.init(_textureSize, _textureSize);
  224. }
  225. m_regions = new AtlasRegion[_maxRegionsCount];
  226. m_textureBuffer = new uint8_t[ _textureSize * _textureSize * 6 * 4 ];
  227. memset(m_textureBuffer, 0, _textureSize * _textureSize * 6 * 4);
  228. m_textureHandle = bgfx::createTextureCube(_textureSize
  229. , 1
  230. , bgfx::TextureFormat::BGRA8
  231. );
  232. }
  233. Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer, uint16_t _regionCount, const uint8_t* _regionBuffer, uint16_t _maxRegionsCount)
  234. : m_usedLayers(24)
  235. , m_usedFaces(6)
  236. , m_textureSize(_textureSize)
  237. , m_regionCount(_regionCount)
  238. , m_maxRegionCount(_regionCount < _maxRegionsCount ? _regionCount : _maxRegionsCount)
  239. {
  240. BX_CHECK(_regionCount <= 64 && _maxRegionsCount <= 4096, "_regionCount %d, _maxRegionsCount %d", _regionCount, _maxRegionsCount);
  241. init();
  242. m_regions = new AtlasRegion[_regionCount];
  243. m_textureBuffer = new uint8_t[getTextureBufferSize()];
  244. memcpy(m_regions, _regionBuffer, _regionCount * sizeof(AtlasRegion) );
  245. memcpy(m_textureBuffer, _textureBuffer, getTextureBufferSize() );
  246. m_textureHandle = bgfx::createTextureCube(_textureSize
  247. , 1
  248. , bgfx::TextureFormat::BGRA8
  249. , BGFX_TEXTURE_NONE
  250. , bgfx::makeRef(m_textureBuffer, getTextureBufferSize() )
  251. );
  252. }
  253. Atlas::~Atlas()
  254. {
  255. bgfx::destroyTexture(m_textureHandle);
  256. delete [] m_layers;
  257. delete [] m_regions;
  258. delete [] m_textureBuffer;
  259. }
  260. void Atlas::init()
  261. {
  262. m_texelSize = float(UINT16_MAX) / float(m_textureSize);
  263. float texelHalf = m_texelSize/2.0f;
  264. switch (bgfx::getRendererType() )
  265. {
  266. case bgfx::RendererType::Direct3D9:
  267. m_texelOffset[0] = 0.0f;
  268. m_texelOffset[1] = 0.0f;
  269. break;
  270. case bgfx::RendererType::Direct3D11:
  271. case bgfx::RendererType::Direct3D12:
  272. m_texelOffset[0] = texelHalf;
  273. m_texelOffset[1] = texelHalf;
  274. break;
  275. default:
  276. m_texelOffset[0] = texelHalf;
  277. m_texelOffset[1] = -texelHalf;
  278. break;
  279. }
  280. }
  281. uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type, uint16_t outline)
  282. {
  283. if (m_regionCount >= m_maxRegionCount)
  284. {
  285. return UINT16_MAX;
  286. }
  287. uint16_t xx = 0;
  288. uint16_t yy = 0;
  289. uint32_t idx = 0;
  290. while (idx < m_usedLayers)
  291. {
  292. if (m_layers[idx].faceRegion.getType() == _type
  293. && m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
  294. {
  295. break;
  296. }
  297. idx++;
  298. }
  299. if (idx >= m_usedLayers)
  300. {
  301. if ( (idx + _type) > 24
  302. || m_usedFaces >= 6)
  303. {
  304. return UINT16_MAX;
  305. }
  306. for (int ii = 0; ii < _type; ++ii)
  307. {
  308. AtlasRegion& region = m_layers[idx + ii].faceRegion;
  309. region.x = 0;
  310. region.y = 0;
  311. region.width = m_textureSize;
  312. region.height = m_textureSize;
  313. region.setMask(_type, m_usedFaces, ii);
  314. }
  315. m_usedLayers += _type;
  316. m_usedFaces++;
  317. if (!m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
  318. {
  319. return UINT16_MAX;
  320. }
  321. }
  322. AtlasRegion& region = m_regions[m_regionCount];
  323. region.x = xx;
  324. region.y = yy;
  325. region.width = _width;
  326. region.height = _height;
  327. region.mask = m_layers[idx].faceRegion.mask;
  328. updateRegion(region, _bitmapBuffer);
  329. region.x += outline;
  330. region.y += outline;
  331. region.width -= (outline * 2);
  332. region.height -= (outline * 2);
  333. return m_regionCount++;
  334. }
  335. void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer)
  336. {
  337. uint32_t size = _region.width * _region.height * 4;
  338. if (0 < size)
  339. {
  340. const bgfx::Memory* mem = bgfx::alloc(size);
  341. memset(mem->data, 0, mem->size);
  342. if (_region.getType() == AtlasRegion::TYPE_BGRA8)
  343. {
  344. const uint8_t* inLineBuffer = _bitmapBuffer;
  345. uint8_t* outLineBuffer = m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4);
  346. for (int yy = 0; yy < _region.height; ++yy)
  347. {
  348. memcpy(outLineBuffer, inLineBuffer, _region.width * 4);
  349. inLineBuffer += _region.width * 4;
  350. outLineBuffer += m_textureSize * 4;
  351. }
  352. memcpy(mem->data, _bitmapBuffer, mem->size);
  353. }
  354. else
  355. {
  356. uint32_t layer = _region.getComponentIndex();
  357. const uint8_t* inLineBuffer = _bitmapBuffer;
  358. uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4) );
  359. for (int yy = 0; yy < _region.height; ++yy)
  360. {
  361. for (int xx = 0; xx < _region.width; ++xx)
  362. {
  363. outLineBuffer[(xx * 4) + layer] = inLineBuffer[xx];
  364. }
  365. memcpy(mem->data + yy * _region.width * 4, outLineBuffer, _region.width * 4);
  366. inLineBuffer += _region.width;
  367. outLineBuffer += m_textureSize * 4;
  368. }
  369. }
  370. bgfx::updateTextureCube(m_textureHandle, (uint8_t)_region.getFaceIndex(), 0, _region.x, _region.y, _region.width, _region.height, mem);
  371. }
  372. }
  373. void Atlas::packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  374. {
  375. packUV(m_layers[_idx].faceRegion, _vertexBuffer, _offset, _stride);
  376. }
  377. void Atlas::packUV(uint16_t _regionHandle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  378. {
  379. const AtlasRegion& region = m_regions[_regionHandle];
  380. packUV(region, _vertexBuffer, _offset, _stride);
  381. }
  382. static void writeUV(uint8_t* _vertexBuffer, int16_t _x, int16_t _y, int16_t _z, int16_t _w)
  383. {
  384. uint16_t* xyzw = (uint16_t*)_vertexBuffer;
  385. xyzw[0] = _x;
  386. xyzw[1] = _y;
  387. xyzw[2] = _z;
  388. xyzw[3] = _w;
  389. }
  390. void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  391. {
  392. int16_t x0 = (int16_t)( ( (float)_region.x * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) );
  393. int16_t y0 = (int16_t)( ( (float)_region.y * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) );
  394. int16_t x1 = (int16_t)( ( ( (float)_region.x + _region.width) * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) );
  395. int16_t y1 = (int16_t)( ( ( (float)_region.y + _region.height) * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) );
  396. int16_t ww = (int16_t)( (float(INT16_MAX) / 4.0f) * (float)_region.getComponentIndex() );
  397. _vertexBuffer += _offset;
  398. switch (_region.getFaceIndex() )
  399. {
  400. case 0: // +X
  401. x0 = -x0;
  402. x1 = -x1;
  403. y0 = -y0;
  404. y1 = -y1;
  405. writeUV(_vertexBuffer, INT16_MAX, y0, x0, ww); _vertexBuffer += _stride;
  406. writeUV(_vertexBuffer, INT16_MAX, y1, x0, ww); _vertexBuffer += _stride;
  407. writeUV(_vertexBuffer, INT16_MAX, y1, x1, ww); _vertexBuffer += _stride;
  408. writeUV(_vertexBuffer, INT16_MAX, y0, x1, ww); _vertexBuffer += _stride;
  409. break;
  410. case 1: // -X
  411. y0 = -y0;
  412. y1 = -y1;
  413. writeUV(_vertexBuffer, INT16_MIN, y0, x0, ww); _vertexBuffer += _stride;
  414. writeUV(_vertexBuffer, INT16_MIN, y1, x0, ww); _vertexBuffer += _stride;
  415. writeUV(_vertexBuffer, INT16_MIN, y1, x1, ww); _vertexBuffer += _stride;
  416. writeUV(_vertexBuffer, INT16_MIN, y0, x1, ww); _vertexBuffer += _stride;
  417. break;
  418. case 2: // +Y
  419. writeUV(_vertexBuffer, x0, INT16_MAX, y0, ww); _vertexBuffer += _stride;
  420. writeUV(_vertexBuffer, x0, INT16_MAX, y1, ww); _vertexBuffer += _stride;
  421. writeUV(_vertexBuffer, x1, INT16_MAX, y1, ww); _vertexBuffer += _stride;
  422. writeUV(_vertexBuffer, x1, INT16_MAX, y0, ww); _vertexBuffer += _stride;
  423. break;
  424. case 3: // -Y
  425. y0 = -y0;
  426. y1 = -y1;
  427. writeUV(_vertexBuffer, x0, INT16_MIN, y0, ww); _vertexBuffer += _stride;
  428. writeUV(_vertexBuffer, x0, INT16_MIN, y1, ww); _vertexBuffer += _stride;
  429. writeUV(_vertexBuffer, x1, INT16_MIN, y1, ww); _vertexBuffer += _stride;
  430. writeUV(_vertexBuffer, x1, INT16_MIN, y0, ww); _vertexBuffer += _stride;
  431. break;
  432. case 4: // +Z
  433. y0 = -y0;
  434. y1 = -y1;
  435. writeUV(_vertexBuffer, x0, y0, INT16_MAX, ww); _vertexBuffer += _stride;
  436. writeUV(_vertexBuffer, x0, y1, INT16_MAX, ww); _vertexBuffer += _stride;
  437. writeUV(_vertexBuffer, x1, y1, INT16_MAX, ww); _vertexBuffer += _stride;
  438. writeUV(_vertexBuffer, x1, y0, INT16_MAX, ww); _vertexBuffer += _stride;
  439. break;
  440. case 5: // -Z
  441. x0 = -x0;
  442. x1 = -x1;
  443. y0 = -y0;
  444. y1 = -y1;
  445. writeUV(_vertexBuffer, x0, y0, INT16_MIN, ww); _vertexBuffer += _stride;
  446. writeUV(_vertexBuffer, x0, y1, INT16_MIN, ww); _vertexBuffer += _stride;
  447. writeUV(_vertexBuffer, x1, y1, INT16_MIN, ww); _vertexBuffer += _stride;
  448. writeUV(_vertexBuffer, x1, y0, INT16_MIN, ww); _vertexBuffer += _stride;
  449. break;
  450. }
  451. }