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