summaryrefslogtreecommitdiff
path: root/indra/newview/lltextureatlas.cpp
blob: d2e4b0173246a3ec04e1b812da026e91e8917ffc (plain)
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
/** 
 * @file lltextureatlas.cpp
 * @brief LLTextureAtlas class implementation.
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */
#include "llviewerprecompiledheaders.h"
#include "linden_common.h"
#include "llerror.h"
#include "llimage.h"
#include "llmath.h"
#include "llgl.h"
#include "llrender.h"
#include "lltextureatlas.h"

//-------------------
S16 LLTextureAtlas::sMaxSubTextureSize = 64 ;
S16 LLTextureAtlas::sSlotSize = 32 ;

#ifndef DEBUG_ATLAS
#define DEBUG_ATLAS 0
#endif

#ifndef DEBUG_USAGE_BITS
#define DEBUG_USAGE_BITS 0
#endif
//**************************************************************************************************************
LLTextureAtlas::LLTextureAtlas(U8 ncomponents, S16 atlas_dim) : 
    LLViewerTexture(atlas_dim * sSlotSize, atlas_dim * sSlotSize, ncomponents, TRUE),
	mAtlasDim(atlas_dim),
	mNumSlotsReserved(0),
	mMaxSlotsInAtlas(atlas_dim * atlas_dim)
{
	generateEmptyUsageBits() ;

	//generate an empty texture
	generateGLTexture() ;
	LLPointer<LLImageRaw> image_raw = new LLImageRaw(mFullWidth, mFullHeight, mComponents);
	createGLTexture(0, image_raw, 0);
	image_raw = NULL;
}

LLTextureAtlas::~LLTextureAtlas() 
{
	if(mSpatialGroupList.size() > 0)
	{
		llerrs << "Not clean up the spatial groups!" << llendl ;
	}
	releaseUsageBits() ;
}

//virtual 
S8 LLTextureAtlas::getType() const
{
	return LLViewerTexture::ATLAS_TEXTURE ;
}

void LLTextureAtlas::getTexCoordOffset(S16 col, S16 row, F32& xoffset, F32& yoffset)
{
	xoffset = (F32)col / mAtlasDim ;
	yoffset = (F32)row / mAtlasDim ;	
}

void LLTextureAtlas::getTexCoordScale(S32 w, S32 h, F32& xscale, F32& yscale)
{
	xscale = (F32)w / (mAtlasDim * sSlotSize) ;
	yscale = (F32)h / (mAtlasDim * sSlotSize) ;	
}

//insert a texture piece into the atlas
LLGLuint LLTextureAtlas::insertSubTexture(LLImageGL* source_gl_tex, S32 discard_level, const LLImageRaw* raw_image, S16 slot_col, S16 slot_row)
{
	if(!getTexName())
	{
		return 0 ;
	}

	S32 w = raw_image->getWidth() ;
	S32 h = raw_image->getHeight() ;
	if(w < 8 || w > sMaxSubTextureSize || h < 8 || h > sMaxSubTextureSize)
	{
		//size overflow
		return 0 ;
	}

	BOOL res = gGL.getTexUnit(0)->bindManual(LLTexUnit::TT_TEXTURE, getTexName());
	if (!res) 
	{
		llerrs << "bindTexture failed" << llendl;
	}
	
	GLint xoffset = sSlotSize * slot_col ;
	GLint yoffset = sSlotSize * slot_row ;

	if(!source_gl_tex->preAddToAtlas(discard_level, raw_image))
	{
		return 0 ;
	}

	glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, TRUE);
	glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, w, h,
						mGLTexturep->getPrimaryFormat(), mGLTexturep->getFormatType(), raw_image->getData());
	
	source_gl_tex->postAddToAtlas() ;
	return getTexName();
}
	
//release a sub-texture slot from the atlas
void LLTextureAtlas::releaseSlot(S16 slot_col, S16 slot_row, S8 slot_width)
{
	unmarkUsageBits(slot_width, slot_col, slot_row) ;
	mNumSlotsReserved -= slot_width * slot_width ;
}

BOOL LLTextureAtlas::isEmpty() const 
{
	return !mNumSlotsReserved ;
}
	
