HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
drawTarget.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_GLF_DRAW_TARGET_H
8 #define PXR_IMAGING_GLF_DRAW_TARGET_H
9 
10 /// \file glf/drawTarget.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/imaging/glf/api.h"
16 
17 #include "pxr/base/gf/vec2i.h"
18 #include "pxr/base/gf/matrix4d.h"
20 #include "pxr/base/tf/refBase.h"
21 #include "pxr/base/tf/weakBase.h"
22 
23 #include <map>
24 #include <memory>
25 #include <set>
26 #include <string>
27 
28 
30 
31 
33 typedef std::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
34 
35 /// \class GlfDrawTarget
36 ///
37 /// A class representing a GL render target with mutliple image attachments.
38 ///
39 /// A DrawTarget is essentially a custom render pass into which several
40 /// arbitrary variables can be output into. These can later be used as
41 /// texture samplers by GLSL shaders.
42 ///
43 /// The DrawTarget maintains a map of named attachments that correspond
44 /// to GL_TEXTURE_2D mages. By default, DrawTargets also create a depth
45 /// component that is used both as a depth buffer during the draw pass,
46 /// and can later be accessed as a regular GL_TEXTURE_2D data. Stencils
47 /// are also available (by setting the format to GL_DEPTH_STENCIL and
48 /// the internalFormat to GL_DEPTH24_STENCIL8)
49 ///
50 class GlfDrawTarget : public TfRefBase, public TfWeakBase {
51 public:
53 
54 public:
55 
56  /// Returns a new instance.
57  GLF_API
58  static GlfDrawTargetRefPtr New( GfVec2i const & size,
59  bool requestMSAA = false );
60 
61  /// Returns a new instance.
62  /// GL framebuffers cannot be shared across contexts, but texture
63  /// attachments can. In order to reflect this, GlfDrawTargets hold
64  /// onto their maps of attachments through a RefPtr that can be shared
65  /// by multiple GlfDrawTargets, one for each of the active GL contexts
66  /// (ex. one for each active QT viewer).
67  /// This constructor creates a new framebuffer, but populates its map of
68  /// attachments by sharing the RefPtr of the source GlfDrawTarget.
69  GLF_API
70  static GlfDrawTargetRefPtr New( GlfDrawTargetPtr const & drawtarget );
71 
72  class Attachment : public GlfTexture {
73  public:
75 
76  GLF_API
77  static AttachmentRefPtr New(int glIndex, GLenum format, GLenum type,
79  unsigned int numSamples);
80 
81  GLF_API
82  ~Attachment() override;
83 
84  /// Returns the GL texture index (can be used as any regular GL texture)
85  GLuint GetGlTextureName() override;
86 
87  /// Returns the GL texture index multisampled of this attachment
88  GLuint GetGlTextureMSName() const { return _textureNameMS; }
89 
90  /// Returns the GL format of the texture (GL_RGB, GL_DEPTH_COMPONENT...)
91  GLenum GetFormat() const { return _format; }
92 
93  /// Returns the GL type of the texture (GL_BYTE, GL_INT, GL_FLOAT...)
94  GLenum GetType() const { return _type; }
95 
96  /// Returns the GL internalFormat of the texture
97  GLenum GetInternalFormat() const { return _internalFormat; }
98 
99  /// Returns the GL attachment point index in the framebuffer.
100  int GetAttach() const { return _glIndex; }
101 
102  /// Resize the attachment recreating the texture
103  GLF_API
104  void ResizeTexture(const GfVec2i &size);
105 
106  // GlfTexture overrides
107  GLF_API
108  BindingVector GetBindings(TfToken const & identifier,
109  GLuint samplerName) override;
110  GLF_API
111  VtDictionary GetTextureInfo(bool forceLoad) override;
112 
113  /// Updates the contents signature for the underlying texture
114  /// to allow downstream consumers to know that the texture image
115  /// data may have changed.
116  GLF_API
117  void TouchContents();
118 
119  private:
120  Attachment(int glIndex, GLenum format, GLenum type,
122  unsigned int numSamples);
123 
124  void _GenTexture();
125  void _DeleteTexture();
126 
127  GLuint _textureName;
128  GLuint _textureNameMS;
129 
130  GLenum _format,
131  _type,
132  _internalFormat;
133 
134  int _glIndex;
135 
136  GfVec2i _size;
137 
138  unsigned int _numSamples;
139  };
140 
142 
143  typedef std::map<std::string, AttachmentRefPtr> AttachmentsMap;
144 
145  /// Add an attachment to the DrawTarget.
146  GLF_API
147  void AddAttachment( std::string const & name,
149 
150  /// Removes the named attachment from the DrawTarget.
151  GLF_API
152  void DeleteAttachment( std::string const & name );
153 
154  /// Clears all the attachments for this DrawTarget.
155  GLF_API
156  void ClearAttachments();
157 
158  /// Copies the list of attachments from DrawTarget.
159  GLF_API
160  void CloneAttachments( GlfDrawTargetPtr const & drawtarget );
161 
162  /// Returns the list of Attachments for this DrawTarget.
163  GLF_API
164  AttachmentsMap const & GetAttachments() const;
165 
166  /// Returns the attachment with a given name or TfNullPtr;
167  GLF_API
168  AttachmentRefPtr GetAttachment(std::string const & name);
169 
170  /// Write the Attachment buffer to an image file (debugging).
171  GLF_API
172  bool WriteToFile(std::string const & name,
173  std::string const & filename,
174  GfMatrix4d const & viewMatrix = GfMatrix4d(1),
175  GfMatrix4d const & projectionMatrix = GfMatrix4d(1));
176 
177  /// Resize the DrawTarget.
178  GLF_API
179  void SetSize( GfVec2i );
180 
181  /// Returns the size of the DrawTarget.
182  GfVec2i const & GetSize() const { return _size; }
183 
184  /// Returns if the draw target uses msaa
185  bool HasMSAA() const { return (_numSamples > 1); }
186 
187  /// Returns the framebuffer object Id.
188  GLF_API
189  GLuint GetFramebufferId() const;
190 
191  /// Returns the id of the framebuffer object with MSAA buffers.
192  GLF_API
193  GLuint GetFramebufferMSId() const;
194 
195  /// Binds the framebuffer.
196  GLF_API
197  void Bind();
198 
199  /// Unbinds the framebuffer.
200  GLF_API
201  void Unbind();
202 
203  /// Returns whether the framebuffer is currently bound.
204  GLF_API
205  bool IsBound() const;
206 
207  /// Resolve the MSAA framebuffer to a regular framebuffer. If there
208  /// is no MSAA enabled, this function does nothing.
209  GLF_API
210  void Resolve();
211 
212  /// Resolve several MSAA framebuffers at once. If any framebuffers don't
213  /// have MSAA enabled, nothing happens to them.
214  GLF_API
215  static void Resolve(const std::vector<GlfDrawTarget*>& drawTargets);
216 
217  /// Updates the contents signature for attached textures
218  /// to allow downstream consumers to know that the texture image
219  /// data may have changed.
220  GLF_API
221  void TouchContents();
222 
223  /// Returns whether the enclosed framebuffer object is complete.
224  /// If \a reason is non-NULL, and this framebuffer is not valid,
225  /// sets \a reason to the reason why not.
226  GLF_API
227  bool IsValid(std::string * reason = NULL);
228 
229 protected:
230 
231  /// Weak/Ref-based container for the the map of texture attachments.
232  /// Multiple GlfDrawTargets can jointly share their attachment textures :
233  /// this construction allows the use of a RefPtr on the map of attachments.
234  class AttachmentsContainer : public TfRefBase, public TfWeakBase {
235  public:
237  };
238 
239  GLF_API
240  GlfDrawTarget( GfVec2i const & size, bool requestMSAA );
241 
242  GLF_API
243  GlfDrawTarget( GlfDrawTargetPtr const & drawtarget );
244 
245  GLF_API
246  virtual ~GlfDrawTarget();
247 
248 private:
249  void _GenFrameBuffer();
250 
251  void _BindAttachment( GlfDrawTarget::AttachmentRefPtr const & a );
252 
253  GLuint _AllocAttachment( GLenum format, GLenum type );
254 
255  AttachmentsMap & _GetAttachments() const;
256 
257  void _DeleteAttachments( );
258 
259  void _AllocDepth( );
260 
261  bool _Validate(std::string * reason = NULL);
262 
263  void _SaveBindingState();
264 
265  void _RestoreBindingState();
266 
267  void _Resolve();
268 
269  GLuint _framebuffer;
270  GLuint _framebufferMS;
271 
272  GLuint _unbindRestoreReadFB,
273  _unbindRestoreDrawFB;
274 
275  int _bindDepth;
276 
277  GfVec2i _size;
278 
279  unsigned int _numSamples;
280 
281  TfRefPtr<AttachmentsContainer> _attachmentsPtr;
282  GlfGLContextSharedPtr _owningContext;
283 };
284 
285 
287 
288 #endif // GLF_DRAW_TARGET_H
GLF_API bool WriteToFile(std::string const &name, std::string const &filename, GfMatrix4d const &viewMatrix=GfMatrix4d(1), GfMatrix4d const &projectionMatrix=GfMatrix4d(1))
Write the Attachment buffer to an image file (debugging).
bool HasMSAA() const
Returns if the draw target uses msaa.
Definition: drawTarget.h:185
GLF_API GlfDrawTarget(GfVec2i const &size, bool requestMSAA)
GLF_API AttachmentsMap const & GetAttachments() const
Returns the list of Attachments for this DrawTarget.
GT_API const UT_StringHolder filename
GLF_API void TouchContents()
GLF_API AttachmentRefPtr GetAttachment(std::string const &name)
Returns the attachment with a given name or TfNullPtr;.
#define GLF_API
Definition: api.h:23
std::map< std::string, AttachmentRefPtr > AttachmentsMap
Definition: drawTarget.h:143
Definition: vec2i.h:43
GLF_API void ResizeTexture(const GfVec2i &size)
Resize the attachment recreating the texture.
TfDeclarePtrs< class Attachment >::RefPtr AttachmentRefPtr
Definition: drawTarget.h:74
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_WEAK_AND_REF_PTRS(GlfDrawTarget)
GLF_API void SetSize(GfVec2i)
Resize the DrawTarget.
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
static GLF_API AttachmentRefPtr New(int glIndex, GLenum format, GLenum type, GLenum internalFormat, GfVec2i size, unsigned int numSamples)
unsigned int GLuint
Definition: cl.hpp:167
unsigned int GLenum
Definition: cl.hpp:166
GLF_API GLuint GetFramebufferMSId() const
Returns the id of the framebuffer object with MSAA buffers.
GLF_API void Unbind()
Unbinds the framebuffer.
GLF_API BindingVector GetBindings(TfToken const &identifier, GLuint samplerName) override
GLenum GetFormat() const
Returns the GL format of the texture (GL_RGB, GL_DEPTH_COMPONENT...)
Definition: drawTarget.h:91
GLsizei GLenum internalFormat
Definition: RE_OGL.h:202
GlfDrawTarget This
Definition: drawTarget.h:52
int GetAttach() const
Returns the GL attachment point index in the framebuffer.
Definition: drawTarget.h:100
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
Definition: token.h:70
GLF_API bool IsValid(std::string *reason=NULL)
GLenum GetInternalFormat() const
Returns the GL internalFormat of the texture.
Definition: drawTarget.h:97
GLF_API VtDictionary GetTextureInfo(bool forceLoad) override
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
GLuint GetGlTextureName() override
Returns the GL texture index (can be used as any regular GL texture)
GLF_API bool IsBound() const
Returns whether the framebuffer is currently bound.
GLF_API ~Attachment() override
GLF_API void Resolve()
GLuint const GLchar * name
Definition: glcorearb.h:786
TfDeclarePtrs< class Attachment >::RefPtr AttachmentRefPtr
Definition: drawTarget.h:141
GLF_API GLuint GetFramebufferId() const
Returns the framebuffer object Id.
std::shared_ptr< class GlfGLContext > GlfGLContextSharedPtr
Definition: drawTarget.h:33
GLenum GetType() const
Returns the GL type of the texture (GL_BYTE, GL_INT, GL_FLOAT...)
Definition: drawTarget.h:94
GLF_API void CloneAttachments(GlfDrawTargetPtr const &drawtarget)
Copies the list of attachments from DrawTarget.
GLF_API void TouchContents()
GLsizeiptr size
Definition: glcorearb.h:664
GfVec2i const & GetSize() const
Returns the size of the DrawTarget.
Definition: drawTarget.h:182
static GLF_API GlfDrawTargetRefPtr New(GfVec2i const &size, bool requestMSAA=false)
Returns a new instance.
virtual GLF_API ~GlfDrawTarget()
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GLF_API void ClearAttachments()
Clears all the attachments for this DrawTarget.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GLF_API void AddAttachment(std::string const &name, GLenum format, GLenum type, GLenum internalFormat)
Add an attachment to the DrawTarget.
GLF_API void Bind()
Binds the framebuffer.
IMF_EXPORT int numSamples(int s, int a, int b)
GLF_API void DeleteAttachment(std::string const &name)
Removes the named attachment from the DrawTarget.
std::vector< Binding > BindingVector
Definition: texture.h:71
GLuint GetGlTextureMSName() const
Returns the GL texture index multisampled of this attachment.
Definition: drawTarget.h:88