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