BOOL LLTextureAtlas::isFull(S8 to_be_reserved) const 
{
	return mNumSlotsReserved + to_be_reserved > mMaxSlotsInAtlas ;
}
F32  LLTextureAtlas::getFullness() const 
{
	return (F32)mNumSlotsReserved / mMaxSlotsInAtlas ;
}

void LLTextureAtlas::addSpatialGroup(LLSpatialGroup* groupp) 
{
	if(groupp && !hasSpatialGroup(groupp))
	{
		mSpatialGroupList.push_back(groupp);
	}
}

void LLTextureAtlas::removeSpatialGroup(LLSpatialGroup* groupp) 
{
	if(groupp)
	{
		mSpatialGroupList.remove(groupp);
	}
}

void LLTextureAtlas::clearSpatialGroup() 
{
	mSpatialGroupList.clear();
}
void LLTextureAtlas::removeLastSpatialGroup() 
{
	mSpatialGroupList.pop_back() ;
}

LLSpatialGroup* LLTextureAtlas::getLastSpatialGroup() 
{
	if(mSpatialGroupList.size() > 0)
	{
		return mSpatialGroupList.back() ;
	}
	return NULL ;
}

BOOL LLTextureAtlas::hasSpatialGroup(LLSpatialGroup* groupp) 
{
	for(std::list<LLSpatialGroup*>::iterator iter = mSpatialGroupList.begin(); iter != mSpatialGroupList.end() ; ++iter)
	{
		if(*iter == groupp)
		{
			return TRUE ;
		}
	}
	return FALSE ;
}

//--------------------------------------------------------------------------------------
//private
void LLTextureAtlas::generateEmptyUsageBits()
{
	S32 col_len = (mAtlasDim + 7) >> 3 ;
	mUsageBits = new U8*[mAtlasDim] ;
	*mUsageBits = new U8[mAtlasDim * col_len] ;

	mUsageBits[0] = *mUsageBits ;
	for(S32 i = 1 ; i < mAtlasDim ; i++)
	{
	   mUsageBits[i] = mUsageBits[i-1] + col_len ;

	   for(S32 j = 0 ; j < col_len ; j++)
	   {
		   //init by 0 for all bits.
		   mUsageBits[i][j] = 0 ;
	   }
	}

	//do not forget mUsageBits[0]!
	for(S32 j = 0 ; j < col_len ; j++)
	{
		//init by 0 for all bits.
		mUsageBits[0][j] = 0 ;
	}

	mTestBits = NULL ;
#if DEBUG_USAGE_BITS
	//------------
	//test
	mTestBits = new U8*[mAtlasDim] ;
	*mTestBits = new U8[mAtlasDim * mAtlasDim] ;
	mTestBits[0] = *mTestBits ;
	for(S32 i = 1 ; i < mAtlasDim ; i++)
	{
	   mTestBits[i] = mTestBits[i-1] + mAtlasDim ;

	   for(S32 j = 0 ; j < mAtlasDim ; j++)
	   {
		   //init by 0 for all bits.
		   mTestBits[i][j] = 0 ;
	   }
	}

	for(S32 j = 0 ; j < mAtlasDim ; j++)
	{
		//init by 0 for all bits.
		mTestBits[0][j] = 0 ;
	}
#endif
}

void LLTextureAtlas::releaseUsageBits()
{
   if(mUsageBits)
   {
       delete[] *mUsageBits ;
       delete[] mUsageBits ;
   }
   mUsageBits = NULL ;

   //test
   if( mTestBits)
   {
	   delete[] *mTestBits;
	   delete[]  mTestBits;
   }
    mTestBits = NULL ;
}

void LLTextureAtlas::markUsageBits(S8 bits_len, U8 mask, S16 col, S16 row)
{
	S16 x = col >> 3 ;
	
	for(S8 i = 0 ; i < bits_len ; i++)
	{
		mUsageBits[row + i][x] |= mask ;
	}

#if DEBUG_USAGE_BITS
	//test
	for(S8 i = row ; i < row + bits_len ; i++)
	{
		for(S8 j = col ; j < col + bits_len ; j++)
		{
			mTestBits[i][j] = 1 ;
		}
	}
#endif
}

