preparedGraphicsObjects.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #ifndef PREPAREDGRAPHICSOBJECTS_H
  14. #define PREPAREDGRAPHICSOBJECTS_H
  15. #include "pandabase.h"
  16. #include "referenceCount.h"
  17. #include "texture.h"
  18. #include "samplerState.h"
  19. #include "geom.h"
  20. #include "geomVertexArrayData.h"
  21. #include "geomPrimitive.h"
  22. #include "shader.h"
  23. #include "shaderBuffer.h"
  24. #include "pointerTo.h"
  25. #include "pStatCollector.h"
  26. #include "pset.h"
  27. #include "reMutex.h"
  28. #include "bufferResidencyTracker.h"
  29. #include "adaptiveLru.h"
  30. #include "asyncFuture.h"
  31. class TextureContext;
  32. class SamplerContext;
  33. class GeomContext;
  34. class ShaderContext;
  35. class VertexBufferContext;
  36. class IndexBufferContext;
  37. class BufferContext;
  38. class GraphicsStateGuardianBase;
  39. class SavedContext;
  40. /**
  41. * A table of objects that are saved within the graphics context for reference
  42. * by handle later. Generally, this represents things like OpenGL texture
  43. * objects or display lists (or their equivalent on other platforms).
  44. *
  45. * This object simply records the pointers to the context objects created by
  46. * the individual GSG's; these context objects will contain enough information
  47. * to reference or release the actual object stored within the graphics
  48. * context.
  49. *
  50. * These tables may potentially be shared between related graphics contexts,
  51. * hence their storage here in a separate object rather than as a part of the
  52. * GraphicsStateGuardian.
  53. */
  54. class EXPCL_PANDA_GOBJ PreparedGraphicsObjects : public ReferenceCount {
  55. public:
  56. PreparedGraphicsObjects();
  57. ~PreparedGraphicsObjects();
  58. PUBLISHED:
  59. INLINE const std::string &get_name() const;
  60. void set_graphics_memory_limit(size_t limit);
  61. INLINE size_t get_graphics_memory_limit() const;
  62. void show_graphics_memory_lru(std::ostream &out) const;
  63. void show_residency_trackers(std::ostream &out) const;
  64. INLINE void release_all();
  65. INLINE int get_num_queued() const;
  66. INLINE int get_num_prepared() const;
  67. void enqueue_texture(Texture *tex);
  68. bool is_texture_queued(const Texture *tex) const;
  69. bool dequeue_texture(Texture *tex);
  70. bool is_texture_prepared(const Texture *tex) const;
  71. void release_texture(TextureContext *tc);
  72. void release_texture(Texture *tex);
  73. int release_all_textures();
  74. int get_num_queued_textures() const;
  75. int get_num_prepared_textures() const;
  76. TextureContext *prepare_texture_now(Texture *tex, int view,
  77. GraphicsStateGuardianBase *gsg);
  78. void enqueue_sampler(const SamplerState &sampler);
  79. bool is_sampler_queued(const SamplerState &sampler) const;
  80. bool dequeue_sampler(const SamplerState &sampler);
  81. bool is_sampler_prepared(const SamplerState &sampler) const;
  82. void release_sampler(SamplerContext *sc);
  83. void release_sampler(const SamplerState &sampler);
  84. int release_all_samplers();
  85. int get_num_queued_samplers() const;
  86. int get_num_prepared_samplers() const;
  87. SamplerContext *prepare_sampler_now(const SamplerState &sampler,
  88. GraphicsStateGuardianBase *gsg);
  89. void enqueue_geom(Geom *geom);
  90. bool is_geom_queued(const Geom *geom) const;
  91. bool dequeue_geom(Geom *geom);
  92. bool is_geom_prepared(const Geom *geom) const;
  93. void release_geom(GeomContext *gc);
  94. int release_all_geoms();
  95. int get_num_queued_geoms() const;
  96. int get_num_prepared_geoms() const;
  97. GeomContext *prepare_geom_now(Geom *geom, GraphicsStateGuardianBase *gsg);
  98. void enqueue_shader(Shader *shader);
  99. bool is_shader_queued(const Shader *shader) const;
  100. bool dequeue_shader(Shader *shader);
  101. bool is_shader_prepared(const Shader *shader) const;
  102. void release_shader(ShaderContext *sc);
  103. int release_all_shaders();
  104. int get_num_queued_shaders() const;
  105. int get_num_prepared_shaders() const;
  106. ShaderContext *prepare_shader_now(Shader *shader, GraphicsStateGuardianBase *gsg);
  107. void enqueue_vertex_buffer(GeomVertexArrayData *data);
  108. bool is_vertex_buffer_queued(const GeomVertexArrayData *data) const;
  109. bool dequeue_vertex_buffer(GeomVertexArrayData *data);
  110. bool is_vertex_buffer_prepared(const GeomVertexArrayData *data) const;
  111. void release_vertex_buffer(VertexBufferContext *vbc);
  112. int release_all_vertex_buffers();
  113. int get_num_queued_vertex_buffers() const;
  114. int get_num_prepared_vertex_buffers() const;
  115. VertexBufferContext *
  116. prepare_vertex_buffer_now(GeomVertexArrayData *data,
  117. GraphicsStateGuardianBase *gsg);
  118. void enqueue_index_buffer(GeomPrimitive *data);
  119. bool is_index_buffer_queued(const GeomPrimitive *data) const;
  120. bool dequeue_index_buffer(GeomPrimitive *data);
  121. bool is_index_buffer_prepared(const GeomPrimitive *data) const;
  122. void release_index_buffer(IndexBufferContext *ibc);
  123. int release_all_index_buffers();
  124. int get_num_queued_index_buffers() const;
  125. int get_num_prepared_index_buffers() const;
  126. IndexBufferContext *
  127. prepare_index_buffer_now(GeomPrimitive *data,
  128. GraphicsStateGuardianBase *gsg);
  129. void enqueue_shader_buffer(ShaderBuffer *data);
  130. bool is_shader_buffer_queued(const ShaderBuffer *data) const;
  131. bool dequeue_shader_buffer(ShaderBuffer *data);
  132. bool is_shader_buffer_prepared(const ShaderBuffer *data) const;
  133. void release_shader_buffer(BufferContext *bc);
  134. int release_all_shader_buffers();
  135. int get_num_queued_shader_buffers() const;
  136. int get_num_prepared_shader_buffers() const;
  137. BufferContext *
  138. prepare_shader_buffer_now(ShaderBuffer *data,
  139. GraphicsStateGuardianBase *gsg);
  140. public:
  141. /**
  142. * This is a handle to an enqueued object, from which the result can be
  143. * obtained upon completion.
  144. */
  145. class EXPCL_PANDA_GOBJ EnqueuedObject final : public AsyncFuture {
  146. public:
  147. EnqueuedObject(PreparedGraphicsObjects *pgo, TypedWritableReferenceCount *object);
  148. TypedWritableReferenceCount *get_object() { return _object.p(); }
  149. SavedContext *get_result() { return (SavedContext *)AsyncFuture::get_result(); }
  150. void set_result(SavedContext *result);
  151. void notify_removed();
  152. virtual bool cancel() final;
  153. PUBLISHED:
  154. MAKE_PROPERTY(object, get_object);
  155. private:
  156. PreparedGraphicsObjects *_pgo;
  157. PT(TypedWritableReferenceCount) const _object;
  158. public:
  159. static TypeHandle get_class_type() {
  160. return _type_handle;
  161. }
  162. static void init_type() {
  163. AsyncFuture::init_type();
  164. register_type(_type_handle, "EnqueuedObject",
  165. AsyncFuture::get_class_type());
  166. }
  167. virtual TypeHandle get_type() const {
  168. return get_class_type();
  169. }
  170. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  171. private:
  172. static TypeHandle _type_handle;
  173. };
  174. // These are variations of enqueue_xxx that also return a future. They are
  175. // used to implement texture->prepare(), etc. They are only marked public
  176. // so we don't have to define a whole bunch of friend classes.
  177. PT(EnqueuedObject) enqueue_texture_future(Texture *tex);
  178. //PT(EnqueuedObject) enqueue_geom_future(Geom *geom);
  179. PT(EnqueuedObject) enqueue_shader_future(Shader *shader);
  180. //PT(EnqueuedObject) enqueue_vertex_buffer_future(GeomVertexArrayData *data);
  181. //PT(EnqueuedObject) enqueue_index_buffer_future(GeomPrimitive *data);
  182. //PT(EnqueuedObject) enqueue_shader_buffer_future(ShaderBuffer *data);
  183. void begin_frame(GraphicsStateGuardianBase *gsg,
  184. Thread *current_thread);
  185. void end_frame(Thread *current_thread);
  186. private:
  187. static std::string init_name();
  188. private:
  189. typedef phash_set<TextureContext *, pointer_hash> Textures;
  190. typedef phash_map< PT(Texture), PT(EnqueuedObject) > EnqueuedTextures;
  191. typedef phash_set<GeomContext *, pointer_hash> Geoms;
  192. typedef phash_set< PT(Geom) > EnqueuedGeoms;
  193. typedef phash_set<ShaderContext *, pointer_hash> Shaders;
  194. typedef phash_map< PT(Shader), PT(EnqueuedObject) > EnqueuedShaders;
  195. typedef phash_set<BufferContext *, pointer_hash> Buffers;
  196. typedef phash_set< PT(GeomVertexArrayData) > EnqueuedVertexBuffers;
  197. typedef phash_set< PT(GeomPrimitive) > EnqueuedIndexBuffers;
  198. typedef phash_set< PT(ShaderBuffer) > EnqueuedShaderBuffers;
  199. // Sampler states are stored a little bit differently, as they are mapped by
  200. // value and can't store the list of prepared samplers.
  201. typedef pmap<SamplerState, SamplerContext *> PreparedSamplers;
  202. typedef pset<SamplerContext *, pointer_hash> ReleasedSamplers;
  203. typedef pset<SamplerState> EnqueuedSamplers;
  204. class BufferCacheKey {
  205. public:
  206. INLINE bool operator < (const BufferCacheKey &other) const;
  207. INLINE bool operator == (const BufferCacheKey &other) const;
  208. INLINE bool operator != (const BufferCacheKey &other) const;
  209. size_t _data_size_bytes;
  210. GeomEnums::UsageHint _usage_hint;
  211. };
  212. typedef pvector<BufferContext *> BufferList;
  213. typedef pmap<BufferCacheKey, BufferList> BufferCache;
  214. typedef plist<BufferCacheKey> BufferCacheLRU;
  215. void cache_unprepared_buffer(BufferContext *buffer, size_t data_size_bytes,
  216. GeomEnums::UsageHint usage_hint,
  217. BufferCache &buffer_cache,
  218. BufferCacheLRU &buffer_cache_lru,
  219. size_t &buffer_cache_size,
  220. int released_buffer_cache_size,
  221. pvector<BufferContext *> &released_buffers);
  222. BufferContext *get_cached_buffer(size_t data_size_bytes,
  223. GeomEnums::UsageHint usage_hint,
  224. BufferCache &buffer_cache,
  225. BufferCacheLRU &buffer_cache_lru,
  226. size_t &buffer_cache_size);
  227. ReMutex _lock;
  228. std::string _name;
  229. Textures _prepared_textures;
  230. pvector<TextureContext *> _released_textures;
  231. EnqueuedTextures _enqueued_textures;
  232. PreparedSamplers _prepared_samplers;
  233. ReleasedSamplers _released_samplers;
  234. EnqueuedSamplers _enqueued_samplers;
  235. Geoms _prepared_geoms, _released_geoms;
  236. EnqueuedGeoms _enqueued_geoms;
  237. Shaders _prepared_shaders, _released_shaders;
  238. EnqueuedShaders _enqueued_shaders;
  239. Buffers _prepared_vertex_buffers;
  240. pvector<BufferContext *> _released_vertex_buffers;
  241. EnqueuedVertexBuffers _enqueued_vertex_buffers;
  242. Buffers _prepared_index_buffers;
  243. pvector<BufferContext *> _released_index_buffers;
  244. EnqueuedIndexBuffers _enqueued_index_buffers;
  245. Buffers _prepared_shader_buffers;
  246. pvector<BufferContext *> _released_shader_buffers;
  247. EnqueuedShaderBuffers _enqueued_shader_buffers;
  248. BufferCache _vertex_buffer_cache;
  249. BufferCacheLRU _vertex_buffer_cache_lru;
  250. size_t _vertex_buffer_cache_size;
  251. BufferCache _index_buffer_cache;
  252. BufferCacheLRU _index_buffer_cache_lru;
  253. size_t _index_buffer_cache_size;
  254. public:
  255. BufferResidencyTracker _texture_residency;
  256. BufferResidencyTracker _vbuffer_residency;
  257. BufferResidencyTracker _ibuffer_residency;
  258. BufferResidencyTracker _sbuffer_residency;
  259. AdaptiveLru _graphics_memory_lru;
  260. SimpleLru _sampler_object_lru;
  261. public:
  262. // This is only public as a temporary hack. Don't mess with it unless you
  263. // know what you're doing.
  264. bool _support_released_buffer_cache;
  265. private:
  266. static int _name_index;
  267. friend class GraphicsStateGuardian;
  268. };
  269. #include "preparedGraphicsObjects.I"
  270. #endif