Developer Documentation
GPUCacheOptimizer.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 #ifndef ACG_GPU_CACHE_OPT_HH
51 #define ACG_GPU_CACHE_OPT_HH
52 
53 
54 //== INCLUDES =================================================================
55 
56 #include <ACG/Config/ACGDefines.hh>
57 
58 //== FORWARDDECLARATIONS ======================================================
59 
60 //== NAMESPACES ===============================================================
61 
62 namespace ACG {
63 
64 //== CLASS DEFINITION =========================================================
65 
66 
72 class ACGDLLEXPORT GPUCacheOptimizer
73 {
74 private:
75  // copy ops are private to prevent copying
76  GPUCacheOptimizer(const GPUCacheOptimizer&); // no implementation
77  GPUCacheOptimizer& operator=(const GPUCacheOptimizer&); // no implementation
78 public:
79 
89  GPUCacheOptimizer(unsigned int NumTris, unsigned int NumVerts, unsigned int IndexSize, const void* pIndices);
90  virtual ~GPUCacheOptimizer(void);
91 
92 
100  const unsigned int* GetTriangleMap() const {return m_pTriMap;}
101 
110  void WriteIndexBuffer(unsigned int DstIndexSize, void* pDst);
111 
112 
128  static void OptimizeVertices(unsigned int NumTris, unsigned int NumVerts, unsigned int IndexSize,
129  const void* pIndices, unsigned int* pVertMap);
130  // this function is declared static to be able to operate on the whole model
131  // instead of just a subset of it
132  // example use:
133  // - optimize triangle list per material group
134  // - optimize vertex buffer on whole mesh independently of material subsets
135 
136 
158  static void RemapVertices(unsigned int NumTris, unsigned int NumVerts, const unsigned int* pVertMap,
159  unsigned int IndexSize, void* pInOutIndices, unsigned int VertexStride, void* pInOutVertices);
160 
161 
165  unsigned int ComputeNumberOfVertexTransformations(unsigned int VertexCacheSize = 16);
166 
171  float ComputeACMR(unsigned int VertexCacheSize = 16);
172 
179  float ComputeATVR(unsigned int VertexCacheSize = 16);
180 
181 protected:
182  // look up m_pIndices w.r.t. index size at location 'i'
183  unsigned int GetIndex(unsigned int i) const;
184 
185  static unsigned int GetIndex(unsigned int i, unsigned int IndexSize, const void* pIB);
186  static void SetIndex(unsigned int i, unsigned int val, unsigned int IndexSize, void* pIB);
187 
188  virtual void MakeAbstract() = 0;
189 
190 protected:
191 
192 
193  unsigned int m_NumVerts;
194  unsigned int m_NumTris;
195 
200  unsigned int* m_pTriMap;
201 
202 private:
203  unsigned int m_IndexSize;
204  const void* m_pIndices;
205 
206 
207  unsigned int m_NumTransformations;
208 
209 
210 protected:
215  struct Opt_Vertex
216  {
217  // Opt_Vertex(): iCachePos(-1), fScore(0.0f), iNumTrisTotal(0), iNumTrisLeft(0), pTris(0) {}
218  // ~Opt_Vertex() {delete [] pTris;}
219 
220  int iCachePos;
221  float fScore;
226  unsigned int* pTris;
227 
229  void FindScore(unsigned int MaxSizeVertexCache);
230 
231  void RemoveTriFromList(unsigned int tri);
232  };
233 
234  struct Opt_Tris
235  {
236  Opt_Tris() : bAdded(0), fScore(0.0f)
237  { v[0] = v[1] = v[2] = 0xDEADBEEF;}
238 
239  int bAdded;
241  float fScore;
243  unsigned int v[3];
244 
245  inline void FindScore(const Opt_Vertex* pVertices)
246  {
247  fScore = 0.0f;
248  for (int i = 0; i < 3; ++i)
249  fScore += pVertices[v[i]].fScore;
250  }
251  };
252 };
253 
255 
262 class ACGDLLEXPORT GPUCacheOptimizerTipsify : public GPUCacheOptimizer
263 {
264 public:
265 
274  GPUCacheOptimizerTipsify(unsigned int CacheSize,
275  unsigned int NumTris,
276  unsigned int NumVerts,
277  unsigned int IndexSize,
278  const void* pIndices);
279 
280 private:
281 
282  void MakeAbstract(){}
283 
285  struct RingStack
286  {
287  private:
288  unsigned int* pStack;
289  unsigned int uiStart, uiLen;
290  unsigned int uiSize;
291 
292  inline unsigned int pos(unsigned int i) const
293  {
294  unsigned int t = uiStart + i;
295  if (t >= uiLen) t -= uiLen;
296  return t;
297  }
298 
299  public:
300  RingStack(unsigned int _uiSize) :
301  uiStart(0),
302  uiLen(0),
303  uiSize(_uiSize)
304  {
305  pStack = new unsigned int[uiSize];
306  }
307 
308  RingStack(const RingStack& _other)
309  {
310  // Copy meta data
311  uiSize = _other.uiSize;
312  uiLen = _other.uiLen;
313  uiStart = _other.uiStart;
314 
315  // Create empty storage
316  pStack = new unsigned int[uiSize];
317 
318  // Copy storage from original to current storage
319  for (unsigned int i = 0 ; i < uiSize; ++i )
320  pStack[i] = _other.pStack[i];
321 
322  }
323 
324  ~RingStack() {delete [] pStack;}
325 
326  unsigned int length() const {return uiLen;}
327  unsigned int size() const {return uiSize;}
328 
329  inline void push(unsigned int v)
330  {
331  if (uiLen == uiSize)
332  {
333  pStack[uiStart++] = v;
334  if (uiStart == uiSize) uiStart = 0;
335  }
336  else
337  pStack[pos(uiLen++)] = v; // pos(uiLen) gives the index of the last element + 1
338  }
339 
340  inline unsigned int pop()
341  {
342  if (uiSize && uiLen) return pStack[pos(--uiLen)];
343  return 0xFFFFFFFF;
344  }
345  };
346 };
347 
352 class ACGDLLEXPORT GPUCacheEfficiencyTester : public GPUCacheOptimizer
353 {
354 public:
355  GPUCacheEfficiencyTester(unsigned int NumTris, unsigned int NumVerts, unsigned int IndexSize, const void* pIndices);
356 
357 private:
358  void MakeAbstract(){}
359 };
360 
361 
362 
363 //=============================================================================
364 } // namespace ACG
365 //=============================================================================
366 #endif // ACG_GPU_CACHE_OPT_HH defined
367 //=============================================================================
368 
unsigned int length() const
current stack length
int iNumTrisLeft
tris left to add to final result
Simple and fast fixed size stack used in tipsify implementation.
const unsigned int * GetTriangleMap() const
Retrieves the map from dst triangle to src triangle. how to remap: for each triangle t in DstTriBuffe...
float fScore
sum of scores of vertices
int iNumTrisTotal
tris using this vertex
unsigned int size() const
reserved stack size i.e. maximum length
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51