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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_GLF_DRAW_TARGET_H
25 #define PXR_IMAGING_GLF_DRAW_TARGET_H
26 
27 /// \file glf/drawTarget.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/glf/api.h"
32 
33 #include "pxr/base/gf/vec2i.h"
34 #include "pxr/base/gf/matrix4d.h"
36 #include "pxr/base/tf/refBase.h"
37 #include "pxr/base/tf/weakBase.h"
38 
39 #include "pxr/imaging/garch/gl.h"
40 
41 #include <map>
42 #include <memory>
43 #include <set>
44 #include <string>
45 
46 
48 
49 
51 typedef std::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
52 
53 /// \class GlfDrawTarget
54 ///
55 /// A class representing a GL render target with mutliple image attachments.
56 ///
57 /// A DrawTarget is essentially a custom render pass into which several
58 /// arbitrary variables can be output into. These can later be used as
59 /// texture samplers by GLSL shaders.
60 ///
61 /// The DrawTarget maintains a map of named attachments that correspond
62 /// to GL_TEXTURE_2D mages. By default, DrawTargets also create a depth
63 /// component that is used both as a depth buffer during the draw pass,
64 /// and can later be accessed as a regular GL_TEXTURE_2D data. Stencils
65 /// are also available (by setting the format to GL_DEPTH_STENCIL and
66 /// the internalFormat to GL_DEPTH24_STENCIL8)
67 ///
68 class GlfDrawTarget : public TfRefBase, public TfWeakBase {
69 public:
71 
72 public:
73 
74  /// Returns a new instance.
75  GLF_API
76  static GlfDrawTargetRefPtr New( GfVec2i const & size,
77  bool requestMSAA = false );
78 
79  /// Returns a new instance.
80  /// GL framebuffers cannot be shared across contexts, but texture
81  /// attachments can. In order to reflect this, GlfDrawTargets hold
82  /// onto their maps of attachments through a RefPtr that can be shared
83  /// by multiple GlfDrawTargets, one for each of the active GL contexts
84  /// (ex. one for each active QT viewer).
85  /// This constructor creates a new framebuffer, but populates its map of
86  /// attachments by sharing the RefPtr of the source GlfDrawTarget.
87  GLF_API
88  static GlfDrawTargetRefPtr New( GlfDrawTargetPtr const & drawtarget );
89 
90  class Attachment : public GlfTexture {
91  public:
93 
94  GLF_API
95  static AttachmentRefPtr New(int glIndex, GLenum format, GLenum type,
97  unsigned int numSamples);
98 
99  GLF_API
100  ~Attachment() override;
101 
102  /// Returns the GL texture index (can be used as any regular GL texture)
103  GLuint GetGlTextureName() override;
104 
105  /// Returns the GL texture index multisampled of this attachment
106  GLuint GetGlTextureMSName() const { return _textureNameMS; }
107 
108  /// Returns the GL format of the texture (GL_RGB, GL_DEPTH_COMPONENT...)
109  GLenum GetFormat() const { return _format; }
110 
111  /// Returns the GL type of the texture (GL_BYTE, GL_INT, GL_FLOAT...)
112  GLenum GetType() const { return _type; }
113 
114  /// Returns the GL attachment point index in the framebuffer.
115  int GetAttach() const { return _glIndex; }
116 
117  /// Resize the attachment recreating the texture
118  GLF_API
119  void ResizeTexture(const GfVec2i &size);
120 
121  // GlfTexture overrides
122  GLF_API
123  BindingVector GetBindings(TfToken const & identifier,
124  GLuint samplerName) override;
125  GLF_API
126  VtDictionary GetTextureInfo(bool forceLoad) override;
127 
128  /// Updates the contents signature for the underlying texture
129  /// to allow downstream consumers to know that the texture image
130  /// data may have changed.
131  GLF_API
132  void TouchContents();
133 
134  private:
135  Attachment(int glIndex, GLenum format, GLenum type,
137  unsigned int numSamples);
138 
139  void _GenTexture();
140  void _DeleteTexture();
141 
142  GLuint _textureName;
143  GLuint _textureNameMS;
144 
145  GLenum _format,
146  _type,
147  _internalFormat;
148 
149  int _glIndex;
150 
151  GfVec2i _size;
152 
153  unsigned int _numSamples;
154  };
155 
157 
158  typedef std::map<std::string, AttachmentRefPtr> AttachmentsMap;
159 
160  /// Add an attachment to the DrawTarget.
161  GLF_API
162  void AddAttachment( std::string const & name,
164 
165  /// Removes the named attachment from the DrawTarget.
166  GLF_API
167  void DeleteAttachment( std::string const & name );
168 
169  /// Clears all the attachments for this DrawTarget.
170  GLF_API
171  void ClearAttachments();
172 
173  /// Copies the list of attachments from DrawTarget.
174  GLF_API
175  void CloneAttachments( GlfDrawTargetPtr const & drawtarget );
176 
177  /// Returns the list of Attachments for this DrawTarget.
178  GLF_API
179  AttachmentsMap const & GetAttachments() const;
180 
181  /// Returns the attachment with a given name or TfNullPtr;
182  GLF_API
184 
185  /// Write the Attachment buffer to an image file (debugging).
186  GLF_API
187  bool WriteToFile(std::string const & name,
188  std::string const & filename,
189  GfMatrix4d const & viewMatrix = GfMatrix4d(1),
190  GfMatrix4d const & projectionMatrix = GfMatrix4d(1));
191 
192  /// Resize the DrawTarget.
193  GLF_API
194  void SetSize( GfVec2i );
195 
196  /// Returns the size of the DrawTarget.
197  GfVec2i const & GetSize() const { return _size; }
198 
199  /// Returns if the draw target uses msaa
200  bool HasMSAA() const { return (_numSamples > 1); }
201 
202  /// Returns the framebuffer object Id.
203  GLF_API
204  GLuint GetFramebufferId() const;
205 
206  /// Returns the id of the framebuffer object with MSAA buffers.
207  GLF_API
208  GLuint GetFramebufferMSId() const;
209 
210  /// Binds the framebuffer.
211  GLF_API
212  void Bind();
213 
214  /// Unbinds the framebuffer.
215  GLF_API
216  void Unbind();
217 
218  /// Returns whether the framebuffer is currently bound.
219  GLF_API
220  bool IsBound() const;
221 
222  /// Resolve the MSAA framebuffer to a regular framebuffer. If there
223  /// is no MSAA enabled, this function does nothing.
224  GLF_API
225  void Resolve();
226 
227  /// Resolve several MSAA framebuffers at once. If any framebuffers don't
228  /// have MSAA enabled, nothing happens to them.
229  GLF_API
230  static void Resolve(const std::vector<GlfDrawTarget*>& drawTargets);
231 
232  /// Updates the contents signature for attached textures
233  /// to allow downstream consumers to know that the texture image
234  /// data may have changed.
235  GLF_API
236  void TouchContents();
237 
238  /// Returns whether the enclosed framebuffer object is complete.
239  /// If \a reason is non-NULL, and this framebuffer is not valid,
240  /// sets \a reason to the reason why not.
241  GLF_API
242  bool IsValid(std::string * reason = NULL);
243 
244 protected:
245 
246  /// Weak/Ref-based container for the the map of texture attachments.
247  /// Multiple GlfDrawTargets can jointly share their attachment textures :
248  /// this construction allows the use of a RefPtr on the map of attachments.
249  class AttachmentsContainer : public TfRefBase, public TfWeakBase {
250  public:
252  };
253 
254  GLF_API
255  GlfDrawTarget( GfVec2i const & size, bool requestMSAA );
256 
257  GLF_API
258  GlfDrawTarget( GlfDrawTargetPtr const & drawtarget );
259 
260  GLF_API
261  virtual ~GlfDrawTarget();
262 
263 private:
264  void _GenFrameBuffer();
265 
266  void _BindAttachment( GlfDrawTarget::AttachmentRefPtr const & a );
267 
268  GLuint _AllocAttachment( GLenum format, GLenum type );
269 
270  AttachmentsMap & _GetAttachments() const;
271 
272  void _DeleteAttachments( );
273 
274  void _AllocDepth( );
275 
276  bool _Validate(std::string * reason = NULL);
277 
278  void _SaveBindingState();
279 
280  void _RestoreBindingState();
281 
282  void _Resolve();
283 
284  GLuint _framebuffer;
285  GLuint _framebufferMS;
286 
287  GLuint _unbindRestoreReadFB,
288  _unbindRestoreDrawFB;
289 
290  int _bindDepth;
291 
292  GfVec2i _size;
293 
294  unsigned int _numSamples;
295 
296  TfRefPtr<AttachmentsContainer> _attachmentsPtr;
297  GlfGLContextSharedPtr _owningContext;
298 };
299 
300 
302 
303 #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:200
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
GLsizeiptr size
Definition: glew.h:1681
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:40
std::map< std::string, AttachmentRefPtr > AttachmentsMap
Definition: drawTarget.h:158
GLuint const GLchar * name
Definition: glew.h:1814
Definition: vec2i.h:61
GLF_API void ResizeTexture(const GfVec2i &size)
Resize the attachment recreating the texture.
TfDeclarePtrs< class Attachment >::RefPtr AttachmentRefPtr
Definition: drawTarget.h:92
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_WEAK_AND_REF_PTRS(GlfDrawTarget)
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
GLF_API void SetSize(GfVec2i)
Resize the DrawTarget.
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
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:109
GlfDrawTarget This
Definition: drawTarget.h:70
int GetAttach() const
Returns the GL attachment point index in the framebuffer.
Definition: drawTarget.h:115
Definition: token.h:87
GLF_API bool IsValid(std::string *reason=NULL)
GLF_API VtDictionary GetTextureInfo(bool forceLoad) override
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()
TfDeclarePtrs< class Attachment >::RefPtr AttachmentRefPtr
Definition: drawTarget.h:156
GLF_API GLuint GetFramebufferId() const
Returns the framebuffer object Id.
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
std::shared_ptr< class GlfGLContext > GlfGLContextSharedPtr
Definition: drawTarget.h:51
GLenum GetType() const
Returns the GL type of the texture (GL_BYTE, GL_INT, GL_FLOAT...)
Definition: drawTarget.h:112
GLF_API void CloneAttachments(GlfDrawTargetPtr const &drawtarget)
Copies the list of attachments from DrawTarget.
GLF_API void TouchContents()
GLsizei const GLchar *const * string
Definition: glew.h:1844
GfVec2i const & GetSize() const
Returns the size of the DrawTarget.
Definition: drawTarget.h:197
static GLF_API GlfDrawTargetRefPtr New(GfVec2i const &size, bool requestMSAA=false)
Returns a new instance.
GLint GLint internalFormat
Definition: glew.h:1254
virtual GLF_API ~GlfDrawTarget()
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
GLF_API void ClearAttachments()
Clears all the attachments for this DrawTarget.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
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.
GLF_API void DeleteAttachment(std::string const &name)
Removes the named attachment from the DrawTarget.
std::vector< Binding > BindingVector
Definition: texture.h:88
GLuint GetGlTextureMSName() const
Returns the GL texture index multisampled of this attachment.
Definition: drawTarget.h:106