cube_atlas.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "common.h"
  6. #include <bgfx/bgfx.h>
  7. #include <limits.h> // INT_MAX
  8. #include <vector>
  9. #include "cube_atlas.h"
  10. class RectanglePacker
  11. {
  12. public:
  13. RectanglePacker();
  14. RectanglePacker(uint32_t _width, uint32_t _height);
  15. /// non constructor initialization
  16. void init(uint32_t _width, uint32_t _height);
  17. /// find a suitable position for the given rectangle
  18. /// @return true if the rectangle can be added, false otherwise
  19. bool addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY);
  20. /// return the used surface in squared unit
  21. uint32_t getUsedSurface()
  22. {
  23. return m_usedSpace;
  24. }
  25. /// return the total available surface in squared unit
  26. uint32_t getTotalSurface()
  27. {
  28. return m_width * m_height;
  29. }
  30. /// return the usage ratio of the available surface [0:1]
  31. float getUsageRatio();
  32. /// reset to initial state
  33. void clear();
  34. private:
  35. int32_t fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height);
  36. /// Merges all skyline nodes that are at the same level.
  37. void merge();
  38. struct Node
  39. {
  40. Node(int16_t _x, int16_t _y, int16_t _width) : x(_x), y(_y), width(_width)
  41. {
  42. }
  43. int16_t x; //< The starting x-coordinate (leftmost).
  44. int16_t y; //< The y-coordinate of the skyline level line.
  45. int32_t width; //< The line _width. The ending coordinate (inclusive) will be x+width-1.
  46. };
  47. uint32_t m_width; //< width (in pixels) of the underlying texture
  48. uint32_t m_height; //< height (in pixels) of the underlying texture
  49. uint32_t m_usedSpace; //< Surface used in squared pixel
  50. std::vector<Node> m_skyline; //< node of the skyline algorithm
  51. };
  52. RectanglePacker::RectanglePacker()
  53. : m_width(0)
  54. , m_height(0)
  55. , m_usedSpace(0)
  56. {
  57. }
  58. RectanglePacker::RectanglePacker(uint32_t _width, uint32_t _height)
  59. : m_width(_width)
  60. , m_height(_height)
  61. , m_usedSpace(0)
  62. {
  63. // We want a one pixel border around the whole atlas to avoid any artefact when
  64. // sampling texture
  65. m_skyline.push_back(Node(1, 1, uint16_t(_width - 2) ) );
  66. }
  67. void RectanglePacker::init(uint32_t _width, uint32_t _height)
  68. {
  69. BX_ASSERT(_width > 2, "_width must be > 2");
  70. BX_ASSERT(_height > 2, "_height must be > 2");
  71. m_width = _width;
  72. m_height = _height;
  73. m_usedSpace = 0;
  74. m_skyline.clear();
  75. // We want a one pixel border around the whole atlas to avoid any artifact when
  76. // sampling texture
  77. m_skyline.push_back(Node(1, 1, uint16_t(_width - 2) ) );
  78. }
  79. bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY)
  80. {
  81. int best_height, best_index;
  82. int32_t best_width;
  83. Node* node;
  84. Node* prev;
  85. _outX = 0;
  86. _outY = 0;
  87. best_height = INT_MAX;
  88. best_index = -1;
  89. best_width = INT_MAX;
  90. for (uint16_t ii = 0, num = uint16_t(m_skyline.size() ); ii < num; ++ii)
  91. {
  92. int32_t yy = fit(ii, _width, _height);
  93. if (yy >= 0)
  94. {
  95. node = &m_skyline[ii];
  96. if ( ( (yy + _height) < best_height)
  97. || ( ( (yy + _height) == best_height) && (node->width < best_width) ) )
  98. {
  99. best_height = uint16_t(yy) + _height;
  100. best_index = ii;
  101. best_width = node->width;
  102. _outX = node->x;
  103. _outY = uint16_t(yy);
  104. }
  105. }
  106. }
  107. if (best_index == -1)
  108. {
  109. return false;
  110. }
  111. Node newNode(_outX, _outY + _height, _width);
  112. m_skyline.insert(m_skyline.begin() + best_index, newNode);
  113. for (uint16_t ii = uint16_t(best_index + 1), num = uint16_t(m_skyline.size() ); ii < num; ++ii)
  114. {
  115. node = &m_skyline[ii];
  116. prev = &m_skyline[ii - 1];
  117. if (node->x < (prev->x + prev->width) )
  118. {
  119. uint16_t shrink = uint16_t(prev->x + prev->width - node->x);
  120. node->x += shrink;
  121. node->width -= shrink;
  122. if (node->width <= 0)
  123. {
  124. m_skyline.erase(m_skyline.begin() + ii);
  125. --ii;
  126. --num;
  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_ASSERT(_textureSize >= 64 && _textureSize <= 4096, "Invalid _textureSize %d.", _textureSize);
  218. BX_ASSERT(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000, "Invalid _maxRegionsCount %d.", _maxRegionsCount);
  219. m_texelSize = float(UINT16_MAX) / float(m_textureSize);
  220. m_layers = new PackedLayer[6];
  221. for (int ii = 0; ii < 6; ++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. bx::memSet(m_textureBuffer, 0, _textureSize * _textureSize * 6 * 4);
  228. m_textureHandle = bgfx::createTextureCube(_textureSize
  229. , false
  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(6)
  236. , m_usedFaces(6)
  237. , m_textureSize(_textureSize)
  238. , m_regionCount(_regionCount)
  239. , m_maxRegionCount(_regionCount < _maxRegionsCount ? _regionCount : _maxRegionsCount)
  240. {
  241. BX_ASSERT(_regionCount <= 64 && _maxRegionsCount <= 4096, "_regionCount %d, _maxRegionsCount %d", _regionCount, _maxRegionsCount);
  242. m_texelSize = float(UINT16_MAX) / float(m_textureSize);
  243. m_regions = new AtlasRegion[_regionCount];
  244. m_textureBuffer = new uint8_t[getTextureBufferSize()];
  245. bx::memCopy(m_regions, _regionBuffer, _regionCount * sizeof(AtlasRegion) );
  246. bx::memCopy(m_textureBuffer, _textureBuffer, getTextureBufferSize() );
  247. m_textureHandle = bgfx::createTextureCube(_textureSize
  248. , false
  249. , 1
  250. , bgfx::TextureFormat::BGRA8
  251. , BGFX_SAMPLER_NONE
  252. , bgfx::makeRef(m_textureBuffer, getTextureBufferSize() )
  253. );
  254. }
  255. Atlas::~Atlas()
  256. {
  257. bgfx::destroy(m_textureHandle);
  258. delete [] m_layers;
  259. delete [] m_regions;
  260. delete [] m_textureBuffer;
  261. }
  262. uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type, uint16_t outline)
  263. {
  264. if (m_regionCount >= m_maxRegionCount)
  265. {
  266. return UINT16_MAX;
  267. }
  268. uint16_t xx = 0;
  269. uint16_t yy = 0;
  270. uint32_t idx = 0;
  271. while (idx < m_usedLayers)
  272. {
  273. if (m_layers[idx].faceRegion.getType() == _type
  274. && m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
  275. {
  276. break;
  277. }
  278. idx++;
  279. }
  280. if (idx >= m_usedLayers)
  281. {
  282. if ( (idx + _type) > 24
  283. || m_usedFaces >= 6)
  284. {
  285. return UINT16_MAX;
  286. }
  287. //for (int ii = 0; ii < _type; ++ii)
  288. {
  289. int ii = 0;
  290. AtlasRegion& region = m_layers[idx + ii].faceRegion;
  291. region.x = 0;
  292. region.y = 0;
  293. region.width = m_textureSize;
  294. region.height = m_textureSize;
  295. region.setMask(_type, m_usedFaces, ii);
  296. }
  297. m_usedLayers++;
  298. m_usedFaces++;
  299. if (!m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
  300. {
  301. return UINT16_MAX;
  302. }
  303. }
  304. AtlasRegion& region = m_regions[m_regionCount];
  305. region.x = xx;
  306. region.y = yy;
  307. region.width = _width;
  308. region.height = _height;
  309. region.mask = m_layers[idx].faceRegion.mask;
  310. updateRegion(region, _bitmapBuffer);
  311. region.x += outline;
  312. region.y += outline;
  313. region.width -= (outline * 2);
  314. region.height -= (outline * 2);
  315. return m_regionCount++;
  316. }
  317. void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer)
  318. {
  319. uint32_t size = _region.width * _region.height * 4;
  320. if (0 < size)
  321. {
  322. const bgfx::Memory* mem = bgfx::alloc(size);
  323. bx::memSet(mem->data, 0, mem->size);
  324. if (_region.getType() == AtlasRegion::TYPE_BGRA8)
  325. {
  326. const uint8_t* inLineBuffer = _bitmapBuffer;
  327. uint8_t* outLineBuffer = m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4);
  328. for (int yy = 0; yy < _region.height; ++yy)
  329. {
  330. bx::memCopy(outLineBuffer, inLineBuffer, _region.width * 4);
  331. inLineBuffer += _region.width * 4;
  332. outLineBuffer += m_textureSize * 4;
  333. }
  334. bx::memCopy(mem->data, _bitmapBuffer, mem->size);
  335. }
  336. else
  337. {
  338. uint32_t layer = _region.getComponentIndex();
  339. const uint8_t* inLineBuffer = _bitmapBuffer;
  340. uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4) );
  341. for (int yy = 0; yy < _region.height; ++yy)
  342. {
  343. for (int xx = 0; xx < _region.width; ++xx)
  344. {
  345. outLineBuffer[(xx * 4) + layer] = inLineBuffer[xx];
  346. }
  347. bx::memCopy(mem->data + yy * _region.width * 4, outLineBuffer, _region.width * 4);
  348. inLineBuffer += _region.width;
  349. outLineBuffer += m_textureSize * 4;
  350. }
  351. }
  352. bgfx::updateTextureCube(m_textureHandle, 0, (uint8_t)_region.getFaceIndex(), 0, _region.x, _region.y, _region.width, _region.height, mem);
  353. }
  354. }
  355. void Atlas::packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  356. {
  357. packUV(m_layers[_idx].faceRegion, _vertexBuffer, _offset, _stride);
  358. }
  359. void Atlas::packUV(uint16_t _regionHandle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  360. {
  361. const AtlasRegion& region = m_regions[_regionHandle];
  362. packUV(region, _vertexBuffer, _offset, _stride);
  363. }
  364. static void writeUV(uint8_t* _vertexBuffer, int16_t _x, int16_t _y, int16_t _z, int16_t _w)
  365. {
  366. uint16_t* xyzw = (uint16_t*)_vertexBuffer;
  367. xyzw[0] = _x;
  368. xyzw[1] = _y;
  369. xyzw[2] = _z;
  370. xyzw[3] = _w;
  371. }
  372. void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
  373. {
  374. int16_t x0 = (int16_t)( ( (float)_region.x * m_texelSize) - float(INT16_MAX) );
  375. int16_t y0 = (int16_t)( ( (float)_region.y * m_texelSize) - float(INT16_MAX) );
  376. int16_t x1 = (int16_t)( ( ( (float)_region.x + _region.width) * m_texelSize) - float(INT16_MAX) );
  377. int16_t y1 = (int16_t)( ( ( (float)_region.y + _region.height) * m_texelSize) - float(INT16_MAX) );
  378. int16_t ww = (int16_t)( (float(INT16_MAX) / 4.0f) * (float)_region.getComponentIndex() );
  379. _vertexBuffer += _offset;
  380. switch (_region.getFaceIndex() )
  381. {
  382. case 0: // +X
  383. x0 = -x0;
  384. x1 = -x1;
  385. y0 = -y0;
  386. y1 = -y1;
  387. writeUV(_vertexBuffer, INT16_MAX, y0, x0, ww); _vertexBuffer += _stride;
  388. writeUV(_vertexBuffer, INT16_MAX, y1, x0, ww); _vertexBuffer += _stride;
  389. writeUV(_vertexBuffer, INT16_MAX, y1, x1, ww); _vertexBuffer += _stride;
  390. writeUV(_vertexBuffer, INT16_MAX, y0, x1, ww); _vertexBuffer += _stride;
  391. break;
  392. case 1: // -X
  393. y0 = -y0;
  394. y1 = -y1;
  395. writeUV(_vertexBuffer, INT16_MIN, y0, x0, ww); _vertexBuffer += _stride;
  396. writeUV(_vertexBuffer, INT16_MIN, y1, x0, ww); _vertexBuffer += _stride;
  397. writeUV(_vertexBuffer, INT16_MIN, y1, x1, ww); _vertexBuffer += _stride;
  398. writeUV(_vertexBuffer, INT16_MIN, y0, x1, ww); _vertexBuffer += _stride;
  399. break;
  400. case 2: // +Y
  401. writeUV(_vertexBuffer, x0, INT16_MAX, y0, ww); _vertexBuffer += _stride;
  402. writeUV(_vertexBuffer, x0, INT16_MAX, y1, ww); _vertexBuffer += _stride;
  403. writeUV(_vertexBuffer, x1, INT16_MAX, y1, ww); _vertexBuffer += _stride;
  404. writeUV(_vertexBuffer, x1, INT16_MAX, y0, ww); _vertexBuffer += _stride;
  405. break;
  406. case 3: // -Y
  407. y0 = -y0;
  408. y1 = -y1;
  409. writeUV(_vertexBuffer, x0, INT16_MIN, y0, ww); _vertexBuffer += _stride;
  410. writeUV(_vertexBuffer, x0, INT16_MIN, y1, ww); _vertexBuffer += _stride;
  411. writeUV(_vertexBuffer, x1, INT16_MIN, y1, ww); _vertexBuffer += _stride;
  412. writeUV(_vertexBuffer, x1, INT16_MIN, y0, ww); _vertexBuffer += _stride;
  413. break;
  414. case 4: // +Z
  415. y0 = -y0;
  416. y1 = -y1;
  417. writeUV(_vertexBuffer, x0, y0, INT16_MAX, ww); _vertexBuffer += _stride;
  418. writeUV(_vertexBuffer, x0, y1, INT16_MAX, ww); _vertexBuffer += _stride;
  419. writeUV(_vertexBuffer, x1, y1, INT16_MAX, ww); _vertexBuffer += _stride;
  420. writeUV(_vertexBuffer, x1, y0, INT16_MAX, ww); _vertexBuffer += _stride;
  421. break;
  422. case 5: // -Z
  423. x0 = -x0;
  424. x1 = -x1;
  425. y0 = -y0;
  426. y1 = -y1;
  427. writeUV(_vertexBuffer, x0, y0, INT16_MIN, ww); _vertexBuffer += _stride;
  428. writeUV(_vertexBuffer, x0, y1, INT16_MIN, ww); _vertexBuffer += _stride;
  429. writeUV(_vertexBuffer, x1, y1, INT16_MIN, ww); _vertexBuffer += _stride;
  430. writeUV(_vertexBuffer, x1, y0, INT16_MIN, ww); _vertexBuffer += _stride;
  431. break;
  432. }
  433. }