cube_atlas.cpp 14 KB

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