Panda3D
preparedGraphicsObjects.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file preparedGraphicsObjects.h
10  * @author drose
11  * @date 2004-02-19
12  */
13 
14 #ifndef PREPAREDGRAPHICSOBJECTS_H
15 #define PREPAREDGRAPHICSOBJECTS_H
16 
17 #include "pandabase.h"
18 #include "referenceCount.h"
19 #include "texture.h"
20 #include "samplerState.h"
21 #include "geom.h"
22 #include "geomVertexArrayData.h"
23 #include "geomPrimitive.h"
24 #include "shader.h"
25 #include "shaderBuffer.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 #include "asyncFuture.h"
33 
34 class TextureContext;
35 class SamplerContext;
36 class GeomContext;
37 class ShaderContext;
39 class IndexBufferContext;
40 class BufferContext;
42 class SavedContext;
43 
44 /**
45  * A table of objects that are saved within the graphics context for reference
46  * by handle later. Generally, this represents things like OpenGL texture
47  * objects or display lists (or their equivalent on other platforms).
48  *
49  * This object simply records the pointers to the context objects created by
50  * the individual GSG's; these context objects will contain enough information
51  * to reference or release the actual object stored within the graphics
52  * context.
53  *
54  * These tables may potentially be shared between related graphics contexts,
55  * hence their storage here in a separate object rather than as a part of the
56  * GraphicsStateGuardian.
57  */
58 class EXPCL_PANDA_GOBJ PreparedGraphicsObjects : public ReferenceCount {
59 public:
62 
63 PUBLISHED:
64  INLINE const std::string &get_name() const;
65 
66  void set_graphics_memory_limit(size_t limit);
67  INLINE size_t get_graphics_memory_limit() const;
68  void show_graphics_memory_lru(std::ostream &out) const;
69  void show_residency_trackers(std::ostream &out) const;
70 
71  INLINE void release_all();
72  INLINE int get_num_queued() const;
73  INLINE int get_num_prepared() const;
74 
75  void enqueue_texture(Texture *tex);
76  bool is_texture_queued(const Texture *tex) const;
77  bool dequeue_texture(Texture *tex);
78  bool is_texture_prepared(const Texture *tex) const;
79  void release_texture(TextureContext *tc);
80  void release_texture(Texture *tex);
81  int release_all_textures();
82  int get_num_queued_textures() const;
83  int get_num_prepared_textures() const;
84 
85  TextureContext *prepare_texture_now(Texture *tex, int view,
87 
88  void enqueue_sampler(const SamplerState &sampler);
89  bool is_sampler_queued(const SamplerState &sampler) const;
90  bool dequeue_sampler(const SamplerState &sampler);
91  bool is_sampler_prepared(const SamplerState &sampler) const;
92  void release_sampler(SamplerContext *sc);
93  void release_sampler(const SamplerState &sampler);
94  int release_all_samplers();
95  int get_num_queued_samplers() const;
96  int get_num_prepared_samplers() const;
97 
98  SamplerContext *prepare_sampler_now(const SamplerState &sampler,
100 
101  void enqueue_geom(Geom *geom);
102  bool is_geom_queued(const Geom *geom) const;
103  bool dequeue_geom(Geom *geom);
104  bool is_geom_prepared(const Geom *geom) const;
105  void release_geom(GeomContext *gc);
106  int release_all_geoms();
107  int get_num_queued_geoms() const;
108  int get_num_prepared_geoms() const;
109 
110  GeomContext *prepare_geom_now(Geom *geom, GraphicsStateGuardianBase *gsg);
111 
112  void enqueue_shader(Shader *shader);
113  bool is_shader_queued(const Shader *shader) const;
114  bool dequeue_shader(Shader *shader);
115  bool is_shader_prepared(const Shader *shader) const;
116  void release_shader(ShaderContext *sc);
117  int release_all_shaders();
118  int get_num_queued_shaders() const;
119  int get_num_prepared_shaders() const;
120 
121  ShaderContext *prepare_shader_now(Shader *shader, GraphicsStateGuardianBase *gsg);
122 
123  void enqueue_vertex_buffer(GeomVertexArrayData *data);
124  bool is_vertex_buffer_queued(const GeomVertexArrayData *data) const;
125  bool dequeue_vertex_buffer(GeomVertexArrayData *data);
126  bool is_vertex_buffer_prepared(const GeomVertexArrayData *data) const;
127  void release_vertex_buffer(VertexBufferContext *vbc);
128  int release_all_vertex_buffers();
129  int get_num_queued_vertex_buffers() const;
130  int get_num_prepared_vertex_buffers() const;
131 
133  prepare_vertex_buffer_now(GeomVertexArrayData *data,
135 
136  void enqueue_index_buffer(GeomPrimitive *data);
137  bool is_index_buffer_queued(const GeomPrimitive *data) const;
138  bool dequeue_index_buffer(GeomPrimitive *data);
139  bool is_index_buffer_prepared(const GeomPrimitive *data) const;
140  void release_index_buffer(IndexBufferContext *ibc);
141  int release_all_index_buffers();
142  int get_num_queued_index_buffers() const;
143  int get_num_prepared_index_buffers() const;
144 
146  prepare_index_buffer_now(GeomPrimitive *data,
148 
149  void enqueue_shader_buffer(ShaderBuffer *data);
150  bool is_shader_buffer_queued(const ShaderBuffer *data) const;
151  bool dequeue_shader_buffer(ShaderBuffer *data);
152  bool is_shader_buffer_prepared(const ShaderBuffer *data) const;
153  void release_shader_buffer(BufferContext *bc);
154  int release_all_shader_buffers();
155  int get_num_queued_shader_buffers() const;
156  int get_num_prepared_shader_buffers() const;
157 
158  BufferContext *
159  prepare_shader_buffer_now(ShaderBuffer *data,
161 
162 public:
163  /**
164  * This is a handle to an enqueued object, from which the result can be
165  * obtained upon completion.
166  */
167  class EXPCL_PANDA_GOBJ EnqueuedObject final : public AsyncFuture {
168  public:
170 
171  TypedWritableReferenceCount *get_object() { return _object.p(); }
173  void set_result(SavedContext *result);
174 
175  void notify_removed();
176  virtual bool cancel() final;
177 
178  PUBLISHED:
179  MAKE_PROPERTY(object, get_object);
180 
181  private:
183  PT(TypedWritableReferenceCount) const _object;
184 
185  public:
186  static TypeHandle get_class_type() {
187  return _type_handle;
188  }
189  static void init_type() {
190  AsyncFuture::init_type();
191  register_type(_type_handle, "EnqueuedObject",
192  AsyncFuture::get_class_type());
193  }
194  virtual TypeHandle get_type() const {
195  return get_class_type();
196  }
197  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
198 
199  private:
200  static TypeHandle _type_handle;
201  };
202 
203  // These are variations of enqueue_xxx that also return a future. They are
204  // used to implement texture->prepare(), etc. They are only marked public
205  // so we don't have to define a whole bunch of friend classes.
206  PT(EnqueuedObject) enqueue_texture_future(Texture *tex);
207  //PT(EnqueuedObject) enqueue_geom_future(Geom *geom);
208  PT(EnqueuedObject) enqueue_shader_future(Shader *shader);
209  //PT(EnqueuedObject) enqueue_vertex_buffer_future(GeomVertexArrayData *data);
210  //PT(EnqueuedObject) enqueue_index_buffer_future(GeomPrimitive *data);
211  //PT(EnqueuedObject) enqueue_shader_buffer_future(ShaderBuffer *data);
212 
213  void begin_frame(GraphicsStateGuardianBase *gsg,
214  Thread *current_thread);
215  void end_frame(Thread *current_thread);
216 
217 private:
218  static std::string init_name();
219 
220 private:
221  typedef phash_set<TextureContext *, pointer_hash> Textures;
222  typedef phash_map< PT(Texture), PT(EnqueuedObject) > EnqueuedTextures;
223  typedef phash_set<GeomContext *, pointer_hash> Geoms;
224  typedef phash_set< PT(Geom) > EnqueuedGeoms;
225  typedef phash_set<ShaderContext *, pointer_hash> Shaders;
226  typedef phash_map< PT(Shader), PT(EnqueuedObject) > EnqueuedShaders;
227  typedef phash_set<BufferContext *, pointer_hash> Buffers;
228  typedef phash_set< PT(GeomVertexArrayData) > EnqueuedVertexBuffers;
229  typedef phash_set< PT(GeomPrimitive) > EnqueuedIndexBuffers;
230  typedef phash_set< PT(ShaderBuffer) > EnqueuedShaderBuffers;
231 
232  // Sampler states are stored a little bit differently, as they are mapped by
233  // value and can't store the list of prepared samplers.
237 
238  class BufferCacheKey {
239  public:
240  INLINE bool operator < (const BufferCacheKey &other) const;
241  INLINE bool operator == (const BufferCacheKey &other) const;
242  INLINE bool operator != (const BufferCacheKey &other) const;
243  size_t _data_size_bytes;
244  GeomEnums::UsageHint _usage_hint;
245  };
246  typedef pvector<BufferContext *> BufferList;
247  typedef pmap<BufferCacheKey, BufferList> BufferCache;
248  typedef plist<BufferCacheKey> BufferCacheLRU;
249 
250  void cache_unprepared_buffer(BufferContext *buffer, size_t data_size_bytes,
251  GeomEnums::UsageHint usage_hint,
252  BufferCache &buffer_cache,
253  BufferCacheLRU &buffer_cache_lru,
254  size_t &buffer_cache_size,
255  int released_buffer_cache_size,
256  Buffers &released_buffers);
257  BufferContext *get_cached_buffer(size_t data_size_bytes,
258  GeomEnums::UsageHint usage_hint,
259  BufferCache &buffer_cache,
260  BufferCacheLRU &buffer_cache_lru,
261  size_t &buffer_cache_size);
262 
263  ReMutex _lock;
264  std::string _name;
265  Textures _prepared_textures, _released_textures;
266  EnqueuedTextures _enqueued_textures;
267  PreparedSamplers _prepared_samplers;
268  ReleasedSamplers _released_samplers;
269  EnqueuedSamplers _enqueued_samplers;
270  Geoms _prepared_geoms, _released_geoms;
271  EnqueuedGeoms _enqueued_geoms;
272  Shaders _prepared_shaders, _released_shaders;
273  EnqueuedShaders _enqueued_shaders;
274  Buffers _prepared_vertex_buffers, _released_vertex_buffers;
275  EnqueuedVertexBuffers _enqueued_vertex_buffers;
276  Buffers _prepared_index_buffers, _released_index_buffers;
277  EnqueuedIndexBuffers _enqueued_index_buffers;
278  Buffers _prepared_shader_buffers, _released_shader_buffers;
279  EnqueuedShaderBuffers _enqueued_shader_buffers;
280 
281  BufferCache _vertex_buffer_cache;
282  BufferCacheLRU _vertex_buffer_cache_lru;
283  size_t _vertex_buffer_cache_size;
284 
285  BufferCache _index_buffer_cache;
286  BufferCacheLRU _index_buffer_cache_lru;
287  size_t _index_buffer_cache_size;
288 
289 public:
290  BufferResidencyTracker _texture_residency;
291  BufferResidencyTracker _vbuffer_residency;
292  BufferResidencyTracker _ibuffer_residency;
293  BufferResidencyTracker _sbuffer_residency;
294 
295  AdaptiveLru _graphics_memory_lru;
296  SimpleLru _sampler_object_lru;
297 
298 public:
299  // This is only public as a temporary hack. Don't mess with it unless you
300  // know what you're doing.
301  bool _support_released_buffer_cache;
302 
303 private:
304  static int _name_index;
305 
306  friend class GraphicsStateGuardian;
307 };
308 
309 #include "preparedGraphicsObjects.I"
310 
311 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An implementation of a very simple LRU algorithm.
Definition: simpleLru.h:28
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a special class object that holds all the information returned by a particular GSG to indicat...
Definition: geomContext.h:34
This class represents a thread-safe handle to a promised future result of an asynchronous operation,...
Definition: asyncFuture.h:61
This is a generic buffer object that lives in graphics memory.
Definition: shaderBuffer.h:33
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:38
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:71
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:56
Definition: shader.h:49
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:31
TypedObject * get_result() const
Returns this future's result.
Definition: asyncFuture.I:65
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a handle to an enqueued object, from which the result can be obtained upon completion.
void set_result(std::nullptr_t)
Sets this future's result.
Definition: asyncFuture.I:92
A container for geometry primitives.
Definition: geom.h:54
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
A basic LRU-type algorithm, except that it is adaptive and attempts to avoid evicting pages that have...
Definition: adaptiveLru.h:45
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:36
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:46
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual bool cancel()
Cancels the future.
Definition: asyncFuture.cxx:43
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the base class for all GSG-specific context objects, such as TextureContext and GeomContext.
Definition: savedContext.h:26
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A reentrant mutex.
Definition: reMutex.h:32
This is the data for one array of a GeomVertexData structure.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.