|
|
@@ -46,6 +46,7 @@ public:
|
|
|
|
|
|
private:
|
|
|
int32_t fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height);
|
|
|
+
|
|
|
/// Merges all skyline nodes that are at the same level.
|
|
|
void merge();
|
|
|
|
|
|
@@ -55,29 +56,29 @@ private:
|
|
|
{
|
|
|
}
|
|
|
|
|
|
- /// The starting x-coordinate (leftmost).
|
|
|
- int16_t x;
|
|
|
- /// The y-coordinate of the skyline level line.
|
|
|
- int16_t y;
|
|
|
- /// The line _width. The ending coordinate (inclusive) will be x+width-1.
|
|
|
- int32_t width; // 32bit to avoid padding
|
|
|
+ int16_t x; //< The starting x-coordinate (leftmost).
|
|
|
+ int16_t y; //< The y-coordinate of the skyline level line.
|
|
|
+ int32_t width; //< The line _width. The ending coordinate (inclusive) will be x+width-1.
|
|
|
};
|
|
|
|
|
|
- /// width (in pixels) of the underlying texture
|
|
|
- uint32_t m_width;
|
|
|
- /// height (in pixels) of the underlying texture
|
|
|
- uint32_t m_height;
|
|
|
- /// Surface used in squared pixel
|
|
|
- uint32_t m_usedSpace;
|
|
|
- /// node of the skyline algorithm
|
|
|
- std::vector<Node> m_skyline;
|
|
|
+
|
|
|
+ uint32_t m_width; //< width (in pixels) of the underlying texture
|
|
|
+ uint32_t m_height; //< height (in pixels) of the underlying texture
|
|
|
+ uint32_t m_usedSpace; //< Surface used in squared pixel
|
|
|
+ std::vector<Node> m_skyline; //< node of the skyline algorithm
|
|
|
};
|
|
|
|
|
|
-RectanglePacker::RectanglePacker() : m_width(0), m_height(0), m_usedSpace(0)
|
|
|
+RectanglePacker::RectanglePacker()
|
|
|
+ : m_width(0)
|
|
|
+ , m_height(0)
|
|
|
+ , m_usedSpace(0)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
-RectanglePacker::RectanglePacker(uint32_t _width, uint32_t _height) : m_width(_width), m_height(_height), m_usedSpace(0)
|
|
|
+RectanglePacker::RectanglePacker(uint32_t _width, uint32_t _height)
|
|
|
+ : m_width(_width)
|
|
|
+ , m_height(_height)
|
|
|
+ , m_usedSpace(0)
|
|
|
{
|
|
|
// We want a one pixel border around the whole atlas to avoid any artefact when
|
|
|
// sampling texture
|
|
|
@@ -100,7 +101,7 @@ void RectanglePacker::init(uint32_t _width, uint32_t _height)
|
|
|
|
|
|
bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY)
|
|
|
{
|
|
|
- int y, best_height, best_index;
|
|
|
+ int yy, best_height, best_index;
|
|
|
int32_t best_width;
|
|
|
Node* node;
|
|
|
Node* prev;
|
|
|
@@ -114,19 +115,19 @@ bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t&
|
|
|
best_width = INT_MAX;
|
|
|
for (ii = 0; ii < m_skyline.size(); ++ii)
|
|
|
{
|
|
|
- y = fit(ii, _width, _height);
|
|
|
- if (y >= 0)
|
|
|
+ yy = fit(ii, _width, _height);
|
|
|
+ if (yy >= 0)
|
|
|
{
|
|
|
node = &m_skyline[ii];
|
|
|
- if ( ( (y + _height) < best_height)
|
|
|
- || ( ( (y + _height) == best_height)
|
|
|
+ if ( ( (yy + _height) < best_height)
|
|
|
+ || ( ( (yy + _height) == best_height)
|
|
|
&& (node->width < best_width) ) )
|
|
|
{
|
|
|
- best_height = y + _height;
|
|
|
+ best_height = yy + _height;
|
|
|
best_index = ii;
|
|
|
best_width = node->width;
|
|
|
_outX = node->x;
|
|
|
- _outY = y;
|
|
|
+ _outY = yy;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -199,34 +200,34 @@ int32_t RectanglePacker::fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16
|
|
|
|
|
|
const Node& baseNode = m_skyline[_skylineNodeIndex];
|
|
|
|
|
|
- int32_t x = baseNode.x, y;
|
|
|
- int32_t _width_left = width;
|
|
|
- int32_t i = _skylineNodeIndex;
|
|
|
+ int32_t xx = baseNode.x, yy;
|
|
|
+ int32_t widthLeft = width;
|
|
|
+ int32_t ii = _skylineNodeIndex;
|
|
|
|
|
|
- if ( (x + width) > (int32_t)(m_width - 1) )
|
|
|
+ if ( (xx + width) > (int32_t)(m_width - 1) )
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- y = baseNode.y;
|
|
|
- while (_width_left > 0)
|
|
|
+ yy = baseNode.y;
|
|
|
+ while (widthLeft > 0)
|
|
|
{
|
|
|
- const Node& node = m_skyline[i];
|
|
|
- if (node.y > y)
|
|
|
+ const Node& node = m_skyline[ii];
|
|
|
+ if (node.y > yy)
|
|
|
{
|
|
|
- y = node.y;
|
|
|
+ yy = node.y;
|
|
|
}
|
|
|
|
|
|
- if ( (y + height) > (int32_t)(m_height - 1) )
|
|
|
+ if ( (yy + height) > (int32_t)(m_height - 1) )
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- _width_left -= node.width;
|
|
|
- ++i;
|
|
|
+ widthLeft -= node.width;
|
|
|
+ ++ii;
|
|
|
}
|
|
|
|
|
|
- return y;
|
|
|
+ return yy;
|
|
|
}
|
|
|
|
|
|
void RectanglePacker::merge()
|
|
|
@@ -255,34 +256,25 @@ struct Atlas::PackedLayer
|
|
|
};
|
|
|
|
|
|
Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount)
|
|
|
+ : m_usedLayers(0)
|
|
|
+ , m_usedFaces(0)
|
|
|
+ , m_textureSize(_textureSize)
|
|
|
+ , m_regionCount(0)
|
|
|
+ , m_maxRegionCount(_maxRegionsCount)
|
|
|
{
|
|
|
- BX_CHECK(_textureSize >= 64
|
|
|
- && _textureSize <= 4096, "suspicious texture size");
|
|
|
- BX_CHECK(_maxRegionsCount >= 64
|
|
|
- && _maxRegionsCount <= 32000, "suspicious _regions count");
|
|
|
+ BX_CHECK(_textureSize >= 64 && _textureSize <= 4096, "Invalid _textureSize %d.", _textureSize);
|
|
|
+ BX_CHECK(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000, "Invalid _maxRegionsCount %d.", _maxRegionsCount);
|
|
|
m_layers = new PackedLayer[24];
|
|
|
for (int ii = 0; ii < 24; ++ii)
|
|
|
{
|
|
|
m_layers[ii].packer.init(_textureSize, _textureSize);
|
|
|
}
|
|
|
|
|
|
- m_usedLayers = 0;
|
|
|
- m_usedFaces = 0;
|
|
|
-
|
|
|
- m_textureSize = _textureSize;
|
|
|
- m_regionCount = 0;
|
|
|
- m_maxRegionCount = _maxRegionsCount;
|
|
|
m_regions = new AtlasRegion[_maxRegionsCount];
|
|
|
m_textureBuffer = new uint8_t[ _textureSize * _textureSize * 6 * 4 ];
|
|
|
memset(m_textureBuffer, 0, _textureSize * _textureSize * 6 * 4);
|
|
|
- //BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT;
|
|
|
- //BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT
|
|
|
- //BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP
|
|
|
- uint32_t flags = 0; // BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT;
|
|
|
-
|
|
|
- //Uncomment this to debug atlas
|
|
|
- //const bgfx::Memory* mem = bgfx::alloc(textureSize*textureSize * 6 * 4);
|
|
|
- //memset(mem->data, 255, mem->size);
|
|
|
+ uint32_t flags = 0;
|
|
|
+
|
|
|
const bgfx::Memory* mem = NULL;
|
|
|
m_textureHandle = bgfx::createTextureCube(6
|
|
|
, _textureSize
|
|
|
@@ -294,32 +286,18 @@ Atlas::Atlas(uint16_t _textureSize, uint16_t _maxRegionsCount)
|
|
|
}
|
|
|
|
|
|
Atlas::Atlas(uint16_t _textureSize, const uint8_t* _textureBuffer, uint16_t _regionCount, const uint8_t* _regionBuffer, uint16_t _maxRegionsCount)
|
|
|
+ : m_usedLayers(24)
|
|
|
+ , m_usedFaces(6)
|
|
|
+ , m_textureSize(_textureSize)
|
|
|
+ , m_regionCount(_regionCount)
|
|
|
+ , m_maxRegionCount(_regionCount < _maxRegionsCount ? _regionCount : _maxRegionsCount)
|
|
|
{
|
|
|
- BX_CHECK(_regionCount <= 64
|
|
|
- && _maxRegionsCount <= 4096, "suspicious initialization");
|
|
|
- //layers are frozen
|
|
|
- m_usedLayers = 24;
|
|
|
- m_usedFaces = 6;
|
|
|
-
|
|
|
- m_textureSize = _textureSize;
|
|
|
- m_regionCount = _regionCount;
|
|
|
- //regions are frozen
|
|
|
- if (_regionCount < _maxRegionsCount)
|
|
|
- {
|
|
|
- m_maxRegionCount = _regionCount;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- m_maxRegionCount = _maxRegionsCount;
|
|
|
- }
|
|
|
+ BX_CHECK(_regionCount <= 64 && _maxRegionsCount <= 4096, "_regionCount %d, _maxRegionsCount %d", _regionCount, _maxRegionsCount);
|
|
|
|
|
|
m_regions = new AtlasRegion[_regionCount];
|
|
|
m_textureBuffer = new uint8_t[getTextureBufferSize()];
|
|
|
|
|
|
- //BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT;
|
|
|
- //BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT
|
|
|
- //BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP
|
|
|
- uint32_t flags = 0; //BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC|BGFX_TEXTURE_MIP_POINT;
|
|
|
+ uint32_t flags = 0;
|
|
|
memcpy(m_regions, _regionBuffer, _regionCount * sizeof(AtlasRegion) );
|
|
|
memcpy(m_textureBuffer, _textureBuffer, getTextureBufferSize() );
|
|
|
|
|
|
@@ -346,15 +324,14 @@ uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bit
|
|
|
return UINT16_MAX;
|
|
|
}
|
|
|
|
|
|
- uint16_t x = 0, y = 0;
|
|
|
- // We want each bitmap to be separated by at least one black pixel
|
|
|
- // TODO manage mipmaps
|
|
|
+ uint16_t xx = 0;
|
|
|
+ uint16_t yy = 0;
|
|
|
uint32_t idx = 0;
|
|
|
while (idx < m_usedLayers)
|
|
|
{
|
|
|
if (m_layers[idx].faceRegion.getType() == _type)
|
|
|
{
|
|
|
- if (m_layers[idx].packer.addRectangle(_width + 1, _height + 1, x, y) )
|
|
|
+ if (m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
@@ -365,36 +342,34 @@ uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bit
|
|
|
|
|
|
if (idx >= m_usedLayers)
|
|
|
{
|
|
|
- //do we have still room to add layers ?
|
|
|
if ( (idx + _type) > 24
|
|
|
- || m_usedFaces >= 6)
|
|
|
+ || m_usedFaces >= 6)
|
|
|
{
|
|
|
return UINT16_MAX;
|
|
|
}
|
|
|
|
|
|
- //create new layers
|
|
|
for (int ii = 0; ii < _type; ++ii)
|
|
|
{
|
|
|
- m_layers[idx + ii].faceRegion.x = 0;
|
|
|
- m_layers[idx + ii].faceRegion.y = 0;
|
|
|
- m_layers[idx + ii].faceRegion.width = m_textureSize;
|
|
|
- m_layers[idx + ii].faceRegion.height = m_textureSize;
|
|
|
- m_layers[idx + ii].faceRegion.setMask(_type, m_usedFaces, ii);
|
|
|
+ AtlasRegion& region = m_layers[idx + ii].faceRegion;
|
|
|
+ region.x = 0;
|
|
|
+ region.y = 0;
|
|
|
+ region.width = m_textureSize;
|
|
|
+ region.height = m_textureSize;
|
|
|
+ region.setMask(_type, m_usedFaces, ii);
|
|
|
}
|
|
|
|
|
|
m_usedLayers += _type;
|
|
|
m_usedFaces++;
|
|
|
|
|
|
- //add it to the created layer
|
|
|
- if (!m_layers[idx].packer.addRectangle(_width + 1, _height + 1, x, y) )
|
|
|
+ if (!m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
|
|
|
{
|
|
|
return UINT16_MAX;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
AtlasRegion& region = m_regions[m_regionCount];
|
|
|
- region.x = x;
|
|
|
- region.y = y;
|
|
|
+ region.x = xx;
|
|
|
+ region.y = yy;
|
|
|
region.width = _width;
|
|
|
region.height = _height;
|
|
|
region.mask = m_layers[idx].faceRegion.mask;
|
|
|
@@ -412,14 +387,12 @@ uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bit
|
|
|
void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer)
|
|
|
{
|
|
|
const bgfx::Memory* mem = bgfx::alloc(_region.width * _region.height * 4);
|
|
|
- //BAD!
|
|
|
memset(mem->data, 0, mem->size);
|
|
|
if (_region.getType() == AtlasRegion::TYPE_BGRA8)
|
|
|
{
|
|
|
const uint8_t* inLineBuffer = _bitmapBuffer;
|
|
|
uint8_t* outLineBuffer = m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4);
|
|
|
|
|
|
- //update the cpu buffer
|
|
|
for (int yy = 0; yy < _region.height; ++yy)
|
|
|
{
|
|
|
memcpy(outLineBuffer, inLineBuffer, _region.width * 4);
|
|
|
@@ -427,17 +400,14 @@ void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffe
|
|
|
outLineBuffer += m_textureSize * 4;
|
|
|
}
|
|
|
|
|
|
- //update the GPU buffer
|
|
|
memcpy(mem->data, _bitmapBuffer, mem->size);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
uint32_t layer = _region.getComponentIndex();
|
|
|
- //uint32_t face = _region.getFaceIndex();
|
|
|
const uint8_t* inLineBuffer = _bitmapBuffer;
|
|
|
uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4) );
|
|
|
|
|
|
- //update the cpu buffer
|
|
|
for (int yy = 0; yy < _region.height; ++yy)
|
|
|
{
|
|
|
for (int xx = 0; xx < _region.width; ++xx)
|
|
|
@@ -445,7 +415,6 @@ void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffe
|
|
|
outLineBuffer[(xx * 4) + layer] = inLineBuffer[xx];
|
|
|
}
|
|
|
|
|
|
- //update the GPU buffer
|
|
|
memcpy(mem->data + yy * _region.width * 4, outLineBuffer, _region.width * 4);
|
|
|
inLineBuffer += _region.width;
|
|
|
outLineBuffer += m_textureSize * 4;
|
|
|
@@ -476,7 +445,7 @@ void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t
|
|
|
int16_t y0 = (int16_t)( ((float)_region.y * texMult) - float(INT16_MAX) );
|
|
|
int16_t x1 = (int16_t)( (((float)_region.x + _region.width) * texMult) - float(INT16_MAX) );
|
|
|
int16_t y1 = (int16_t)( (((float)_region.y + _region.height) * texMult) - float(INT16_MAX) );
|
|
|
- int16_t w = (int16_t) ( (float(INT16_MAX) / 4.0f) * (float) _region.getComponentIndex() );
|
|
|
+ int16_t w = (int16_t)( (float(INT16_MAX) / 4.0f) * (float) _region.getComponentIndex() );
|
|
|
|
|
|
_vertexBuffer += _offset;
|
|
|
switch (_region.getFaceIndex() )
|