triListOpt.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TRI_LIST_OPT_H_
  23. #define _TRI_LIST_OPT_H_
  24. #include "core/util/tVector.h"
  25. namespace TriListOpt
  26. {
  27. typedef U32 IndexType;
  28. const U32 MaxSizeVertexCache = 32;
  29. struct VertData
  30. {
  31. S32 cachePosition;
  32. F32 score;
  33. U32 numReferences;
  34. U32 numUnaddedReferences;
  35. S32 *triIndex;
  36. VertData() : cachePosition(-1), score(0.0f), numReferences(0), numUnaddedReferences(0), triIndex(NULL) {}
  37. ~VertData() { if(triIndex != NULL) delete [] triIndex; triIndex = NULL; }
  38. };
  39. struct TriData
  40. {
  41. bool isInList;
  42. F32 score;
  43. U32 vertIdx[3];
  44. TriData() : isInList(false), score(0.0f) { dMemset(vertIdx, 0, sizeof(vertIdx)); }
  45. };
  46. class LRUCacheModel
  47. {
  48. struct LRUCacheEntry
  49. {
  50. LRUCacheEntry *next;
  51. U32 vIdx;
  52. VertData *vData;
  53. LRUCacheEntry() : next(NULL), vIdx(0), vData(NULL) {}
  54. };
  55. LRUCacheEntry *mCacheHead;
  56. public:
  57. LRUCacheModel() : mCacheHead(NULL) {}
  58. ~LRUCacheModel();
  59. void enforceSize(const dsize_t maxSize, Vector<U32> &outTrisToUpdate);
  60. void useVertex(const U32 vIdx, VertData *vData);
  61. S32 getCachePosition(const U32 vIdx);
  62. };
  63. /// This method will look at the index buffer for a triangle list, and generate
  64. /// a new index buffer which is optimized using Tom Forsyth's paper:
  65. /// "Linear-Speed Vertex Cache Optimization"
  66. /// http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
  67. /// @param numVerts Number of vertices indexed by the 'indices'
  68. /// @param numIndices Number of elements in both 'indices' and 'outIndices'
  69. /// @param indices Input index buffer
  70. /// @param outIndices Output index buffer
  71. ///
  72. /// @note Both 'indices' and 'outIndices' can point to the same memory.
  73. void OptimizeTriangleOrdering(const dsize_t numVerts, const dsize_t numIndices, const U32 *indices, IndexType *outIndices);
  74. namespace FindVertexScore
  75. {
  76. const F32 CacheDecayPower = 1.5f;
  77. const F32 LastTriScore = 0.75f;
  78. const F32 ValenceBoostScale = 2.0f;
  79. const F32 ValenceBoostPower = 0.5f;
  80. F32 score(const VertData &vertexData);
  81. };
  82. };
  83. #endif