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.
175  HGI_API
176  virtual void SubmitLayoutChange(HgiTextureUsage newLayout) = 0;
177 
178 protected:
179  HGI_API
180  static
181  size_t _GetByteSizeOfResource(const HgiTextureDesc &descriptor);
182 
183  HGI_API
184  HgiTexture(HgiTextureDesc const& desc);
185 
187 
188 private:
189  HgiTexture() = delete;
190  HgiTexture & operator=(const HgiTexture&) = delete;
191  HgiTexture(const HgiTexture&) = delete;
192 };
193 
194 
196 using HgiTextureHandleVector = std::vector<HgiTextureHandle>;
197 
198 
199 /// \struct HgiTextureViewDesc
200 ///
201 /// Describes the properties needed to create a GPU texture view from an
202 /// existing GPU texture object.
203 ///
204 /// <ul>
205 /// <li>debugName:
206 /// This label can be applied as debug label for GPU debugging.</li>
207 /// <li>format:
208 /// The format of the texture view. This format must be compatible with
209 /// the sourceTexture, but does not have to be the identical format.
210 /// Generally: All 8-, 16-, 32-, 64-, and 128-bit color formats are
211 /// compatible with other formats with the same bit length.
212 /// For example HgiFormatFloat32Vec4 and HgiFormatInt32Vec4 are compatible.
213 /// <li>layerCount:
214 /// The number of layers (texture-arrays).</li>
215 /// <li>mipLevels:
216 /// The number of mips in texture.</li>
217 /// <li>sourceTexture:
218 /// Handle to the HgiTexture to be used as the source data backing.</li>
219 /// <li>sourceFirstLayer:
220 /// The layer index to use from the source texture as the first layer of the
221 /// view.</li>
222 /// <li>sourceFirstMip:
223 /// The mip index to ues from the source texture as the first mip of the
224 /// view.</li>
225 /// </ul>
226 ///
228 {
231  , layerCount(1)
232  , mipLevels(1)
233  , sourceTexture()
234  , sourceFirstLayer(0)
235  , sourceFirstMip(0)
236  {}
237 
238  std::string debugName;
240  uint16_t layerCount;
241  uint16_t mipLevels;
244  uint16_t sourceFirstMip;
245 };
246 
247 HGI_API
248 bool operator==(
249  const HgiTextureViewDesc& lhs,
250  const HgiTextureViewDesc& rhs);
251 
252 HGI_API
253 bool operator!=(
254  const HgiTextureViewDesc& lhs,
255  const HgiTextureViewDesc& rhs);
256 
257 ///
258 /// \class HgiTextureView
259 ///
260 /// Represents a graphics platform independent GPU texture view resource.
261 /// Texture Views should be created via Hgi::CreateTextureView.
262 ///
263 /// A TextureView aliases the data of another texture and is a thin wrapper
264 /// around a HgiTextureHandle. The embeded texture handle is used to
265 /// add the texture to resource bindings for use in shaders.
266 ///
267 /// For example when using a compute shader to fill the mip levels of a
268 /// texture, like a lightDome texture, we can use a texture view to give the
269 /// shader access to a specific mip level of a sourceTexture via a TextureView.
270 ///
271 /// Another example is to conserve resources by reusing a RGBAF32 texture as
272 /// a RGBAI32 texture once the F32 texture is no longer needed
273 /// (transient resources).
274 ///
276 {
277 public:
278  HGI_API
279  HgiTextureView(HgiTextureViewDesc const& desc);
280 
281  HGI_API
282  virtual ~HgiTextureView();
283 
284  /// Set the handle to the texture that aliases another texture.
285  HGI_API
286  void SetViewTexture(HgiTextureHandle const& handle);
287 
288  /// Returns the handle to the texture that aliases another texture.
289  HGI_API
290  HgiTextureHandle const& GetViewTexture() const;
291 
292 protected:
294 
295 private:
296  HgiTextureView() = delete;
297  HgiTextureView & operator=(const HgiTextureView&) = delete;
298  HgiTextureView(const HgiTextureView&) = delete;
299 };
300 
302 using HgiTextureViewHandleVector = std::vector<HgiTextureViewHandle>;
303 
305 
306 #endif
virtual HGI_API void SubmitLayoutChange(HgiTextureUsage newLayout)=0
HgiComponentSwizzle a
Definition: texture.h:42
HgiTextureHandle sourceTexture
Definition: texture.h:242
std::string debugName
Definition: texture.h:109
std::string debugName
Definition: texture.h:238
virtual HGI_API size_t GetByteSizeOfResource() const =0
HgiTextureHandle _viewTexture
Definition: texture.h:293
uint16_t layerCount
Definition: texture.h:240
HgiTextureDesc _descriptor
Definition: texture.h:186
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:196
HgiFormat
Definition: types.h:28
HgiFormat format
Definition: texture.h:239
HgiBits HgiTextureUsage
Definition: enums.h:153
HgiComponentSwizzle
Definition: enums.h:563
HgiComponentMapping componentMapping
Definition: texture.h:112
HgiSampleCount sampleCount
Definition: texture.h:117
uint16_t mipLevels
Definition: texture.h:241
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:244
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:243
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:107
HgiFormat format
Definition: texture.h:111
#define HGI_API
Definition: api.h:23
std::vector< HgiTextureViewHandle > HgiTextureViewHandleVector
Definition: texture.h:302
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:664
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
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:234
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