// Filename: preparedGraphicsObjects.h
// Created by:  drose (19Feb04)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license.  You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://etc.cmu.edu/panda3d/docs/license/ .
//
// To contact the maintainers of this program write to
// panda3d-general@lists.sourceforge.net .
//
////////////////////////////////////////////////////////////////////

#ifndef PREPAREDGRAPHICSOBJECTS_H
#define PREPAREDGRAPHICSOBJECTS_H

#include "pandabase.h"
#include "referenceCount.h"
#include "texture.h"
#include "geom.h"
#include "geomVertexArrayData.h"
#include "geomPrimitive.h"
#include "shaderExpansion.h"
#include "pointerTo.h"
#include "pStatCollector.h"
#include "pset.h"
#include "reMutex.h"
#include "bufferResidencyTracker.h"

class TextureContext;
class GeomContext;
class ShaderContext;
class VertexBufferContext;
class IndexBufferContext;
class GraphicsStateGuardianBase;

////////////////////////////////////////////////////////////////////
//       Class : PreparedGraphicsObjects
// Description : A table of objects that are saved within the graphics
//               context for reference by handle later.  Generally,
//               this represents things like OpenGL texture objects or
//               display lists (or their equivalent on other
//               platforms).
//
//               This object simply records the pointers to the
//               context objects created by the individual GSG's;
//               these context objects will contain enough information
//               to reference or release the actual object stored
//               within the graphics context.
//
//               These tables may potentially be shared between
//               related graphics contexts, hence their storage here
//               in a separate object rather than as a part of the
//               GraphicsStateGuardian.
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA PreparedGraphicsObjects : public ReferenceCount {
public:
  PreparedGraphicsObjects();
  ~PreparedGraphicsObjects();

PUBLISHED:
  INLINE const string &get_name() const;

  INLINE void release_all();
  INLINE int get_num_queued() const;
  INLINE int get_num_prepared() const;

  void enqueue_texture(Texture *tex);
  bool is_texture_queued(const Texture *tex) const;
  bool dequeue_texture(Texture *tex);
  bool is_texture_prepared(const Texture *tex) const;
  void release_texture(TextureContext *tc);
  int release_all_textures();
  int get_num_queued_textures() const;
  int get_num_prepared_textures() const;

  TextureContext *prepare_texture_now(Texture *tex, GraphicsStateGuardianBase *gsg);

  void enqueue_geom(Geom *geom);
  bool is_geom_queued(const Geom *geom) const;
  bool dequeue_geom(Geom *geom);
  bool is_geom_prepared(const Geom *geom) const;
  void release_geom(GeomContext *gc);
  int release_all_geoms();
  int get_num_queued_geoms() const;
  int get_num_prepared_geoms() const;

  GeomContext *prepare_geom_now(Geom *geom, GraphicsStateGuardianBase *gsg);

  void enqueue_shader(ShaderExpansion *shader);
  bool is_shader_queued(const ShaderExpansion *shader) const;
  bool dequeue_shader(ShaderExpansion *shader);
  bool is_shader_prepared(const ShaderExpansion *shader) const;
  void release_shader(ShaderContext *sc);
  int release_all_shaders();
  int get_num_queued_shaders() const;
  int get_num_prepared_shaders() const;

  ShaderContext *prepare_shader_now(ShaderExpansion *shader, GraphicsStateGuardianBase *gsg);

  void enqueue_vertex_buffer(GeomVertexArrayData *data);
  bool is_vertex_buffer_queued(const GeomVertexArrayData *data) const;
  bool dequeue_vertex_buffer(GeomVertexArrayData *data);
  bool is_vertex_buffer_prepared(const GeomVertexArrayData *data) const;
  void release_vertex_buffer(VertexBufferContext *vbc);
  int release_all_vertex_buffers();
  int get_num_queued_vertex_buffers() const;
  int get_num_prepared_vertex_buffers() const;

  VertexBufferContext *
  prepare_vertex_buffer_now(GeomVertexArrayData *data,
                            GraphicsStateGuardianBase *gsg);

  void enqueue_index_buffer(GeomPrimitive *data);
  bool is_index_buffer_queued(const GeomPrimitive *data) const;
  bool dequeue_index_buffer(GeomPrimitive *data);
  bool is_index_buffer_prepared(const GeomPrimitive *data) const;
  void release_index_buffer(IndexBufferContext *ibc);
  int release_all_index_buffers();
  int get_num_queued_index_buffers() const;
  int get_num_prepared_index_buffers() const;

  IndexBufferContext *
  prepare_index_buffer_now(GeomPrimitive *data,
                           GraphicsStateGuardianBase *gsg);

public:
  void begin_frame(GraphicsStateGuardianBase *gsg,
                   Thread *current_thread);
  void end_frame(Thread *current_thread);

private:
  static string init_name();

private:
  typedef phash_set<TextureContext *, pointer_hash> Textures;
  typedef phash_set< PT(Texture) > EnqueuedTextures;
  typedef phash_set<GeomContext *, pointer_hash> Geoms;
  typedef phash_set< PT(Geom) > EnqueuedGeoms;
  typedef phash_set<ShaderContext *, pointer_hash> Shaders;
  typedef phash_set< PT(ShaderExpansion) > EnqueuedShaders;
  typedef phash_set<VertexBufferContext *, pointer_hash> VertexBuffers;
  typedef phash_set< PT(GeomVertexArrayData) > EnqueuedVertexBuffers;
  typedef phash_set<IndexBufferContext *, pointer_hash> IndexBuffers;
  typedef phash_set< PT(GeomPrimitive) > EnqueuedIndexBuffers;

  ReMutex _lock;
  string _name;
  Textures _prepared_textures, _released_textures;  
  EnqueuedTextures _enqueued_textures;
  Geoms _prepared_geoms, _released_geoms;  
  EnqueuedGeoms _enqueued_geoms;
  Shaders _prepared_shaders, _released_shaders;
  EnqueuedShaders _enqueued_shaders;
  VertexBuffers _prepared_vertex_buffers, _released_vertex_buffers;  
  EnqueuedVertexBuffers _enqueued_vertex_buffers;
  IndexBuffers _prepared_index_buffers, _released_index_buffers;  
  EnqueuedIndexBuffers _enqueued_index_buffers;

public:
  BufferResidencyTracker _texture_residency;
  BufferResidencyTracker _vbuffer_residency;
  BufferResidencyTracker _ibuffer_residency;

private:
  static int _name_index;

  friend class GraphicsStateGuardian;
};

#include "preparedGraphicsObjects.I"

#endif
