00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Rafal Jaroszkiewicz 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: OBJ_XformCache.h ( OBJ Library, C++) 00015 * 00016 * COMMENTS: 00017 */ 00018 00019 #ifndef __OBJ_XformCacher__ 00020 #define __OBJ_XformCacher__ 00021 00022 #include "OBJ_API.h" 00023 #include <UT/UT_Cache.h> 00024 #include <UT/UT_HashTable.h> 00025 #include <UT/UT_LinkList.h> 00026 #include <OP/OP_Version.h> 00027 00028 00029 class UT_DMatrix4; 00030 class CMD_Args; 00031 class OBJ_XformCacheData; 00032 class OBJ_XformCacheTag; 00033 00034 /// The OBJ_XformCache maintains a table of recently calculated transforms 00035 /// which describe world position and orientation of objects. 00036 /// 00037 /// It can also watch memory usage to see what transform matrices should be 00038 /// removed from the table. 00039 class OBJ_API OBJ_XformCache : public UT_Cache 00040 { 00041 public: 00042 00043 // specifies transforms that a single object is able to cache 00044 enum OBJ_XformType 00045 { 00046 WORLD_XFORM, //world transform of an object 00047 LOCAL_XFORM //local transform of an object 00048 }; 00049 00050 // specifies the results returned by cache lookups 00051 enum OBJ_LookupStatus 00052 { 00053 HIT, // lookup is successful 00054 MISS_VERSION, // lookup failed: invalidated data 00055 MISS_DATA // lookup failed: data never cached 00056 }; 00057 00058 // some constants 00059 enum 00060 { 00061 // default memory limit for the cache in bytes NB: for a production size 00062 // scene with three characters there are around 1000 bones, which take 00063 // about 350kB per frame to cache 1000 objects. So 300 frames take 00064 // around 100MB of cache memory. 00065 DEFAULT_CACHE_SIZE_KB = 50*1024 00066 }; 00067 00068 00069 public: 00070 OBJ_XformCache(); 00071 ~OBJ_XformCache(); 00072 00073 /// Obtains the transform matrix cache used by all objects 00074 static OBJ_XformCache *getCache(); 00075 00076 /// querries the cache for the transform matrix 00077 /// INPUTS: 00078 /// id - the unique identification number of the operator 00079 /// time - the time at which the xform is applied to the object 00080 /// type - tranformation type 00081 /// version - version of parameters used to construct the xform 00082 /// OUTPUT: 00083 /// xform - on cache hit, the transform matrix, otherwise unchanged 00084 /// RETURNS: 00085 /// the status of the lookup (hit or miss) 00086 /// 00087 OBJ_LookupStatus getXform( int id, float time, 00088 OBJ_XformType type, 00089 OP_VERSION version, 00090 UT_DMatrix4 &xform ); 00091 00092 /// sets the transform matrix in the cache 00093 /// INPUTS: 00094 /// id - the unique identification number of the operator 00095 /// time - the time at which the xform is applied to the object 00096 /// type - tranformation type 00097 /// version - version of parameters used to construct the xform 00098 /// RETURNS: 00099 /// true: if succeeded 00100 /// false: if failed 00101 /// 00102 bool setXform( int id, float time, 00103 OBJ_XformType type, 00104 OP_VERSION version, 00105 const UT_DMatrix4 &xform ); 00106 00107 // TODO: add higher performance version of getXform, which returns 00108 // cache data container to be set directly by cache caller 00109 // on cache miss (determined by version ID) 00110 00111 00112 /// Used as the interface to objcache, which is installed in 00113 /// MOT_Command.C 00114 void parseCommand( CMD_Args &args ); 00115 00116 00117 /// Sets the new memory limit for the cache size in bytes 00118 void setMemoryLimit( int64 max_memory ); 00119 00120 /// Clears the cache and frees memory 00121 void clear(); 00122 00123 protected: // 00124 enum OBJ_MemoryState 00125 { 00126 MEMORY_CHECK_NEVER, // never track memory size 00127 MEMORY_CHECK_ALWAYS // always watch for memory limit 00128 }; 00129 00130 protected: 00131 // the UT_Cache interface 00132 virtual const char *utGetCacheName() const 00133 {return "Object Transform Cache";} 00134 virtual int64 utGetCurrentSize() 00135 { return entries()* getEntryMemUsage(); } 00136 00137 virtual bool utHasMaxSize() const { return true; } 00138 virtual int64 utGetMaxSize() 00139 { return myMaxEntries* getEntryMemUsage(); } 00140 virtual void utSetMaxSize(int64 size); 00141 00142 virtual int64 utReduceCacheSizeBy(int64 amount); 00143 00144 private: //methods 00145 // finds the entry, and if found also sets the hash_tag to be the 00146 // tag used in the hash table for that cache data 00147 OBJ_XformCacheData *findEntry( int id, float time, 00148 OBJ_XformType type, 00149 OBJ_XformCacheTag *&hash_tag )const; 00150 // sets the give tag as most recently used 00151 void setMRU( OBJ_XformCacheTag *hash_tag ); 00152 // remove least recently used entry 00153 bool removeLRU(); 00154 // returns the memory size of a cache entry 00155 int64 getEntryMemUsage(); 00156 // returns number of entries in the cache 00157 int64 entries() const { return myData->entries(); } 00158 00159 private: 00160 /// This stores the transformation matrices computed previously 00161 UT_HashTable *myData; 00162 00163 /// Maintains a list sorted by the usage (access) order 00164 UT_LinkList myAccessOrder; 00165 00166 // Memory management variables 00167 int64 myMaxEntries; //memory limit (# of xforms) 00168 OBJ_MemoryState myMemoryState; //if should respect limit 00169 }; 00170 00171 00172 #endif 00173
1.5.9