aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/llagparray.h
blob: 2569d95748594f398fe1aac339513de88b3a3125 (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
/** 
 * @file llagparray.h
 * @brief LLAGPArray - arrays used for rendering w/ AGP memory (if on)
 *
 * Copyright (c) 2001-2007, Linden Research, Inc.
 * 
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 */

#ifndef LL_LLAGPARRAY_H
#define LL_LLAGPARRAY_H

#include <stdlib.h>
#include "llagpmempool.h"

class LLAGPMemBlock;

template <class Type> class LLAGPArray
{
public:
	enum
	{
		OKAY = 0,
		FAIL = -1
	};

	inline LLAGPArray(const S32 size=0, const U32 target=0);
	inline ~LLAGPArray();

	inline void init();
	inline void destroy();

	inline void flushAGP();
	inline void setDirty()							{ mDirty = TRUE; }
	inline BOOL isDirty() const						{ return mDirty; }

	inline BOOL setUseAGP(const BOOL on = TRUE);	// Returns false if AGP memory is not available
	inline BOOL isAGP() const						{ return mAGPp ? TRUE : FALSE; }
	LLAGPMemBlock *getAGPMemBlock() const			{ return mAGPp; }

	inline BOOL realloc(U32 newsize);
	inline S32  getMax() const						{ return mMaxObj; }
	inline S32  count() const;
	inline S32  shrinkTo(S32 newcount);
	inline void reset(S32 reserve_count);
	
	inline S32 find      (const Type &obj) const;
	inline S32 fastRemove(const S32 i);
	inline S32 safeRemove(const S32 r);
	inline S32 remove    (const S32 i);
	inline S32 removeObj (const Type &obj);
	inline S32 removeLast()							{ return (mNumObj > 0 ? mNumObj-- : 0); }

	inline Type* reserve_block(const U32 num);

	inline S32         put(const Type &obj);
	inline const Type& get(const S32 i) const;
	inline Type&       get(const S32 i);

	inline BOOL        sync     ();
	inline BOOL        syncColor();

	inline const Type* getMem  () const;
	inline const S32   getIndex(const Type *objp) const { return objp - mMemp; }
	inline const Type& operator[](const S32 i) const;
	inline Type&       operator[](const S32 i);

	void bindGLVertexPointer(const U32 stride, const U32 offset);
	void bindGLTexCoordPointer(const U32 stride, const U32 offset);
	void bindGLNormalPointer(const U32 stride, const U32 offset);
	void bindGLBinormalPointer(const S32 index, const U32 stride, const U32 offset);
	void bindGLColorPointer(const U32 stride, const U32 offset);
	void bindGLVertexWeightPointer(const S32 index, const U32 stride, const U32 offset);
	void bindGLVertexClothingWeightPointer(const S32 index, const U32 stride, const U32 offset);

	void copyToMem(const S32 offset, const U8 *source, const S32 size);

	U8*  getScratchMemory();

	U32  createFence()        { return (mAGPp ? mAGPp->createFence() : 0); }
	void deleteFence(const U32 fence) { if (mAGPp) { mAGPp->deleteFence(fence); } }
	void sendFence(U32 fence) { if (mAGPp) mAGPp->sendFence(fence); }
	void waitFence(U32 fence) { if (mAGPp) mAGPp->waitFence(fence); }

	S32			getSysMemUsage() { return sizeof(Type)*mMaxObj; }

protected:
	BOOL		mDirty;
	BOOL		mUseAGP;
	U32			mNumObj;
	U32			mMaxObj;
	U8			*mUnalignedMemp;
	Type		*mMemp;
	U32			mTarget;	//for VBO implementations of this class, store the type of buffer this is

	LLAGPMemBlock *mAGPp;

public:
	BOOL       mSynced; // for graph only -- not critical
	
public:
	static S32 sNumRealloced;
	static S32 sBytesRealloced;
	static char* sTypeName;
};

template <class Type> S32 LLAGPArray<Type>::sNumRealloced = 0;
template <class Type> S32 LLAGPArray<Type>::sBytesRealloced = 0;

// constructor is in .inl file because it calls realloc()
// which access gPipeline (yuck!)

template <class Type>
LLAGPArray<Type>::~LLAGPArray()
{
	destroy();
}

template <class Type> void
LLAGPArray<Type>::destroy()
{
	realloc(0);
	delete mAGPp;
	mAGPp = NULL;
}

template <class Type>
const Type *LLAGPArray<Type>::getMem() const
{
	return mMemp;
}

template <class Type>
const Type& LLAGPArray<Type>::get(const S32 i) const
{
	llassert(i < (S32)mNumObj);
	return mMemp[i];
}

template <class Type>
Type& LLAGPArray<Type>::get(const S32 i)
{
	setDirty();
	llassert(i < (S32)mNumObj);
	return mMemp[i];
}

template <class Type>
const Type& LLAGPArray<Type>::operator[](const S32 i) const
{ 
	return get(i); 
}

template <class Type>
Type& LLAGPArray<Type>::operator[](const S32 i)
{
	return get(i); 
}

template <class Type>
S32 LLAGPArray<Type>::fastRemove(const S32 i)
{
	if (i < 0 || mNumObj <= 0) return FAIL;
	setDirty();
	mMemp[i] = mMemp[--mNumObj];

	return OKAY;
}

template <class Type>
S32 LLAGPArray<Type>::remove(const S32 i)
{
	setDirty();
	return fastRemove(i);
}

template <class Type>
S32 LLAGPArray<Type>::removeObj(const Type& obj)
{
	setDirty();

	S32 ind = find(obj);
	if (ind >= 0)
	{
		return fastRemove(ind);
	}
	return FAIL;
}

template <class Type>
S32	LLAGPArray<Type>::count() const
{
	return mNumObj;
}


#endif // LL_LLAGPARRAY_H