1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
/*
* Copyright 2013 Jeremie Roy. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "common.h"
#include <bgfx/bgfx.h>
#include <limits.h> // INT_MAX
#include <memory.h> // memset
#include <vector>
#include "cube_atlas.h"
class RectanglePacker
{
public:
RectanglePacker();
RectanglePacker(uint32_t _width, uint32_t _height);
/// non constructor initialization
void init(uint32_t _width, uint32_t _height);
/// find a suitable position for the given rectangle
/// @return true if the rectangle can be added, false otherwise
bool addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY);
/// return the used surface in squared unit
uint32_t getUsedSurface()
{
return m_usedSpace;
}
/// return the total available surface in squared unit
uint32_t getTotalSurface()
{
return m_width * m_height;
}
/// return the usage ratio of the available surface [0:1]
float getUsageRatio();
/// reset to initial state
void clear();
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();
struct Node
{
Node(int16_t _x, int16_t _y, int16_t _width) : x(_x), y(_y), width(_width)
{
}
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.
};
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(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
m_skyline.push_back(Node(1, 1, uint16_t(_width - 2) ) );
}
void RectanglePacker::init(uint32_t _width, uint32_t _height)
{
BX_CHECK(_width > 2, "_width must be > 2");
BX_CHECK(_height > 2, "_height must be > 2");
m_width = _width;
m_height = _height;
m_usedSpace = 0;
m_skyline.clear();
// We want a one pixel border around the whole atlas to avoid any artifact when
// sampling texture
m_skyline.push_back(Node(1, 1, uint16_t(_width - 2) ) );
}
bool RectanglePacker::addRectangle(uint16_t _width, uint16_t _height, uint16_t& _outX, uint16_t& _outY)
{
int best_height, best_index;
int32_t best_width;
Node* node;
Node* prev;
_outX = 0;
_outY = 0;
best_height = INT_MAX;
best_index = -1;
best_width = INT_MAX;
for (uint16_t ii = 0, num = uint16_t(m_skyline.size() ); ii < num; ++ii)
{
int32_t yy = fit(ii, _width, _height);
if (yy >= 0)
{
node = &m_skyline[ii];
if ( ( (yy + _height) < best_height)
|| ( ( (yy + _height) == best_height) && (node->width < best_width) ) )
{
best_height = uint16_t(yy) + _height;
best_index = ii;
best_width = node->width;
_outX = node->x;
_outY = uint16_t(yy);
}
}
}
if (best_index == -1)
{
return false;
}
Node newNode(_outX, _outY + _height, _width);
m_skyline.insert(m_skyline.begin() + best_index, newNode);
for (uint16_t ii = uint16_t(best_index + 1), num = uint16_t(m_skyline.size() ); ii < num; ++ii)
{
node = &m_skyline[ii];
prev = &m_skyline[ii - 1];
if (node->x < (prev->x + prev->width) )
{
uint16_t shrink = uint16_t(prev->x + prev->width - node->x);
node->x += shrink;
node->width -= shrink;
if (node->width <= 0)
{
m_skyline.erase(m_skyline.begin() + ii);
--ii;
--num;
}
else
{
break;
}
}
else
{
break;
}
}
merge();
m_usedSpace += _width * _height;
return true;
}
float RectanglePacker::getUsageRatio()
{
uint32_t total = m_width * m_height;
if (total > 0)
{
return (float)m_usedSpace / (float)total;
}
return 0.0f;
}
void RectanglePacker::clear()
{
m_skyline.clear();
m_usedSpace = 0;
// We want a one pixel border around the whole atlas to avoid any artefact when
// sampling texture
m_skyline.push_back(Node(1, 1, uint16_t(m_width - 2) ) );
}
int32_t RectanglePacker::fit(uint32_t _skylineNodeIndex, uint16_t _width, uint16_t _height)
{
int32_t width = _width;
int32_t height = _height;
const Node& baseNode = m_skyline[_skylineNodeIndex];
int32_t xx = baseNode.x, yy;
int32_t widthLeft = width;
int32_t ii = _skylineNodeIndex;
if ( (xx + width) > (int32_t)(m_width - 1) )
{
return -1;
}
yy = baseNode.y;
while (widthLeft > 0)
{
const Node& node = m_skyline[ii];
if (node.y > yy)
{
yy = node.y;
}
if ( (yy + height) > (int32_t)(m_height - 1) )
{
return -1;
}
widthLeft -= node.width;
++ii;
}
return yy;
}
void RectanglePacker::merge()
{
Node* node;
Node* next;
uint32_t ii;
for (ii = 0; ii < m_skyline.size() - 1; ++ii)
{
node = (Node*) &m_skyline[ii];
next = (Node*) &m_skyline[ii + 1];
if (node->y == next->y)
{
node->width += next->width;
m_skyline.erase(m_skyline.begin() + ii + 1);
--ii;
}
}
}
struct Atlas::PackedLayer
{
RectanglePacker packer;
AtlasRegion faceRegion;
};
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, "Invalid _textureSize %d.", _textureSize);
BX_CHECK(_maxRegionsCount >= 64 && _maxRegionsCount <= 32000, "Invalid _maxRegionsCount %d.", _maxRegionsCount);
init();
m_layers = new PackedLayer[24];
for (int ii = 0; ii < 24; ++ii)
{
m_layers[ii].packer.init(_textureSize, _textureSize);
}
m_regions = new AtlasRegion[_maxRegionsCount];
m_textureBuffer = new uint8_t[ _textureSize * _textureSize * 6 * 4 ];
memset(m_textureBuffer, 0, _textureSize * _textureSize * 6 * 4);
m_textureHandle = bgfx::createTextureCube(_textureSize
, false
, 1
, bgfx::TextureFormat::BGRA8
);
}
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, "_regionCount %d, _maxRegionsCount %d", _regionCount, _maxRegionsCount);
init();
m_regions = new AtlasRegion[_regionCount];
m_textureBuffer = new uint8_t[getTextureBufferSize()];
memcpy(m_regions, _regionBuffer, _regionCount * sizeof(AtlasRegion) );
memcpy(m_textureBuffer, _textureBuffer, getTextureBufferSize() );
m_textureHandle = bgfx::createTextureCube(_textureSize
, false
, 1
, bgfx::TextureFormat::BGRA8
, BGFX_TEXTURE_NONE
, bgfx::makeRef(m_textureBuffer, getTextureBufferSize() )
);
}
Atlas::~Atlas()
{
bgfx::destroyTexture(m_textureHandle);
delete [] m_layers;
delete [] m_regions;
delete [] m_textureBuffer;
}
void Atlas::init()
{
m_texelSize = float(UINT16_MAX) / float(m_textureSize);
float texelHalf = m_texelSize/2.0f;
switch (bgfx::getRendererType() )
{
case bgfx::RendererType::Direct3D9:
m_texelOffset[0] = 0.0f;
m_texelOffset[1] = 0.0f;
break;
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12:
m_texelOffset[0] = texelHalf;
m_texelOffset[1] = texelHalf;
break;
default:
m_texelOffset[0] = texelHalf;
m_texelOffset[1] = -texelHalf;
break;
}
}
uint16_t Atlas::addRegion(uint16_t _width, uint16_t _height, const uint8_t* _bitmapBuffer, AtlasRegion::Type _type, uint16_t outline)
{
if (m_regionCount >= m_maxRegionCount)
{
return UINT16_MAX;
}
uint16_t xx = 0;
uint16_t yy = 0;
uint32_t idx = 0;
while (idx < m_usedLayers)
{
if (m_layers[idx].faceRegion.getType() == _type
&& m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
{
break;
}
idx++;
}
if (idx >= m_usedLayers)
{
if ( (idx + _type) > 24
|| m_usedFaces >= 6)
{
return UINT16_MAX;
}
for (int ii = 0; ii < _type; ++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++;
if (!m_layers[idx].packer.addRectangle(_width + 1, _height + 1, xx, yy) )
{
return UINT16_MAX;
}
}
AtlasRegion& region = m_regions[m_regionCount];
region.x = xx;
region.y = yy;
region.width = _width;
region.height = _height;
region.mask = m_layers[idx].faceRegion.mask;
updateRegion(region, _bitmapBuffer);
region.x += outline;
region.y += outline;
region.width -= (outline * 2);
region.height -= (outline * 2);
return m_regionCount++;
}
void Atlas::updateRegion(const AtlasRegion& _region, const uint8_t* _bitmapBuffer)
{
uint32_t size = _region.width * _region.height * 4;
if (0 < size)
{
const bgfx::Memory* mem = bgfx::alloc(size);
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);
for (int yy = 0; yy < _region.height; ++yy)
{
memcpy(outLineBuffer, inLineBuffer, _region.width * 4);
inLineBuffer += _region.width * 4;
outLineBuffer += m_textureSize * 4;
}
memcpy(mem->data, _bitmapBuffer, mem->size);
}
else
{
uint32_t layer = _region.getComponentIndex();
const uint8_t* inLineBuffer = _bitmapBuffer;
uint8_t* outLineBuffer = (m_textureBuffer + _region.getFaceIndex() * (m_textureSize * m_textureSize * 4) + ( ( (_region.y * m_textureSize) + _region.x) * 4) );
for (int yy = 0; yy < _region.height; ++yy)
{
for (int xx = 0; xx < _region.width; ++xx)
{
outLineBuffer[(xx * 4) + layer] = inLineBuffer[xx];
}
memcpy(mem->data + yy * _region.width * 4, outLineBuffer, _region.width * 4);
inLineBuffer += _region.width;
outLineBuffer += m_textureSize * 4;
}
}
bgfx::updateTextureCube(m_textureHandle, 0, (uint8_t)_region.getFaceIndex(), 0, _region.x, _region.y, _region.width, _region.height, mem);
}
}
void Atlas::packFaceLayerUV(uint32_t _idx, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
{
packUV(m_layers[_idx].faceRegion, _vertexBuffer, _offset, _stride);
}
void Atlas::packUV(uint16_t _regionHandle, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
{
const AtlasRegion& region = m_regions[_regionHandle];
packUV(region, _vertexBuffer, _offset, _stride);
}
static void writeUV(uint8_t* _vertexBuffer, int16_t _x, int16_t _y, int16_t _z, int16_t _w)
{
uint16_t* xyzw = (uint16_t*)_vertexBuffer;
xyzw[0] = _x;
xyzw[1] = _y;
xyzw[2] = _z;
xyzw[3] = _w;
}
void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t _offset, uint32_t _stride) const
{
int16_t x0 = (int16_t)( ( (float)_region.x * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) );
int16_t y0 = (int16_t)( ( (float)_region.y * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) );
int16_t x1 = (int16_t)( ( ( (float)_region.x + _region.width) * m_texelSize + m_texelOffset[0]) - float(INT16_MAX) );
int16_t y1 = (int16_t)( ( ( (float)_region.y + _region.height) * m_texelSize + m_texelOffset[1]) - float(INT16_MAX) );
int16_t ww = (int16_t)( (float(INT16_MAX) / 4.0f) * (float)_region.getComponentIndex() );
_vertexBuffer += _offset;
switch (_region.getFaceIndex() )
{
case 0: // +X
x0 = -x0;
x1 = -x1;
y0 = -y0;
y1 = -y1;
writeUV(_vertexBuffer, INT16_MAX, y0, x0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, INT16_MAX, y1, x0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, INT16_MAX, y1, x1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, INT16_MAX, y0, x1, ww); _vertexBuffer += _stride;
break;
case 1: // -X
y0 = -y0;
y1 = -y1;
writeUV(_vertexBuffer, INT16_MIN, y0, x0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, INT16_MIN, y1, x0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, INT16_MIN, y1, x1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, INT16_MIN, y0, x1, ww); _vertexBuffer += _stride;
break;
case 2: // +Y
writeUV(_vertexBuffer, x0, INT16_MAX, y0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x0, INT16_MAX, y1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, INT16_MAX, y1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, INT16_MAX, y0, ww); _vertexBuffer += _stride;
break;
case 3: // -Y
y0 = -y0;
y1 = -y1;
writeUV(_vertexBuffer, x0, INT16_MIN, y0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x0, INT16_MIN, y1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, INT16_MIN, y1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, INT16_MIN, y0, ww); _vertexBuffer += _stride;
break;
case 4: // +Z
y0 = -y0;
y1 = -y1;
writeUV(_vertexBuffer, x0, y0, INT16_MAX, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x0, y1, INT16_MAX, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, y1, INT16_MAX, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, y0, INT16_MAX, ww); _vertexBuffer += _stride;
break;
case 5: // -Z
x0 = -x0;
x1 = -x1;
y0 = -y0;
y1 = -y1;
writeUV(_vertexBuffer, x0, y0, INT16_MIN, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x0, y1, INT16_MIN, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, y1, INT16_MIN, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, y0, INT16_MIN, ww); _vertexBuffer += _stride;
break;
}
}
|