Loading...
Searching...
No Matches
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
28
29#include "pxr/pxr.h"
30#include "pxr/imaging/glf/api.h"
32#include "pxr/imaging/garch/glApi.h"
33
34#include "pxr/base/gf/vec2i.h"
37#include "pxr/base/tf/refBase.h"
39
40#include <map>
41#include <memory>
42#include <set>
43#include <string>
44
45
46PXR_NAMESPACE_OPEN_SCOPE
47
48
50typedef std::shared_ptr<class GlfGLContext> GlfGLContextSharedPtr;
51
67class GlfDrawTarget : public TfRefBase, public TfWeakBase {
68public:
69 typedef GlfDrawTarget This;
70
71public:
72
74 GLF_API
75 static GlfDrawTargetRefPtr New( GfVec2i const & size,
76 bool requestMSAA = false );
77
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,
95 GLenum internalFormat, GfVec2i size,
96 unsigned int numSamples);
97
98 GLF_API
99 ~Attachment() override;
100
102 GLuint GetGlTextureName() override;
103
105 GLuint GetGlTextureMSName() const { return _textureNameMS; }
106
108 GLenum GetFormat() const { return _format; }
109
111 GLenum GetType() const { return _type; }
112
114 GLenum GetInternalFormat() const { return _internalFormat; }
115
117 int GetAttach() const { return _glIndex; }
118
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
133 GLF_API
134 void TouchContents();
135
136 private:
137 Attachment(int glIndex, GLenum format, GLenum type,
138 GLenum internalFormat, GfVec2i size,
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
158 typedef TfDeclarePtrs<class Attachment>::RefPtr AttachmentRefPtr;
159
160 typedef std::map<std::string, AttachmentRefPtr> AttachmentsMap;
161
163 GLF_API
164 void AddAttachment( std::string const & name,
165 GLenum format, GLenum type, GLenum internalFormat );
166
168 GLF_API
169 void DeleteAttachment( std::string const & name );
170
172 GLF_API
174
176 GLF_API
177 void CloneAttachments( GlfDrawTargetPtr const & drawtarget );
178
180 GLF_API
181 AttachmentsMap const & GetAttachments() const;
182
184 GLF_API
185 AttachmentRefPtr GetAttachment(std::string const & name);
186
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
195 GLF_API
196 void SetSize( GfVec2i );
197
199 GfVec2i const & GetSize() const { return _size; }
200
202 bool HasMSAA() const { return (_numSamples > 1); }
203
205 GLF_API
206 GLuint GetFramebufferId() const;
207
209 GLF_API
210 GLuint GetFramebufferMSId() const;
211
213 GLF_API
214 void Bind();
215
217 GLF_API
218 void Unbind();
219
221 GLF_API
222 bool IsBound() const;
223
226 GLF_API
227 void Resolve();
228
231 GLF_API
232 static void Resolve(const std::vector<GlfDrawTarget*>& drawTargets);
233
237 GLF_API
239
243 GLF_API
244 bool IsValid(std::string * reason = NULL);
245
246protected:
247
252 public:
253 AttachmentsMap attachments;
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
265private:
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
303PXR_NAMESPACE_CLOSE_SCOPE
304
305#endif // GLF_DRAW_TARGET_H
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
Weak/Ref-based container for the the map of texture attachments.
Definition: drawTarget.h:251
A class representing a GL render target with mutliple image attachments.
Definition: drawTarget.h:67
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:199
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:202
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:63
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:73
Reference-counted smart pointer utility class.
Definition: refPtr.h:601
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141
A map with string keys and VtValue values.
Definition: dictionary.h:60
Standard pointer typedefs.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89