This commit is contained in:
bkaradzic 2013-05-29 23:12:05 -07:00
parent b8a69a700b
commit 32b95cace3
2 changed files with 79 additions and 107 deletions

View file

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

View file

@ -33,14 +33,17 @@ struct AtlasRegion
{ {
return (Type) ( (mask >> 0) & 0x0000000F); return (Type) ( (mask >> 0) & 0x0000000F);
} }
uint32_t getFaceIndex() const uint32_t getFaceIndex() const
{ {
return (mask >> 4) & 0x0000000F; return (mask >> 4) & 0x0000000F;
} }
uint32_t getComponentIndex() const uint32_t getComponentIndex() const
{ {
return (mask >> 8) & 0x0000000F; return (mask >> 8) & 0x0000000F;
} }
void setMask(Type _type, uint32_t _faceIndex, uint32_t _componentIndex) void setMask(Type _type, uint32_t _faceIndex, uint32_t _componentIndex)
{ {
mask = (_componentIndex << 8) + (_faceIndex << 4) + (uint32_t)_type; mask = (_componentIndex << 8) + (_faceIndex << 4) + (uint32_t)_type;
@ -87,14 +90,15 @@ public:
void packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const; void packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const;
/// Pack the vertex index of the region as 2 quad into an index buffer /// Pack the vertex index of the region as 2 quad into an index buffer
void packIndex(uint16_t* _indexBuffer, uint32_t _startIndex, uint32_t _startVertex) const static void packIndex(uint16_t* _indexBuffer, uint32_t _startIndex, uint32_t _startVertex)
{ {
_indexBuffer[_startIndex + 0] = _startVertex + 0; uint16_t* indices = &_indexBuffer[_startIndex];
_indexBuffer[_startIndex + 1] = _startVertex + 1; *indices++ = _startVertex + 0;
_indexBuffer[_startIndex + 2] = _startVertex + 2; *indices++ = _startVertex + 1;
_indexBuffer[_startIndex + 3] = _startVertex + 0; *indices++ = _startVertex + 2;
_indexBuffer[_startIndex + 4] = _startVertex + 2; *indices++ = _startVertex + 0;
_indexBuffer[_startIndex + 5] = _startVertex + 3; *indices++ = _startVertex + 2;
*indices++ = _startVertex + 3;
} }
/// return the TextureHandle (cube) of the atlas /// return the TextureHandle (cube) of the atlas
@ -153,6 +157,8 @@ private:
struct PackedLayer; struct PackedLayer;
PackedLayer* m_layers; PackedLayer* m_layers;
AtlasRegion* m_regions;
uint8_t* m_textureBuffer;
uint32_t m_usedLayers; uint32_t m_usedLayers;
uint32_t m_usedFaces; uint32_t m_usedFaces;
@ -162,9 +168,6 @@ private:
uint16_t m_regionCount; uint16_t m_regionCount;
uint16_t m_maxRegionCount; uint16_t m_maxRegionCount;
AtlasRegion* m_regions;
uint8_t* m_textureBuffer;
}; };
#endif // __CUBE_ATLAS_H__ #endif // __CUBE_ATLAS_H__