Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
renderer.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFX_RENDERER_H_
19 #define ION_GFX_RENDERER_H_
20 
21 #include <bitset>
22 #include <memory>
23 
24 #include "base/integral_types.h"
25 #include "ion/base/referent.h"
30 #include "ion/gfx/image.h"
31 #include "ion/gfx/node.h"
33 #include "ion/gfx/shaderprogram.h"
34 #include "ion/gfx/statetable.h"
35 #include "ion/gfx/uniform.h"
36 #include "ion/math/range.h"
37 
38 namespace ion {
39 namespace portgfx {
40 class Visual;
41 } // namespace portgfx
42 namespace gfx {
43 
44 class IndexBuffer;
45 class Shape;
46 
50 class ION_API Renderer : public base::Referent {
51  public:
54  enum Flag {
61 
65  kClearActiveTexture, // Sets the active image unit to unit 0.
67  kClearCubemaps, // Clears cubemaps from _all_ image units.
70  kClearSamplers, // Clears samplers from _all_ image units.
72  kClearTextures, // Clears textures from _all_ image units.
74 
85 
96  };
97  static const int kNumFlags = kSaveVertexArray + 1;
98  typedef std::bitset<kNumFlags> Flags;
99 
110  };
111  static const int kNumResourceTypes = kTexture + 1;
112 
120  kWriteOnly
121  };
122 
124  explicit Renderer(const GraphicsManagerPtr& gm);
125 
127  const Flags& GetFlags() const { return flags_; }
128  void ClearFlag(Flag flag) { flags_.reset(flag); }
129  void ClearFlags(const Flags& flags) { flags_ &= ~flags; }
130  void SetFlag(Flag flag) { flags_.set(flag); }
131  void SetFlags(const Flags& flags) { flags_ |= flags; }
132 
134  static const Flags& AllFlags();
135  static const Flags& AllClearFlags();
136  static const Flags& AllProcessFlags();
137  static const Flags& AllRestoreFlags();
138  static const Flags& AllSaveFlags();
139 
141  const GraphicsManagerPtr& GetGraphicsManager() const;
142 
145  gfx::ResourceManager* GetResourceManager() const;
146 
153  void BindFramebuffer(const FramebufferObjectPtr& fbo);
154 
162  const FramebufferObjectPtr GetCurrentFramebuffer() const;
163 
174  template <typename T>
175  void BindResource(T* holder);
176 
193  template <typename T>
194  void CreateOrUpdateResource(T* holder);
198  void CreateOrUpdateResources(const NodePtr& node);
201  void CreateOrUpdateShapeResources(const ShapePtr& shape);
202 
207  template <typename T>
208  void RequestForcedUpdate(T* holder);
211  void RequestForcedUpdates(const NodePtr& node);
214  void RequestForcedShapeUpdates(const ShapePtr& shape);
215 
219  void ProcessStateTable(const StateTablePtr& state_table);
220 
227  template <typename T>
228  void CreateResourceWithExternallyManagedId(T* holder, uint32 gl_id);
229 
234  void SetInitialUniformValue(const Uniform& u);
235 
239  void ResolveMultisampleFramebuffer(const FramebufferObjectPtr& ms_fbo,
240  const FramebufferObjectPtr& dest_fbo);
241 
244  virtual void DrawScene(const NodePtr& node);
247  void ProcessResourceInfoRequests();
248 
253  template <typename T>
254  uint32 GetResourceGlId(T* holder);
255 
266  void SetTextureImageUnitRange(const math::Range1i& units);
267 
284  void MapBufferObjectData(const BufferObjectPtr& buffer,
285  BufferObjectDataMapMode mode);
291  void MapBufferObjectDataRange(const BufferObjectPtr& buffer,
292  BufferObjectDataMapMode mode,
293  const math::Range1ui& range);
297  void UnmapBufferObjectData(const BufferObjectPtr& buffer);
298 
302  return default_shader_;
303  }
304 
312  void UpdateStateFromOpenGL(int window_width, int window_height);
313 
317  void UpdateStateFromStateTable(const StateTablePtr& state_to_clear);
318 
321  const StateTable& GetStateTable() const;
322 
326  void UpdateDefaultFramebufferFromOpenGL();
327 
334  void ClearCachedBindings();
335 
339  const ImagePtr ReadImage(const math::Range2i& range, Image::Format format,
340  const base::AllocatorPtr& allocator);
341 
345  void PushDebugMarker(const std::string& marker);
349  void PopDebugMarker();
350 
352  template <typename HolderType>
353  void ClearResources(const HolderType* holder);
354 
356  void ClearAllResources();
357 
360  void ReleaseResources();
361 
364  void ClearTypedResources(ResourceType type);
365 
368  static void DestroyStateCache(const portgfx::Visual* visual);
369 
372  static void DestroyCurrentStateCache();
373 
377  size_t GetGpuMemoryUsage(ResourceType type) const;
378 
379  protected:
382  ~Renderer() override;
383 
384  private:
386  template <int NumModifiedBits> class Resource;
387  class BufferResource;
388  class FramebufferResource;
389  class ResourceBinder;
390  class ResourceManager;
391  class SamplerResource;
392  class ScopedLabel;
393  class ShaderInputRegistryResource;
394  class ShaderProgramResource;
395  class ShaderResource;
396  class TextureResource;
397  class VertexArrayResource;
398  class VertexArrayEmulatorResource;
399 
401  template <typename T>
402  struct HolderToResource;
403 
408  ResourceBinderMap;
409  static ResourceBinderMap& GetResourceBinderMap();
410  ResourceBinder* GetOrCreateInternalResourceBinder(int line) const;
411  ResourceBinder* GetInternalResourceBinder(size_t* visual_id) const;
412 
413  static const ShaderProgramPtr CreateDefaultShaderProgram(
414  const base::AllocatorPtr& allocator);
415  static void SetResourceHolderBit(const ResourceHolder* holder, int bit);
416 
418  Flags flags_;
419 
421  std::unique_ptr<ResourceManager> resource_manager_;
422 
424  ShaderProgramPtr default_shader_;
425 };
426 
429 
430 } // namespace gfx
431 } // namespace ion
432 
433 #endif // ION_GFX_RENDERER_H_
std::string buffer
Format
Supported image formats.
Definition: image.h:38
Flag
The below flags determine what operations will be performed in a call to DrawScene().
Definition: renderer.h:54
This class can be used in place of std::unordered_map to allow an Ion Allocator to be used for memory...
const Flags & GetFlags() const
Returns/sets rendering flags.
Definition: renderer.h:127
std::string type
Definition: printer.cc:353
const ShaderProgramPtr & GetDefaultShaderProgram() const
Returns the default shader program that the Renderer uses if no other shader program is set...
Definition: renderer.h:301
Range< 1, int32 > Range1i
Definition: range.h:363
Process any outstanding requests for information about internal resources that have been made through...
Definition: renderer.h:57
ResourceType
The types of resources created by the renderer.
Definition: renderer.h:101
Whether to query OpenGL for current states and save them, which can be restored using the above kRest...
Definition: renderer.h:89
A Uniform instance represents a uniform shader argument.
Definition: uniform.h:76
Range< 2, int32 > Range2i
Definition: range.h:371
Range< 1, uint32 > Range1ui
Definition: range.h:364
Whether to restore certain GL state types when drawing is finished.
Definition: renderer.h:78
Thread-safe abstract base class.
Definition: referent.h:49
void SetFlags(const Flags &flags)
Definition: renderer.h:131
std::bitset< kNumFlags > Flags
Definition: renderer.h:98
void ClearFlags(const Flags &flags)
Definition: renderer.h:129
Release any internal resources that have been marked for destruction, including OpenGL objects...
Definition: renderer.h:60
BufferObjectDataMapMode
The possible ways a BufferObject's data can be mapped.
Definition: renderer.h:117
void SetFlag(Flag flag)
Definition: renderer.h:130
Whether to clear (set to 0) certain GL objects when drawing is finished.
Definition: renderer.h:65
A ResourceManager is an interface for getting information about a Renderer's internal resources...
Opaque class that sets up an offscreen OpenGL context/surface/visual in a platform-specific way to al...
Definition: visual.h:40
void ClearFlag(Flag flag)
Definition: renderer.h:128
A StateTable represents a collection of graphical state items that affect OpenGL rendering.
Definition: statetable.h:48
base::ReferentPtr< Renderer >::Type RendererPtr
Convenience typedef for shared pointer to a Renderer.
Definition: renderer.h:428
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
The Renderer class handles rendering ION scene graphs using OpenGL.
Definition: renderer.h:50
ResourceHolder is an internal base class for objects that hold resources managed by an outside entity...