Panda3D

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, GraphicsStateGuardianBase *gsg);
00086 
00087   void enqueue_geom(Geom *geom);
00088   bool is_geom_queued(const Geom *geom) const;
00089   bool dequeue_geom(Geom *geom);
00090   bool is_geom_prepared(const Geom *geom) const;
00091   void release_geom(GeomContext *gc);
00092   int release_all_geoms();
00093   int get_num_queued_geoms() const;
00094   int get_num_prepared_geoms() const;
00095 
00096   GeomContext *prepare_geom_now(Geom *geom, GraphicsStateGuardianBase *gsg);
00097 
00098   void enqueue_shader(Shader *shader);
00099   bool is_shader_queued(const Shader *shader) const;
00100   bool dequeue_shader(Shader *shader);
00101   bool is_shader_prepared(const Shader *shader) const;
00102   void release_shader(ShaderContext *sc);
00103   int release_all_shaders();
00104   int get_num_queued_shaders() const;
00105   int get_num_prepared_shaders() const;
00106 
00107   ShaderContext *prepare_shader_now(Shader *shader, GraphicsStateGuardianBase *gsg);
00108 
00109   void enqueue_vertex_buffer(GeomVertexArrayData *data);
00110   bool is_vertex_buffer_queued(const GeomVertexArrayData *data) const;
00111   bool dequeue_vertex_buffer(GeomVertexArrayData *data);
00112   bool is_vertex_buffer_prepared(const GeomVertexArrayData *data) const;
00113   void release_vertex_buffer(VertexBufferContext *vbc);
00114   int release_all_vertex_buffers();
00115   int get_num_queued_vertex_buffers() const;
00116   int get_num_prepared_vertex_buffers() const;
00117 
00118   VertexBufferContext *
00119   prepare_vertex_buffer_now(GeomVertexArrayData *data,
00120                             GraphicsStateGuardianBase *gsg);
00121 
00122   void enqueue_index_buffer(GeomPrimitive *data);
00123   bool is_index_buffer_queued(const GeomPrimitive *data) const;
00124   bool dequeue_index_buffer(GeomPrimitive *data);
00125   bool is_index_buffer_prepared(const GeomPrimitive *data) const;
00126   void release_index_buffer(IndexBufferContext *ibc);
00127   int release_all_index_buffers();
00128   int get_num_queued_index_buffers() const;
00129   int get_num_prepared_index_buffers() const;
00130 
00131   IndexBufferContext *
00132   prepare_index_buffer_now(GeomPrimitive *data,
00133                            GraphicsStateGuardianBase *gsg);
00134 
00135 public:
00136   void begin_frame(GraphicsStateGuardianBase *gsg,
00137                    Thread *current_thread);
00138   void end_frame(Thread *current_thread);
00139 
00140 private:
00141   static string init_name();
00142 
00143 private:
00144   typedef phash_set<TextureContext *, pointer_hash> Textures;
00145   typedef phash_set< PT(Texture) > EnqueuedTextures;
00146   typedef phash_set<GeomContext *, pointer_hash> Geoms;
00147   typedef phash_set< PT(Geom) > EnqueuedGeoms;
00148   typedef phash_set<ShaderContext *, pointer_hash> Shaders;
00149   typedef phash_set< PT(Shader) > EnqueuedShaders;
00150   typedef phash_set<BufferContext *, pointer_hash> Buffers;
00151   typedef phash_set< PT(GeomVertexArrayData) > EnqueuedVertexBuffers;
00152   typedef phash_set< PT(GeomPrimitive) > EnqueuedIndexBuffers;
00153 
00154   class BufferCacheKey {
00155   public:
00156     INLINE bool operator < (const BufferCacheKey &other) const;
00157     INLINE bool operator == (const BufferCacheKey &other) const;
00158     INLINE bool operator != (const BufferCacheKey &other) const;
00159     size_t _data_size_bytes;
00160     GeomEnums::UsageHint _usage_hint;
00161   };
00162   typedef pvector<BufferContext *> BufferList;
00163   typedef pmap<BufferCacheKey, BufferList> BufferCache;
00164   typedef plist<BufferCacheKey> BufferCacheLRU;
00165 
00166   void cache_unprepared_buffer(BufferContext *buffer, size_t data_size_bytes,
00167                                GeomEnums::UsageHint usage_hint,
00168                                BufferCache &buffer_cache,
00169                                BufferCacheLRU &buffer_cache_lru,
00170                                size_t &buffer_cache_size,
00171                                int released_buffer_cache_size,
00172                                Buffers &released_buffers);
00173   BufferContext *get_cached_buffer(size_t data_size_bytes, 
00174                                    GeomEnums::UsageHint usage_hint,
00175                                    BufferCache &buffer_cache,
00176                                    BufferCacheLRU &buffer_cache_lru,
00177                                    size_t &buffer_cache_size);
00178 
00179   ReMutex _lock;
00180   string _name;
00181   Textures _prepared_textures, _released_textures;  
00182   EnqueuedTextures _enqueued_textures;
00183   Geoms _prepared_geoms, _released_geoms;  
00184   EnqueuedGeoms _enqueued_geoms;
00185   Shaders _prepared_shaders, _released_shaders;
00186   EnqueuedShaders _enqueued_shaders;
00187   Buffers _prepared_vertex_buffers, _released_vertex_buffers;  
00188   EnqueuedVertexBuffers _enqueued_vertex_buffers;
00189   Buffers _prepared_index_buffers, _released_index_buffers;  
00190   EnqueuedIndexBuffers _enqueued_index_buffers;
00191 
00192   BufferCache _vertex_buffer_cache;
00193   BufferCacheLRU _vertex_buffer_cache_lru;
00194   size_t _vertex_buffer_cache_size;
00195 
00196   BufferCache _index_buffer_cache;
00197   BufferCacheLRU _index_buffer_cache_lru;
00198   size_t _index_buffer_cache_size;
00199 
00200 public:
00201   BufferResidencyTracker _texture_residency;
00202   BufferResidencyTracker _vbuffer_residency;
00203   BufferResidencyTracker _ibuffer_residency;
00204 
00205   AdaptiveLru _graphics_memory_lru;
00206 
00207 public:
00208   // This is only public as a temporary hack.  Don't mess with it
00209   // unless you know what you're doing.
00210   bool _support_released_buffer_cache;
00211 
00212 private:
00213   static int _name_index;
00214 
00215   friend class GraphicsStateGuardian;
00216 };
00217 
00218 #include "preparedGraphicsObjects.I"
00219 
00220 #endif
 All Classes Functions Variables Enumerations