Loading...
Searching...
No Matches
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
11
12#include "pxr/pxr.h"
13#include "pxr/imaging/glf/api.h"
15#include "pxr/imaging/garch/glApi.h"
16
17#include "pxr/base/gf/vec2i.h"
20#include "pxr/base/tf/refBase.h"
22
23#include <map>
24#include <memory>
25#include <set>
26#include <string>
27
28
29PXR_NAMESPACE_OPEN_SCOPE
30
31
33typedef std::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
34
50class GlfDrawTarget : public TfRefBase, public TfWeakBase {
51public:
52 typedef GlfDrawTarget This;
53
54public:
55
57 GLF_API
58 static GlfDrawTargetRefPtr New( GfVec2i const & size,
59 bool requestMSAA = false );
60
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,
78 GLenum internalFormat, GfVec2i size,
79 unsigned int numSamples);
80
81 GLF_API
82 ~Attachment() override;
83
85 GLuint GetGlTextureName() override;
86
88 GLuint GetGlTextureMSName() const { return _textureNameMS; }
89
91 GLenum GetFormat() const { return _format; }
92
94 GLenum GetType() const { return _type; }
95
97 GLenum GetInternalFormat() const { return _internalFormat; }
98
100 int GetAttach() const { return _glIndex; }
101
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
116 GLF_API
117 void TouchContents();
118
119 private:
120 Attachment(int glIndex, GLenum format, GLenum type,
121 GLenum internalFormat, GfVec2i size,
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
141 typedef TfDeclarePtrs<class Attachment>::RefPtr AttachmentRefPtr;
142
143 typedef std::map<std::string, AttachmentRefPtr> AttachmentsMap;
144
146 GLF_API
147 void AddAttachment( std::string const & name,
148 GLenum format, GLenum type, GLenum internalFormat );
149
151 GLF_API
152 void DeleteAttachment( std::string const & name );
153
155 GLF_API
157
159 GLF_API
160 void CloneAttachments( GlfDrawTargetPtr const & drawtarget );
161
163 GLF_API
164 AttachmentsMap const & GetAttachments() const;
165
167 GLF_API
168 AttachmentRefPtr GetAttachment(std::string const & name);
169
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
178 GLF_API
179 void SetSize( GfVec2i );
180
182 GfVec2i const & GetSize() const { return _size; }
183
185 bool HasMSAA() const { return (_numSamples > 1); }
186
188 GLF_API
189 GLuint GetFramebufferId() const;
190
192 GLF_API
193 GLuint GetFramebufferMSId() const;
194
196 GLF_API
197 void Bind();
198
200 GLF_API
201 void Unbind();
202
204 GLF_API
205 bool IsBound() const;
206
209 GLF_API
210 void Resolve();
211
214 GLF_API
215 static void Resolve(const std::vector<GlfDrawTarget*>& drawTargets);
216
220 GLF_API
222
226 GLF_API
227 bool IsValid(std::string * reason = NULL);
228
229protected:
230
235 public:
236 AttachmentsMap attachments;
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
248private:
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
286PXR_NAMESPACE_CLOSE_SCOPE
287
288#endif // GLF_DRAW_TARGET_H
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:71
Basic type for a vector of 2 int components.
Definition: vec2i.h:44
Weak/Ref-based container for the the map of texture attachments.
Definition: drawTarget.h:234
A class representing a GL render target with mutliple image attachments.
Definition: drawTarget.h:50
GLF_API AttachmentsMap const & GetAttachments() const
Returns the list of Attachments for this DrawTarget.
GLF_API void AddAttachment(std::string const &name, GLenum format, GLenum type, GLenum internalFormat)
Add an attachment to the DrawTarget.
GLF_API bool IsBound() const
Returns whether the framebuffer is currently bound.
static GLF_API GlfDrawTargetRefPtr New(GlfDrawTargetPtr const &drawtarget)
Returns a new instance.
static GLF_API void Resolve(const std::vector< GlfDrawTarget * > &drawTargets)
Resolve several MSAA framebuffers at once.
GLF_API AttachmentRefPtr GetAttachment(std::string const &name)
Returns the attachment with a given name or TfNullPtr;.
GLF_API void ClearAttachments()
Clears all the attachments for this DrawTarget.
GLF_API bool IsValid(std::string *reason=NULL)
Returns whether the enclosed framebuffer object is complete.
GLF_API GLuint GetFramebufferId() const
Returns the framebuffer object Id.
GLF_API void DeleteAttachment(std::string const &name)
Removes the named attachment from the DrawTarget.
GLF_API void CloneAttachments(GlfDrawTargetPtr const &drawtarget)
Copies the list of attachments from DrawTarget.
GLF_API GLuint GetFramebufferMSId() const
Returns the id of the framebuffer object with MSAA buffers.
GLF_API void TouchContents()
Updates the contents signature for attached textures to allow downstream consumers to know that the t...
GfVec2i const & GetSize() const
Returns the size of the DrawTarget.
Definition: drawTarget.h:182
GLF_API void Bind()
Binds the framebuffer.
GLF_API void SetSize(GfVec2i)
Resize the DrawTarget.
static GLF_API GlfDrawTargetRefPtr New(GfVec2i const &size, bool requestMSAA=false)
Returns a new instance.
bool HasMSAA() const
Returns if the draw target uses msaa.
Definition: drawTarget.h:185
GLF_API void Unbind()
Unbinds the framebuffer.
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).
GLF_API void Resolve()
Resolve the MSAA framebuffer to a regular framebuffer.
Represents a texture object in Glf.
Definition: texture.h:46
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:56
Reference-counted smart pointer utility class.
Definition: refPtr.h:590
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
A map with string keys and VtValue values.
Definition: dictionary.h:43
Standard pointer typedefs.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:72