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 GLF_DRAWTARGET_H
25 #define GLF_DRAWTARGET_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/glf/api.h"
31 #include "pxr/imaging/glf/texture.h"
32 
33 #include "pxr/base/gf/vec2i.h"
34 #include "pxr/base/gf/matrix4d.h"
35 #include "pxr/base/tf/declarePtrs.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 <set>
43 #include <string>
44 
45 #include <boost/shared_ptr.hpp>
46 
47 PXR_NAMESPACE_OPEN_SCOPE
48 
49 
51 typedef boost::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
52 
68 class GlfDrawTarget : public TfRefBase, public TfWeakBase {
69 public:
70  typedef GlfDrawTarget This;
71 
72 public:
73 
75  GLF_API
76  static GlfDrawTargetRefPtr New( GfVec2i const & size,
77  bool requestMSAA = false );
78 
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,
96  GLenum internalFormat, GfVec2i size,
97  unsigned int numSamples);
98 
99  GLF_API
100  ~Attachment() override;
101 
103  GLuint GetGlTextureName() override;
104 
106  GLuint GetGlTextureMSName() const { return _textureNameMS; }
107 
109  GLenum GetFormat() const { return _format; }
110 
112  GLenum GetType() const { return _type; }
113 
115  int GetAttach() const { return _glIndex; }
116 
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 
131  GLF_API
132  void TouchContents();
133 
134  private:
135  Attachment(int glIndex, GLenum format, GLenum type,
136  GLenum internalFormat, GfVec2i size,
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 
161  GLF_API
162  void AddAttachment( std::string const & name,
163  GLenum format, GLenum type, GLenum internalFormat );
164 
166  GLF_API
167  void DeleteAttachment( std::string const & name );
168 
170  GLF_API
171  void ClearAttachments();
172 
174  GLF_API
175  void CloneAttachments( GlfDrawTargetPtr const & drawtarget );
176 
178  GLF_API
179  AttachmentsMap const & GetAttachments() const;
180 
182  GLF_API
183  AttachmentRefPtr GetAttachment(std::string const & name);
184 
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 
193  GLF_API
194  void SetSize( GfVec2i );
195 
197  GfVec2i const & GetSize() const { return _size; }
198 
200  bool HasMSAA() const { return (_numSamples > 1); }
201 
203  GLF_API
204  GLuint GetFramebufferId() const;
205 
207  GLF_API
208  GLuint GetFramebufferMSId() const;
209 
211  GLF_API
212  void Bind();
213 
215  GLF_API
216  void Unbind();
217 
219  GLF_API
220  bool IsBound() const;
221 
224  GLF_API
225  void Resolve();
226 
229  GLF_API
230  static void Resolve(const std::vector<GlfDrawTarget*>& drawTargets);
231 
235  GLF_API
236  void TouchContents();
237 
241  GLF_API
242  bool IsValid(std::string * reason = NULL);
243 
244 protected:
245 
249  class AttachmentsContainer : public TfRefBase, public TfWeakBase {
250  public:
251  AttachmentsMap attachments;
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 
301 PXR_NAMESPACE_CLOSE_SCOPE
302 
303 #endif // GLF_DRAW_TARGET_H
GLF_API void SetSize(GfVec2i)
Resize the DrawTarget.
GLF_API void Unbind()
Unbinds the framebuffer.
GLF_API bool IsValid(std::string *reason=NULL)
Returns whether the enclosed framebuffer object is complete.
GLF_API void Bind()
Binds the framebuffer.
GLF_API AttachmentsMap const & GetAttachments() const
Returns the list of Attachments for this DrawTarget.
GLF_API void DeleteAttachment(std::string const &name)
Removes the named attachment from the DrawTarget.
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
GLF_API void Resolve()
Resolve the MSAA framebuffer to a regular framebuffer.
bool HasMSAA() const
Returns if the draw target uses msaa.
Definition: drawTarget.h:200
static GLF_API GlfDrawTargetRefPtr New(GfVec2i const &size, bool requestMSAA=false)
Returns a new instance.
GLF_API void AddAttachment(std::string const &name, GLenum format, GLenum type, GLenum internalFormat)
Add an attachment to the DrawTarget.
A map with string keys and VtValue values.
Definition: dictionary.h:61
GLF_API bool IsBound() const
Returns whether the framebuffer is currently bound.
GLF_API GLuint GetFramebufferId() const
Returns the framebuffer object Id.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
A class representing a GL render target with mutliple image attachments.
Definition: drawTarget.h:68
GLF_API void CloneAttachments(GlfDrawTargetPtr const &drawtarget)
Copies the list of attachments from DrawTarget.
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:72
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
GLF_API void TouchContents()
Updates the contents signature for attached textures to allow downstream consumers to know that the t...
GLF_API GLuint GetFramebufferMSId() const
Returns the id of the framebuffer object with MSAA buffers.
Weak/Ref-based container for the the map of texture attachments.
Definition: drawTarget.h:249
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).
Represents a texture object in Glf.
Definition: texture.h:63
Reference-counted smart pointer utility class.
Definition: hash.h:45
GLF_API void ClearAttachments()
Clears all the attachments for this DrawTarget.
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:142
GfVec2i const & GetSize() const
Returns the size of the DrawTarget.
Definition: drawTarget.h:197
GLF_API AttachmentRefPtr GetAttachment(std::string const &name)
Returns the attachment with a given name or TfNullPtr;.