mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-28 18:45:54 -05:00
Cleanup.
This commit is contained in:
parent
b8a69a700b
commit
32b95cace3
2 changed files with 79 additions and 107 deletions
|
@ -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;
|
||||
uint32_t flags = 0;
|
||||
|
||||
//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;
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
|
|
@ -33,14 +33,17 @@ struct AtlasRegion
|
|||
{
|
||||
return (Type) ( (mask >> 0) & 0x0000000F);
|
||||
}
|
||||
|
||||
uint32_t getFaceIndex() const
|
||||
{
|
||||
return (mask >> 4) & 0x0000000F;
|
||||
}
|
||||
|
||||
uint32_t getComponentIndex() const
|
||||
{
|
||||
return (mask >> 8) & 0x0000000F;
|
||||
}
|
||||
|
||||
void setMask(Type _type, uint32_t _faceIndex, uint32_t _componentIndex)
|
||||
{
|
||||
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;
|
||||
|
||||
/// 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;
|
||||
_indexBuffer[_startIndex + 1] = _startVertex + 1;
|
||||
_indexBuffer[_startIndex + 2] = _startVertex + 2;
|
||||
_indexBuffer[_startIndex + 3] = _startVertex + 0;
|
||||
_indexBuffer[_startIndex + 4] = _startVertex + 2;
|
||||
_indexBuffer[_startIndex + 5] = _startVertex + 3;
|
||||
uint16_t* indices = &_indexBuffer[_startIndex];
|
||||
*indices++ = _startVertex + 0;
|
||||
*indices++ = _startVertex + 1;
|
||||
*indices++ = _startVertex + 2;
|
||||
*indices++ = _startVertex + 0;
|
||||
*indices++ = _startVertex + 2;
|
||||
*indices++ = _startVertex + 3;
|
||||
}
|
||||
|
||||
/// return the TextureHandle (cube) of the atlas
|
||||
|
@ -153,6 +157,8 @@ private:
|
|||
|
||||
struct PackedLayer;
|
||||
PackedLayer* m_layers;
|
||||
AtlasRegion* m_regions;
|
||||
uint8_t* m_textureBuffer;
|
||||
|
||||
uint32_t m_usedLayers;
|
||||
uint32_t m_usedFaces;
|
||||
|
@ -162,9 +168,6 @@ private:
|
|||
|
||||
uint16_t m_regionCount;
|
||||
uint16_t m_maxRegionCount;
|
||||
|
||||
AtlasRegion* m_regions;
|
||||
uint8_t* m_textureBuffer;
|
||||
};
|
||||
|
||||
#endif // __CUBE_ATLAS_H__
|
||||
|
|
Loading…
Reference in a new issue