Panda3D
 All Classes Functions Variables Enumerations
preparedGraphicsObjects.h
1 // Filename: preparedGraphicsObjects.h
2 // Created by: drose (19Feb04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef PREPAREDGRAPHICSOBJECTS_H
16 #define PREPAREDGRAPHICSOBJECTS_H
17 
18 #include "pandabase.h"
19 #include "referenceCount.h"
20 #include "texture.h"
21 #include "samplerState.h"
22 #include "geom.h"
23 #include "geomVertexArrayData.h"
24 #include "geomPrimitive.h"
25 #include "shader.h"
26 #include "pointerTo.h"
27 #include "pStatCollector.h"
28 #include "pset.h"
29 #include "reMutex.h"
30 #include "bufferResidencyTracker.h"
31 #include "adaptiveLru.h"
32 
33 class TextureContext;
34 class SamplerContext;
35 class GeomContext;
36 class ShaderContext;
38 class IndexBufferContext;
40 
41 ////////////////////////////////////////////////////////////////////
42 // Class : PreparedGraphicsObjects
43 // Description : A table of objects that are saved within the graphics
44 // context for reference by handle later. Generally,
45 // this represents things like OpenGL texture objects or
46 // display lists (or their equivalent on other
47 // platforms).
48 //
49 // This object simply records the pointers to the
50 // context objects created by the individual GSG's;
51 // these context objects will contain enough information
52 // to reference or release the actual object stored
53 // within the graphics context.
54 //
55 // These tables may potentially be shared between
56 // related graphics contexts, hence their storage here
57 // in a separate object rather than as a part of the
58 // GraphicsStateGuardian.
59 ////////////////////////////////////////////////////////////////////
60 class EXPCL_PANDA_GOBJ PreparedGraphicsObjects : public ReferenceCount {
61 public:
64 
65 PUBLISHED:
66  INLINE const string &get_name() const;
67 
68  void set_graphics_memory_limit(size_t limit);
69  INLINE size_t get_graphics_memory_limit() const;
70  void show_graphics_memory_lru(ostream &out) const;
71  void show_residency_trackers(ostream &out) const;
72 
73  INLINE void release_all();
74  INLINE int get_num_queued() const;
75  INLINE int get_num_prepared() const;
76 
77  void enqueue_texture(Texture *tex);
78  bool is_texture_queued(const Texture *tex) const;
79  bool dequeue_texture(Texture *tex);
80  bool is_texture_prepared(const Texture *tex) const;
81  void release_texture(TextureContext *tc);
82  void release_texture(Texture *tex);
83  int release_all_textures();
84  int get_num_queued_textures() const;
85  int get_num_prepared_textures() const;
86 
87  TextureContext *prepare_texture_now(Texture *tex, int view,
89 
90  void enqueue_sampler(const SamplerState &sampler);
91  bool is_sampler_queued(const SamplerState &sampler) const;
92  bool dequeue_sampler(const SamplerState &sampler);
93  bool is_sampler_prepared(const SamplerState &sampler) const;
94  void release_sampler(SamplerContext *sc);
95  void release_sampler(const SamplerState &sampler);
96  int release_all_samplers();
97  int get_num_queued_samplers() const;
98  int get_num_prepared_samplers() const;
99 
100  SamplerContext *prepare_sampler_now(const SamplerState &sampler,
102 
103  void enqueue_geom(Geom *geom);
104  bool is_geom_queued(const Geom *geom) const;
105  bool dequeue_geom(Geom *geom);
106  bool is_geom_prepared(const Geom *geom) const;
107  void release_geom(GeomContext *gc);
108  int release_all_geoms();
109  int get_num_queued_geoms() const;
110  int get_num_prepared_geoms() const;
111 
112  GeomContext *prepare_geom_now(Geom *geom, GraphicsStateGuardianBase *gsg);
113 
114  void enqueue_shader(Shader *shader);
115  bool is_shader_queued(const Shader *shader) const;
116  bool dequeue_shader(Shader *shader);
117  bool is_shader_prepared(const Shader *shader) const;
118  void release_shader(ShaderContext *sc);
119  int release_all_shaders();
120  int get_num_queued_shaders() const;
121  int get_num_prepared_shaders() const;
122 
123  ShaderContext *prepare_shader_now(Shader *shader, GraphicsStateGuardianBase *gsg);
124 
125  void enqueue_vertex_buffer(GeomVertexArrayData *data);
126  bool is_vertex_buffer_queued(const GeomVertexArrayData *data) const;
127  bool dequeue_vertex_buffer(GeomVertexArrayData *data);
128  bool is_vertex_buffer_prepared(const GeomVertexArrayData *data) const;
129  void release_vertex_buffer(VertexBufferContext *vbc);
130  int release_all_vertex_buffers();
131  int get_num_queued_vertex_buffers() const;
132  int get_num_prepared_vertex_buffers() const;
133 
135  prepare_vertex_buffer_now(GeomVertexArrayData *data,
137 
138  void enqueue_index_buffer(GeomPrimitive *data);
139  bool is_index_buffer_queued(const GeomPrimitive *data) const;
140  bool dequeue_index_buffer(GeomPrimitive *data);
141  bool is_index_buffer_prepared(const GeomPrimitive *data) const;
142  void release_index_buffer(IndexBufferContext *ibc);
143  int release_all_index_buffers();
144  int get_num_queued_index_buffers() const;
145  int get_num_prepared_index_buffers() const;
146 
148  prepare_index_buffer_now(GeomPrimitive *data,
150 
151 public:
152  void begin_frame(GraphicsStateGuardianBase *gsg,
153  Thread *current_thread);
154  void end_frame(Thread *current_thread);
155 
156 private:
157  static string init_name();
158 
159 private:
160  typedef phash_set<TextureContext *, pointer_hash> Textures;
161  typedef phash_set< PT(Texture) > EnqueuedTextures;
162  typedef phash_set<GeomContext *, pointer_hash> Geoms;
163  typedef phash_set< PT(Geom) > EnqueuedGeoms;
164  typedef phash_set<ShaderContext *, pointer_hash> Shaders;
165  typedef phash_set< PT(Shader) > EnqueuedShaders;
166  typedef phash_set<BufferContext *, pointer_hash> Buffers;
167  typedef phash_set< PT(GeomVertexArrayData) > EnqueuedVertexBuffers;
168  typedef phash_set< PT(GeomPrimitive) > EnqueuedIndexBuffers;
169 
170  // Sampler states are stored a little bit differently, as they are
171  // mapped by value and can't store the list of prepared samplers.
175 
176  class BufferCacheKey {
177  public:
178  INLINE bool operator < (const BufferCacheKey &other) const;
179  INLINE bool operator == (const BufferCacheKey &other) const;
180  INLINE bool operator != (const BufferCacheKey &other) const;
181  size_t _data_size_bytes;
182  GeomEnums::UsageHint _usage_hint;
183  };
187 
188  void cache_unprepared_buffer(BufferContext *buffer, size_t data_size_bytes,
189  GeomEnums::UsageHint usage_hint,
190  BufferCache &buffer_cache,
191  BufferCacheLRU &buffer_cache_lru,
192  size_t &buffer_cache_size,
193  int released_buffer_cache_size,
194  Buffers &released_buffers);
195  BufferContext *get_cached_buffer(size_t data_size_bytes,
196  GeomEnums::UsageHint usage_hint,
197  BufferCache &buffer_cache,
198  BufferCacheLRU &buffer_cache_lru,
199  size_t &buffer_cache_size);
200 
201  ReMutex _lock;
202  string _name;
203  Textures _prepared_textures, _released_textures;
204  EnqueuedTextures _enqueued_textures;
205  PreparedSamplers _prepared_samplers;
206  ReleasedSamplers _released_samplers;
207  EnqueuedSamplers _enqueued_samplers;
208  Geoms _prepared_geoms, _released_geoms;
209  EnqueuedGeoms _enqueued_geoms;
210  Shaders _prepared_shaders, _released_shaders;
211  EnqueuedShaders _enqueued_shaders;
212  Buffers _prepared_vertex_buffers, _released_vertex_buffers;
213  EnqueuedVertexBuffers _enqueued_vertex_buffers;
214  Buffers _prepared_index_buffers, _released_index_buffers;
215  EnqueuedIndexBuffers _enqueued_index_buffers;
216 
217  BufferCache _vertex_buffer_cache;
218  BufferCacheLRU _vertex_buffer_cache_lru;
219  size_t _vertex_buffer_cache_size;
220 
221  BufferCache _index_buffer_cache;
222  BufferCacheLRU _index_buffer_cache_lru;
223  size_t _index_buffer_cache_size;
224 
225 public:
226  BufferResidencyTracker _texture_residency;
227  BufferResidencyTracker _vbuffer_residency;
228  BufferResidencyTracker _ibuffer_residency;
229 
230  AdaptiveLru _graphics_memory_lru;
231  SimpleLru _sampler_object_lru;
232 
233 public:
234  // This is only public as a temporary hack. Don't mess with it
235  // unless you know what you're doing.
236  bool _support_released_buffer_cache;
237 
238 private:
239  static int _name_index;
240 
241  friend class GraphicsStateGuardian;
242 };
243 
244 #include "preparedGraphicsObjects.I"
245 
246 #endif
An implementation of a very simple LRU algorithm.
Definition: simpleLru.h:31
This is a special class object that holds all the information returned by a particular GSG to indicat...
Definition: geomContext.h:39
This is a special class object that holds all the information returned by a particular GSG to indicat...
This is a base class for those kinds of SavedContexts that occupy an easily-measured (and substantial...
Definition: bufferContext.h:41
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:63
Definition: shader.h:50
This is a special class object that holds all the information returned by a particular GSG to indicat...
This class is used to keep track of the current state of all the BufferContexts for a particular grap...
A table of objects that are saved within the graphics context for reference by handle later...
The ShaderContext is meant to contain the compiled version of a shader string.
Definition: shaderContext.h:35
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
A container for geometry primitives.
Definition: geom.h:58
A basic LRU-type algorithm, except that it is adaptive and attempts to avoid evicting pages that have...
Definition: adaptiveLru.h:49
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:39
This is a special class object that holds a handle to the sampler state object given by the graphics ...
A base class for all things that want to be reference-counted.
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
A thread; that is, a lightweight process.
Definition: thread.h:51
This is a special class object that holds all the information returned by a particular GSG to indicat...
Encapsulates all the communication with a particular instance of a given rendering backend...
A reentrant mutex.
Definition: reMutex.h:36
This is the data for one array of a GeomVertexData structure.