Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
resourcemanager.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFX_RESOURCEMANAGER_H_
19 #define ION_GFX_RESOURCEMANAGER_H_
20 
21 #include <functional>
22 #include <string>
23 #include <vector>
24 
25 #include "ion/base/allocatable.h"
26 #include "ion/base/lockguards.h"
27 #include "ion/base/referent.h"
28 #include "ion/gfx/image.h"
29 #include "ion/gfx/openglobjects.h"
30 #include "ion/port/mutex.h"
31 
32 namespace ion {
33 namespace gfx {
34 
36 class AttributeArray;
37 class BufferObject;
38 class FramebufferObject;
39 class GraphicsManager;
40 class Sampler;
41 class Shader;
42 class ShaderProgram;
43 class TextureBase;
46 typedef base::ReferentPtr<FramebufferObject>::Type FramebufferObjectPtr;
52 
69 class ION_API ResourceManager : public base::Allocatable {
70  public:
72 
77 
83  struct ResourceInfo {
84  ResourceInfo() : id(0) {}
86  GLuint id;
88  std::string label;
89  };
90 
95 
100  size_t vertex_count;
101  };
104  GLuint target;
105  };
111  };
114  GLenum unit;
116  GLuint sampler;
118  GLuint width;
119  GLuint height;
122  };
123 
129 
131  struct PlatformInfo {
132  PlatformInfo() : major_version(-1), minor_version(-1), glsl_version(-1) {}
136  GLuint glsl_version;
137 
139  GLfloat aliased_line_width_range[2];
140  GLfloat aliased_point_size_range[2];
141 
159  GLint max_viewport_dims[2];
161 
163  std::vector<GLenum> compressed_texture_formats;
164  std::vector<GLenum> shader_binary_formats;
165 
167  std::string extensions;
168  std::string renderer;
169  std::string vendor;
170  std::string version_string;
171  };
172 
178  : images(base::AllocationManager::GetDefaultAllocatorForLifetime(
179  base::kMediumTerm)) {}
182  };
183 
186  template <typename T> struct InfoCallback {
187  typedef std::function<void(const std::vector<T>& infos)> Type;
188  };
189 
191 
196 
200  return graphics_manager_;
201  }
202 
208  template <typename HolderType, typename InfoType>
210  const typename base::ReferentPtr<HolderType>::Type& holder,
211  const typename InfoCallback<InfoType>::Type& callback) {
212  if (holder.Get()) {
213  base::LockGuard lock_guard(&this->request_mutex_);
214  GetResourceRequestVector<HolderType, InfoType>()->push_back(
215  ResourceRequest<HolderType, InfoType>(holder, callback));
216  }
217  }
218 
221  template <typename HolderType, typename InfoType>
223  const typename InfoCallback<InfoType>::Type& callback) {
224  base::LockGuard lock_guard(&this->request_mutex_);
225  GetResourceRequestVector<HolderType, InfoType>()->push_back(
227  typename base::ReferentPtr<HolderType>::Type(), callback));
228  }
229 
232  void RequestPlatformInfo(const InfoCallback<PlatformInfo>::Type& callback);
233 
240  void RequestTextureImage(
241  GLuint id, const InfoCallback<TextureImageInfo>::Type& callback);
242 
243  protected:
245  template <typename InfoType>
246  struct DataRequest {
248  GLuint id_in,
249  const typename InfoCallback<InfoType>::Type& callback_in)
250  : id(id_in), callback(callback_in) {}
251  GLuint id;
253  };
254 
256  template <typename HolderType, typename InfoType>
259  const typename base::ReferentPtr<HolderType>::Type& holder_in,
260  const typename InfoCallback<InfoType>::Type& callback_in)
261  : holder(holder_in), callback(callback_in) {}
264  };
265 
269  explicit ResourceManager(const GraphicsManagerPtr& gm);
270  ~ResourceManager() override;
271 
274  template <typename HolderType, typename InfoType>
275  std::vector<ResourceRequest<HolderType, InfoType> >*
276  GetResourceRequestVector();
277 
280  template <typename InfoType>
281  std::vector<DataRequest<InfoType> >* GetDataRequestVector();
282 
287  template <typename InfoType>
288  void FillInfoFromOpenGL(InfoType* info);
289 
292 
293  private:
294  GraphicsManagerPtr graphics_manager_;
295 
297  std::vector<ResourceRequest<AttributeArray, ArrayInfo> > array_requests_;
298  std::vector<ResourceRequest<BufferObject, BufferInfo> > buffer_requests_;
299  std::vector<ResourceRequest<FramebufferObject, FramebufferInfo> >
300  framebuffer_requests_;
301  std::vector<DataRequest<PlatformInfo> > platform_requests_;
302  std::vector<ResourceRequest<ShaderProgram, ProgramInfo> >
303  program_requests_;
304  std::vector<ResourceRequest<Sampler, SamplerInfo> > sampler_requests_;
305  std::vector<ResourceRequest<Shader, ShaderInfo> > shader_requests_;
306  std::vector<DataRequest<TextureImageInfo> > texture_image_requests_;
307  std::vector<ResourceRequest<TextureBase, TextureInfo> > texture_requests_;
308 };
309 
310 } // namespace gfx
311 } // namespace ion
312 
313 #endif // ION_GFX_RESOURCEMANAGER_H_
base::ReferentPtr< FramebufferObject >::Type FramebufferObjectPtr
Convenience typedef for shared pointer to a FramebufferObject.
A ShaderInfo corresponds to an OpenGL Shader Object.
GLint max_color_attachments
Maximum capabilities.
A SamplerInfo corresponds to an OpenGL Sampler Object.
The below info types contain additional informative fields about resources.
Format
Supported image formats.
Definition: image.h:38
std::vector< GLenum > compressed_texture_formats
Formats.
gfx::BufferInfo< BufferTargetInfo > BufferInfo
DataRequest(GLuint id_in, const typename InfoCallback< InfoType >::Type &callback_in)
GLenum unit
The texture unit the texture is bound to.
gfx::FramebufferInfo< FramebufferResourceInfo > FramebufferInfo
Image::Format format
The format of the texture.
GLuint target
The buffer's target, either GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER.
const GraphicsManagerPtr & GetGraphicsManager() const
Functions.
base::AllocVector< ImagePtr > images
void RequestResourceInfo(const typename base::ReferentPtr< HolderType >::Type &holder, const typename InfoCallback< InfoType >::Type &callback)
Requests information about a particular resource if the ReferentPtr is non-NULL.
std::function< void(const std::vector< ion::gfx::ProgramInfo > &infos)> Type
Callbacks called when requested resource information is available.
uint32 id
base::ReferentPtr< Sampler >::Type SamplerPtr
Convenience typedef for shared pointer to a Sampler.
Struct for getting information about the local OpenGL platform.
GLuint width
The dimensions of the texture.
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
A LockGuard locks a mutex when created, and unlocks it when destroyed.
Definition: lockguards.h:90
gfx::TextureInfo< TextureResourceInfo > TextureInfo
Allocatable is an abstract base class for classes whose memory is managed by an Allocator.
Definition: allocatable.h:60
Wrapper struct for resource info requests.
Struct containing information about a texture and its image(s).
gfx::ProgramInfo< ResourceInfo > ProgramInfo
InfoCallback< InfoType >::Type callback
port::Mutex request_mutex_
For locking access to request vectors.
GLuint sampler
The sampler that is currently bound to the same unit as the texture.
base::ReferentPtr< ShaderProgram >::Type ShaderProgramPtr
gfx::SamplerInfo< ResourceInfo > SamplerInfo
base::ReferentPtr< Shader >::Type ShaderPtr
Convenience typedef for shared pointers to Shaders.
void RequestAllResourceInfos(const typename InfoCallback< InfoType >::Type &callback)
Requests information about all resources of the passed type.
A ProgramInfo corresponds to an OpenGL Program Object.
A ResourceManager is an interface for getting information about a Renderer's internal resources...
ResourceRequest(const typename base::ReferentPtr< HolderType >::Type &holder_in, const typename InfoCallback< InfoType >::Type &callback_in)
base::ReferentPtr< TextureBase >::Type TextureBasePtr
Wrapper struct for data requests.
base::ReferentPtr< AttributeArray >::Type AttributeArrayPtr
Convenience typedef for shared pointer to a AttributeArray.
SharedPtr< AttributeArray > Type
Definition: referent.h:60
A BufferInfo corresponds to an OpenGL Framebuffer Object.
size_t vertex_count
The total number of vertices calculated the last time the array was rendered.
base::ReferentPtr< GraphicsManager >::Type GraphicsManagerPtr
Convenience typedef for shared pointer to a GraphicsManager.
gfx::RenderbufferInfo< ResourceInfo > RenderbufferInfo
A BufferInfo corresponds to an OpenGL Buffer Object.
Definition: openglobjects.h:85
gfx::ArrayInfo< ArrayResourceInfo > ArrayInfo
The actual info types.
RenderbufferInfo color0_renderbuffer
The renderbuffers attached to the framebuffer, if any.
base::ReferentPtr< BufferObject >::Type BufferObjectPtr
Convenience typedef for shared pointer to a BufferObject.
Definition: bufferobject.h:31
The below structs correspond to OpenGL "objects." For example, a BufferObject corresponds to an OpenG...
Definition: openglobjects.h:42
A TextureInfo corresponds to an OpenGL Texture Object.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
kMediumTerm is used for objects that don't fall into the kShortTerm or kLongTerm categories.
Definition: allocator.h:40
base::ReferentPtr< HolderType >::Type holder
std::string label
The label of the ResourceHolder that owns the Resource.
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
Definition: mutex.h:34
InfoCallback< InfoType >::Type callback
gfx::ShaderInfo< ResourceInfo > ShaderInfo
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...
Definition: allocvector.h:50