All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
texture.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_TEXTURE_H
25 #define GLF_TEXTURE_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/glf/api.h"
31 #include "pxr/imaging/glf/image.h"
32 #include "pxr/base/tf/declarePtrs.h"
33 #include "pxr/base/tf/refPtr.h"
34 #include "pxr/base/tf/staticTokens.h"
35 #include "pxr/base/tf/weakPtr.h"
36 #include "pxr/base/vt/dictionary.h"
37 
38 #include "pxr/imaging/garch/gl.h"
39 
40 #include <map>
41 #include <string>
42 #include <vector>
43 #include <boost/noncopyable.hpp>
44 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
47 
48 #define GLF_TEXTURE_TOKENS \
49  (texels) \
50  (layout)
51 
52 TF_DECLARE_PUBLIC_TOKENS(GlfTextureTokens, GLF_API, GLF_TEXTURE_TOKENS);
53 
55 
63 class GlfTexture : public TfRefBase, public TfWeakBase, boost::noncopyable {
64 public:
73  struct Binding {
74  Binding(TfToken name, TfToken role, GLenum target,
75  GLuint textureId, GLuint samplerId)
76  : name(name)
77  , role(role)
78  , target(target)
79  , textureId(textureId)
80  , samplerId(samplerId) { }
81 
82  TfToken name;
83  TfToken role;
84  GLenum target;
85  GLuint textureId;
86  GLuint samplerId;
87  };
88  typedef std::vector<Binding> BindingVector;
89 
90  GLF_API
91  virtual ~GlfTexture() = 0;
92 
96  virtual BindingVector GetBindings(TfToken const & identifier,
97  GLuint samplerId = 0) = 0;
98 
100  virtual GLuint GetGlTextureName() = 0;
101 
103  GLF_API
104  size_t GetMemoryUsed() const;
105 
107  GLF_API
108  size_t GetMemoryRequested() const;
109 
111  GLF_API
112  void SetMemoryRequested(size_t targetMemory);
113 
114  virtual VtDictionary GetTextureInfo(bool forceLoad) = 0;
115 
116  GLF_API
117  virtual bool IsMinFilterSupported(GLenum filter);
118 
119  GLF_API
120  virtual bool IsMagFilterSupported(GLenum filter);
121 
123  GLF_API
124  static size_t GetTextureMemoryAllocated();
125 
132  GLF_API
133  size_t GetContentsID() const;
134 
135  GLF_API
136  GlfImage::ImageOriginLocation GetOriginLocation() const;
137 
138  GLF_API
139  bool IsOriginLowerLeft() const;
140 
141 protected:
142  GLF_API
143  GlfTexture();
144 
145  GLF_API
147 
148  GLF_API
149  void _SetMemoryUsed(size_t size);
150 
151  GLF_API
152  virtual void _OnMemoryRequestedDirty();
153 
154  GLF_API
155  void _UpdateContentsID();
156 
157 private:
158  size_t _memoryUsed;
159  size_t _memoryRequested;
160  size_t _contentsID;
161  GlfImage::ImageOriginLocation _originLocation;
162 };
163 
164 class GlfTextureFactoryBase : public TfType::FactoryBase {
165 public:
166  virtual GlfTextureRefPtr New(const TfToken& texturePath,
167  GlfImage::ImageOriginLocation originLocation) const = 0;
168  virtual GlfTextureRefPtr New(const TfTokenVector& texturePaths,
169  GlfImage::ImageOriginLocation originLocation) const = 0;
170 };
171 
172 template <class T>
173 class GlfTextureFactory : public GlfTextureFactoryBase {
174 public:
175  virtual GlfTextureRefPtr New(const TfToken& texturePath,
176  GlfImage::ImageOriginLocation originLocation =
177  GlfImage::OriginUpperLeft) const
178  {
179  return T::New(texturePath);
180  }
181 
182  virtual GlfTextureRefPtr New(const TfTokenVector& texturePaths,
183  GlfImage::ImageOriginLocation originLocation =
184  GlfImage::OriginUpperLeft) const
185  {
186  return TfNullPtr;
187  }
188 };
189 
190 
191 PXR_NAMESPACE_CLOSE_SCOPE
192 
193 #endif // GLF_TEXTURE_H
GLF_API size_t GetMemoryRequested() const
Amount of memory the user wishes to allocate to the texture.
A map with string keys and VtValue values.
Definition: dictionary.h:61
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
A texture has one or more bindings which describe how the different aspects of the texture should be ...
Definition: texture.h:73
Base class of all factory types.
Definition: type.h:79
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
virtual GLuint GetGlTextureName()=0
Returns the OpenGl texture name for the texture.
GLF_API size_t GetContentsID() const
Returns an identifier that can be used to determine when the contents of this texture (i...
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
ImageOriginLocation
Specifies whether to treat the image origin as the upper-left corner or the lower left...
Definition: image.h:60
GLF_API size_t GetMemoryUsed() const
Amount of memory used to store the texture.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
static GLF_API size_t GetTextureMemoryAllocated()
static reporting function
virtual BindingVector GetBindings(TfToken const &identifier, GLuint samplerId=0)=0
Returns the bindings to use this texture for the shader resource named identifier.
Represents a texture object in Glf.
Definition: texture.h:63
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:142
GLF_API void SetMemoryRequested(size_t targetMemory)
Specify the amount of memory the user wishes to allocate to the texture.