Panda3D
 All Classes Functions Variables Enumerations
preparedGraphicsObjects.h
00001 // Filename: preparedGraphicsObjects.h
00002 // Created by:  drose (19Feb04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef PREPAREDGRAPHICSOBJECTS_H
00016 #define PREPAREDGRAPHICSOBJECTS_H
00017 
00018 #include "pandabase.h"
00019 #include "referenceCount.h"
00020 #include "texture.h"
00021 #include "geom.h"
00022 #include "geomVertexArrayData.h"
00023 #include "geomPrimitive.h"
00024 #include "shader.h"
00025 #include "pointerTo.h"
00026 #include "pStatCollector.h"
00027 #include "pset.h"
00028 #include "reMutex.h"
00029 #include "bufferResidencyTracker.h"
00030 #include "adaptiveLru.h"
00031 
00032 class TextureContext;
00033 class GeomContext;
00034 class ShaderContext;
00035 class VertexBufferContext;
00036 class IndexBufferContext;
00037 class GraphicsStateGuardianBase;
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //       Class : PreparedGraphicsObjects
00041 // Description : A table of objects that are saved within the graphics
00042 //               context for reference by handle later.  Generally,
00043 //               this represents things like OpenGL texture objects or
00044 //               display lists (or their equivalent on other
00045 //               platforms).
00046 //
00047 //               This object simply records the pointers to the
00048 //               context objects created by the individual GSG's;
00049 //               these context objects will contain enough information
00050 //               to reference or release the actual object stored
00051 //               within the graphics context.
00052 //
00053 //               These tables may potentially be shared between
00054 //               related graphics contexts, hence their storage here
00055 //               in a separate object rather than as a part of the
00056 //               GraphicsStateGuardian.
00057 ////////////////////////////////////////////////////////////////////
00058 class EXPCL_PANDA_GOBJ PreparedGraphicsObjects : public ReferenceCount {
00059 public:
00060   PreparedGraphicsObjects();
00061   ~PreparedGraphicsObjects();
00062 
00063 PUBLISHED:
00064   INLINE const string &get_name() const;
00065 
00066   void set_graphics_memory_limit(size_t limit);
00067   INLINE size_t get_graphics_memory_limit() const;
00068   void show_graphics_memory_lru(ostream &out) const;
00069   void show_residency_trackers(ostream &out) const;
00070 
00071   INLINE void release_all();
00072   INLINE int get_num_queued() const;
00073   INLINE int get_num_prepared() const;
00074 
00075   void enqueue_texture(Texture *tex);
00076   bool is_texture_queued(const Texture *tex) const;
00077   bool dequeue_texture(Texture *tex);
00078   bool is_texture_prepared(const Texture *tex) const;
00079   void release_texture(TextureContext *tc);
00080   void release_texture(Texture *tex);
00081   int release_all_textures();
00082   int get_num_queued_textures() const;
00083   int get_num_prepared_textures() const;
00084 
00085   TextureContext *prepare_texture_now(Texture *tex, int view, 
00086                                       GraphicsStateGuardianBase *gsg);
00087 
00088   void enqueue_geom(Geom *geom);
00089   bool is_geom_queued(const Geom *geom) const;
00090   bool dequeue_geom(Geom *geom);
00091   bool is_geom_prepared(const Geom *geom) const;
00092   void release_geom(GeomContext *gc);
00093   int release_all_geoms();
00094   int get_num_queued_geoms() const;
00095   int get_num_prepared_geoms() const;
00096 
00097   GeomContext *prepare_geom_now(Geom *geom, GraphicsStateGuardianBase *gsg);
00098 
00099   void enqueue_shader(Shader *shader);
00100   bool is_shader_queued(const Shader *shader) const;
00101   bool dequeue_shader(Shader *shader);
00102   bool is_shader_prepared(const Shader *shader) const;
00103   void release_shader(ShaderContext *sc);
00104   int release_all_shaders();
00105   int get_num_queued_shaders() const;
00106   int get_num_prepared_shaders() const;
00107 
00108   ShaderContext *prepare_shader_now(Shader *shader, GraphicsStateGuardianBase *gsg);
00109 
00110   void enqueue_vertex_buffer(GeomVertexArrayData *data);
00111   bool is_vertex_buffer_queued(const GeomVertexArrayData *data) const;
00112   bool dequeue_vertex_buffer(GeomVertexArrayData *data);
00113   bool is_vertex_buffer_prepared(const GeomVertexArrayData *data) const;
00114   void release_vertex_buffer(VertexBufferContext *vbc);
00115   int release_all_vertex_buffers();
00116   int get_num_queued_vertex_buffers() const;
00117   int get_num_prepared_vertex_buffers() const;
00118 
00119   VertexBufferContext *
00120   prepare_vertex_buffer_now(GeomVertexArrayData *data,
00121                             GraphicsStateGuardianBase *gsg);
00122 
00123   void enqueue_index_buffer(GeomPrimitive *data);
00124   bool is_index_buffer_queued(const GeomPrimitive *data) const;
00125   bool dequeue_index_buffer(GeomPrimitive *data);
00126   bool is_index_buffer_prepared(const GeomPrimitive *data) const;
00127   void release_index_buffer(IndexBufferContext *ibc);
00128   int release_all_index_buffers();
00129   int get_num_queued_index_buffers() const;
00130   int get_num_prepared_index_buffers() const;
00131 
00132   IndexBufferContext *
00133   prepare_index_buffer_now(GeomPrimitive *data,
00134                            GraphicsStateGuardianBase *gsg);
00135 
00136 public:
00137   void begin_frame(GraphicsStateGuardianBase *gsg,
00138                    Thread *current_thread);
00139   void end_frame(Thread *current_thread);
00140 
00141 private:
00142   static string init_name();
00143 
00144 private:
00145   typedef phash_set<TextureContext *, pointer_hash> Textures;
00146   typedef phash_set< PT(Texture) > EnqueuedTextures;
00147   typedef phash_set<GeomContext *, pointer_hash> Geoms;
00148   typedef phash_set< PT(Geom) > EnqueuedGeoms;
00149   typedef phash_set<ShaderContext *, pointer_hash> Shaders;
00150   typedef phash_set< PT(Shader) > EnqueuedShaders;
00151   typedef phash_set<BufferContext *, pointer_hash> Buffers;
00152   typedef phash_set< PT(GeomVertexArrayData) > EnqueuedVertexBuffers;
00153   typedef phash_set< PT(GeomPrimitive) > EnqueuedIndexBuffers;
00154 
00155   class BufferCacheKey {
00156   public:
00157     INLINE bool operator < (const BufferCacheKey &other) const;
00158     INLINE bool operator == (const BufferCacheKey &other) const;
00159     INLINE bool operator != (const BufferCacheKey &other) const;
00160     size_t _data_size_bytes;
00161     GeomEnums::UsageHint _usage_hint;
00162   };
00163   typedef pvector<BufferContext *> BufferList;
00164   typedef pmap<BufferCacheKey, BufferList> BufferCache;
00165   typedef plist<BufferCacheKey> BufferCacheLRU;
00166 
00167   void cache_unprepared_buffer(BufferContext *buffer, size_t data_size_bytes,
00168                                GeomEnums::UsageHint usage_hint,
00169                                BufferCache &buffer_cache,
00170                                BufferCacheLRU &buffer_cache_lru,
00171                                size_t &buffer_cache_size,
00172                                int released_buffer_cache_size,
00173                                Buffers &released_buffers);
00174   BufferContext *get_cached_buffer(size_t data_size_bytes, 
00175                                    GeomEnums::UsageHint usage_hint,
00176                                    BufferCache &buffer_cache,
00177                                    BufferCacheLRU &buffer_cache_lru,
00178                                    size_t &buffer_cache_size);
00179 
00180   ReMutex _lock;
00181   string _name;
00182   Textures _prepared_textures, _released_textures;  
00183   EnqueuedTextures _enqueued_textures;
00184   Geoms _prepared_geoms, _released_geoms;  
00185   EnqueuedGeoms _enqueued_geoms;
00186   Shaders _prepared_shaders, _released_shaders;
00187   EnqueuedShaders _enqueued_shaders;
00188   Buffers _prepared_vertex_buffers, _released_vertex_buffers;  
00189   EnqueuedVertexBuffers _enqueued_vertex_buffers;
00190   Buffers _prepared_index_buffers, _released_index_buffers;  
00191   EnqueuedIndexBuffers _enqueued_index_buffers;
00192 
00193   BufferCache _vertex_buffer_cache;
00194   BufferCacheLRU _vertex_buffer_cache_lru;
00195   size_t _vertex_buffer_cache_size;
00196 
00197   BufferCache _index_buffer_cache;
00198   BufferCacheLRU _index_buffer_cache_lru;
00199   size_t _index_buffer_cache_size;
00200 
00201 public:
00202   BufferResidencyTracker _texture_residency;
00203   BufferResidencyTracker _vbuffer_residency;
00204   BufferResidencyTracker _ibuffer_residency;
00205 
00206   AdaptiveLru _graphics_memory_lru;
00207 
00208 public:
00209   // This is only public as a temporary hack.  Don't mess with it
00210   // unless you know what you're doing.
00211   bool _support_released_buffer_cache;
00212 
00213 private:
00214   static int _name_index;
00215 
00216   friend class GraphicsStateGuardian;
00217 };
00218 
00219 #include "preparedGraphicsObjects.I"
00220 
00221 #endif
 All Classes Functions Variables Enumerations