HDK
 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 2019 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_HGI_TEXTURE_H
8 #define PXR_IMAGING_HGI_TEXTURE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/base/gf/vec3i.h"
12 #include "pxr/imaging/hgi/api.h"
13 #include "pxr/imaging/hgi/enums.h"
14 #include "pxr/imaging/hgi/handle.h"
15 #include "pxr/imaging/hgi/types.h"
16 
17 #include <string>
18 #include <vector>
19 
21 
22 /// \struct HgiComponentMapping
23 ///
24 /// Describes color component mapping.
25 ///
26 /// <ul>
27 /// <li>r:
28 /// What component is used for red channel.
29 /// <li>g:
30 /// What component is used for green channel.
31 /// <li>b:
32 /// What component is used for blue channel.
33 /// <li>a:
34 /// What component is used for alpha channel.
35 /// </ul>
36 ///
38 {
43 };
44 
45 HGI_API
46 bool operator==(
47  const HgiComponentMapping& lhs,
48  const HgiComponentMapping& rhs);
49 
50 HGI_API
51 bool operator!=(
52  const HgiComponentMapping& lhs,
53  const HgiComponentMapping& rhs);
54 
55 /// \struct HgiTextureDesc
56 ///
57 /// Describes the properties needed to create a GPU texture.
58 ///
59 /// <ul>
60 /// <li>debugName:
61 /// This label can be applied as debug label for GPU debugging.</li>
62 /// <li>usage:
63 /// Describes how the texture is intended to be used.</li>
64 /// <li>format:
65 /// The format of the texture.
66 /// <li>componentMapping:
67 /// The mapping of rgba components when accessing the texture.</li>
68 /// <li>dimensions:
69 /// The resolution of the texture (width, height, depth).</li>
70 /// <li>type:
71 /// Type of texture (2D, 3D).</li>
72 /// <li>layerCount:
73 /// The number of layers (texture-arrays).</li>
74 /// <li>mipLevels:
75 /// The number of mips in texture.</li>
76 /// <li>sampleCount:
77 /// samples per texel (multi-sampling).</li>
78 /// <li>pixelsByteSize:
79 /// Byte size (length) of pixel data (i.e., initialData).</li>
80 /// <li>initialData:
81 /// CPU pointer to initialization pixels of the texture.
82 /// The memory is consumed immediately during the creation of the HgiTexture.
83 /// The application may alter or free this memory as soon as the constructor
84 /// of the HgiTexture has returned.
85 /// Data may optionally include pixels for each mip-level.
86 /// HgiGetMipInitialData can be used to get to each mip's data and describes
87 /// in more detail how mip dimensions are rounded.</li>
88 /// </ul>
89 ///
91 {
93  : usage(0)
101  , dimensions(0)
102  , layerCount(1)
103  , mipLevels(1)
105  , pixelsByteSize(0)
106  , initialData(nullptr)
107  {}
108 
109  std::string debugName;
115  uint16_t layerCount;
116  uint16_t mipLevels;
119  void const* initialData;
120 };
121 
122 HGI_API
123 bool operator==(
124  const HgiTextureDesc& lhs,
125  const HgiTextureDesc& rhs);
126 
127 HGI_API
128 bool operator!=(
129  const HgiTextureDesc& lhs,
130  const HgiTextureDesc& rhs);
131 
132 
133 ///
134 /// \class HgiTexture
135 ///
136 /// Represents a graphics platform independent GPU texture resource.
137 /// Textures should be created via Hgi::CreateTexture.
138 ///
139 /// Base class for Hgi textures.
140 /// To the client (HdSt) texture resources are referred to via
141 /// opaque, stateless handles (HgTextureHandle).
142 ///
144 {
145 public:
146  HGI_API
147  virtual ~HgiTexture();
148 
149  /// The descriptor describes the object.
150  HGI_API
151  HgiTextureDesc const& GetDescriptor() const;
152 
153  /// Returns the byte size of the GPU texture.
154  /// This can be helpful if the application wishes to tally up memory usage.
155  HGI_API
156  virtual size_t GetByteSizeOfResource() const = 0;
157 
158  /// This function returns the handle to the Hgi backend's gpu resource, cast
159  /// to a uint64_t. Clients should avoid using this function and instead
160  /// use Hgi base classes so that client code works with any Hgi platform.
161  /// For transitioning code to Hgi, it can however we useful to directly
162  /// access a platform's internal resource handles.
163  /// There is no safety provided in using this. If you by accident pass a
164  /// HgiMetal resource into an OpenGL call, bad things may happen.
165  /// In OpenGL this returns the GLuint resource name.
166  /// In Metal this returns the id<MTLTexture> as uint64_t.
167  /// In Vulkan this returns the VkImage as uint64_t.
168  /// In DX12 this returns the ID3D12Resource pointer as uint64_t.
169  HGI_API
170  virtual uint64_t GetRawResource() const = 0;
171 
172  /// This function initiates a layout change process on this texture
173  /// resource. This feature is at the moment required explicitly by explicit
174  /// APIs like Vulkan. Returns the previous layout to make it easier to
175  /// restore it correctly.
176  HGI_API
177  virtual HgiTextureUsage SubmitLayoutChange(HgiTextureUsage newLayout) = 0;
178 
179 protected:
180  HGI_API
181  static
182  size_t _GetByteSizeOfResource(const HgiTextureDesc &descriptor);
183 
184  HGI_API
185  HgiTexture(HgiTextureDesc const& desc);
186 
188 
189 private:
190  HgiTexture() = delete;
191  HgiTexture & operator=(const HgiTexture&) = delete;
192  HgiTexture(const HgiTexture&) = delete;
193 };
194 
195 
197 using HgiTextureHandleVector = std::vector<HgiTextureHandle>;
198 
199 
200 /// \struct HgiTextureViewDesc
201 ///
202 /// Describes the properties needed to create a GPU texture view from an
203 /// existing GPU texture object.
204 ///
205 /// <ul>
206 /// <li>debugName:
207 /// This label can be applied as debug label for GPU debugging.</li>
208 /// <li>format:
209 /// The format of the texture view. This format must be compatible with
210 /// the sourceTexture, but does not have to be the identical format.
211 /// Generally: All 8-, 16-, 32-, 64-, and 128-bit color formats are
212 /// compatible with other formats with the same bit length.
213 /// For example HgiFormatFloat32Vec4 and HgiFormatInt32Vec4 are compatible.
214 /// <li>layerCount:
215 /// The number of layers (texture-arrays).</li>
216 /// <li>mipLevels:
217 /// The number of mips in texture.</li>
218 /// <li>sourceTexture:
219 /// Handle to the HgiTexture to be used as the source data backing.</li>
220 /// <li>sourceFirstLayer:
221 /// The layer index to use from the source texture as the first layer of the
222 /// view.</li>
223 /// <li>sourceFirstMip:
224 /// The mip index to ues from the source texture as the first mip of the
225 /// view.</li>
226 /// </ul>
227 ///
229 {
232  , layerCount(1)
233  , mipLevels(1)
234  , sourceTexture()
235  , sourceFirstLayer(0)
236  , sourceFirstMip(0)
237  {}
238 
239  std::string debugName;
241  uint16_t layerCount;
242  uint16_t mipLevels;
245  uint16_t sourceFirstMip;
246 };
247 
248 HGI_API
249 bool operator==(
250  const HgiTextureViewDesc& lhs,
251  const HgiTextureViewDesc& rhs);
252 
253 HGI_API
254 bool operator!=(
255  const HgiTextureViewDesc& lhs,
256  const HgiTextureViewDesc& rhs);
257 
258 ///
259 /// \class HgiTextureView
260 ///
261 /// Represents a graphics platform independent GPU texture view resource.
262 /// Texture Views should be created via Hgi::CreateTextureView.
263 ///
264 /// A TextureView aliases the data of another texture and is a thin wrapper
265 /// around a HgiTextureHandle. The embeded texture handle is used to
266 /// add the texture to resource bindings for use in shaders.
267 ///
268 /// For example when using a compute shader to fill the mip levels of a
269 /// texture, like a lightDome texture, we can use a texture view to give the
270 /// shader access to a specific mip level of a sourceTexture via a TextureView.
271 ///
272 /// Another example is to conserve resources by reusing a RGBAF32 texture as
273 /// a RGBAI32 texture once the F32 texture is no longer needed
274 /// (transient resources).
275 ///
277 {
278 public:
279  HGI_API
280  HgiTextureView(HgiTextureViewDesc const& desc);
281 
282  HGI_API
283  virtual ~HgiTextureView();
284 
285  /// Set the handle to the texture that aliases another texture.
286  HGI_API
287  void SetViewTexture(HgiTextureHandle const& handle);
288 
289  /// Returns the handle to the texture that aliases another texture.
290  HGI_API
291  HgiTextureHandle const& GetViewTexture() const;
292 
293 protected:
295 
296 private:
297  HgiTextureView() = delete;
298  HgiTextureView & operator=(const HgiTextureView&) = delete;
299  HgiTextureView(const HgiTextureView&) = delete;
300 };
301 
303 using HgiTextureViewHandleVector = std::vector<HgiTextureViewHandle>;
304 
306 
307 #endif
HgiComponentSwizzle a
Definition: texture.h:42
HgiTextureHandle sourceTexture
Definition: texture.h:243
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
std::string debugName
Definition: texture.h:109
std::string debugName
Definition: texture.h:239
virtual HGI_API size_t GetByteSizeOfResource() const =0
HgiTextureHandle _viewTexture
Definition: texture.h:294
uint16_t layerCount
Definition: texture.h:241
HgiTextureDesc _descriptor
Definition: texture.h:187
size_t pixelsByteSize
Definition: texture.h:118
int HgiHandle< class HgiTexture > HgiTextureHandle
static HGI_API size_t _GetByteSizeOfResource(const HgiTextureDesc &descriptor)
std::vector< HgiTextureHandle > HgiTextureHandleVector
Definition: texture.h:197
HgiFormat
Definition: types.h:28
HgiFormat format
Definition: texture.h:240
virtual HGI_API HgiTextureUsage SubmitLayoutChange(HgiTextureUsage newLayout)=0
HgiBits HgiTextureUsage
Definition: enums.h:157
HgiComponentSwizzle
Definition: enums.h:570
HgiComponentMapping componentMapping
Definition: texture.h:112
HgiSampleCount sampleCount
Definition: texture.h:117
uint16_t mipLevels
Definition: texture.h:242
virtual HGI_API uint64_t GetRawResource() const =0
HGI_API HgiTextureDesc const & GetDescriptor() const
The descriptor describes the object.
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
virtual HGI_API ~HgiTexture()
uint16_t sourceFirstMip
Definition: texture.h:245
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
uint16_t sourceFirstLayer
Definition: texture.h:244
Definition: vec3i.h:43
HGI_API void SetViewTexture(HgiTextureHandle const &handle)
Set the handle to the texture that aliases another texture.
HgiTextureType
Definition: enums.h:110
HgiFormat format
Definition: texture.h:111
#define HGI_API
Definition: api.h:23
std::vector< HgiTextureViewHandle > HgiTextureViewHandleVector
Definition: texture.h:303
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:664
HgiTextureDesc()
Definition: texture.h:92
HgiComponentSwizzle r
Definition: texture.h:39
HgiTextureUsage usage
Definition: texture.h:110
GfVec3i dimensions
Definition: texture.h:114
HgiComponentSwizzle b
Definition: texture.h:41
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
uint16_t layerCount
Definition: texture.h:115
void const * initialData
Definition: texture.h:119
virtual HGI_API ~HgiTextureView()
HGI_API HgiTextureHandle const & GetViewTexture() const
Returns the handle to the texture that aliases another texture.
HgiSampleCount
Definition: enums.h:238
uint16_t mipLevels
Definition: texture.h:116
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542
HgiComponentSwizzle g
Definition: texture.h:40
HgiTextureType type
Definition: texture.h:113