void LLTextureAtlas::unmarkUsageBits(S8 bits_len, S16 col, S16 row)
{
	S16 x = col >> 3 ;
	U8  mask = 1 ;
	for(S8 i = 1 ; i < bits_len ; i++)
	{
		mask |= (1 << i) ;
	}
	mask <<= (col & 7) ;
	mask = ~mask ;
	
	for(S8 i = 0 ; i < bits_len ; i++)
	{
		mUsageBits[row + i][x] &= mask ;
	}

#if DEBUG_USAGE_BITS
	//test
	for(S8 i = row ; i < row + bits_len ; i++)
	{
		for(S8 j = col ; j < col + bits_len ; j++)
		{
			mTestBits[i][j] = 0 ;
		}
	}
#endif
}

//return true if any of bits in the range marked.
BOOL LLTextureAtlas::areUsageBitsMarked(S8 bits_len, U8 mask, S16 col, S16 row)
{
	BOOL ret = FALSE ;	
	S16 x = col >> 3 ;
	
	for(S8 i = 0 ; i < bits_len ; i++)
	{
		if(mUsageBits[row + i][x] & mask)
		{
			ret = TRUE ;
			break ;
			//return TRUE ;
		}
	}

#if DEBUG_USAGE_BITS
	//test
	BOOL ret2 = FALSE ;
	for(S8 i = row ; i < row + bits_len ; i++)
	{
		for(S8 j = col ; j < col + bits_len ; j++)
		{
			if(mTestBits[i][j])
			{
				ret2 = TRUE ;
			}
		}
	}

	if(ret != ret2)
	{
		llerrs << "bits map corrupted." << llendl ;
	}
#endif
	return ret ;//FALSE ;
}

//----------------------------------------------------------------------
//
//index order: Z order, i.e.: 
// |-----|-----|-----|-----|
// |  10 |  11 | 14  | 15  |
// |-----|-----|-----|-----|
// |   8 |   9 | 12  | 13  |
// |-----|-----|-----|-----|
// |   2 |   3 |   6 |   7 |
// |-----|-----|-----|-----|
// |   0 |   1 |   4 |   5 |
// |-----|-----|-----|-----|
void LLTextureAtlas::getPositionFromIndex(S16 index, S16& col, S16& row)
{
	col = 0 ;
	row = 0 ;

	S16 index_copy = index ;
	for(S16 i = 0 ; index_copy && i < 16 ; i += 2)
	{
		col |= ((index & (1 << i)) >> i) << (i >> 1) ;
		row |= ((index & (1 << (i + 1))) >> (i + 1)) << (i >> 1) ;
		index_copy >>= 2 ;
	}
}
void LLTextureAtlas::getIndexFromPosition(S16 col, S16 row, S16& index)
{
	index = 0 ;
	S16 col_copy = col ;
	S16 row_copy = row ;
	for(S16 i = 0 ; (col_copy || row_copy) && i < 16 ; i++)
	{
		index |= ((col & 1 << i) << i) | ((row & 1 << i) << ( i + 1)) ;
		col_copy >>= 1 ;
		row_copy >>= 1 ;
	}
}
//----------------------------------------------------------------------
//return TRUE if succeeds.
BOOL LLTextureAtlas::getNextAvailableSlot(S8 bits_len, S16& col, S16& row)
{
    S16 index_step = bits_len * bits_len ;

    U8 mask = 1 ;
	for(S8 i = 1 ; i < bits_len ; i++)
	{
		mask |= (1 << i) ;
	}	
   
	U8 cur_mask ;
	for(S16 index = 0 ; index < mMaxSlotsInAtlas ; index += index_step)
    {
		getPositionFromIndex(index, col, row) ;
		
		cur_mask = mask << (col & 7) ;
		if(!areUsageBitsMarked(bits_len, cur_mask, col, row))
		{
			markUsageBits(bits_len, cur_mask, col, row) ;
			mNumSlotsReserved += bits_len * bits_len ;
			
			return TRUE ;
		}
    }

   return FALSE ;
}