HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buffer.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 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_BUFFER_H
8 #define PXR_IMAGING_HGI_BUFFER_H
9 
10 #include <string>
11 #include <vector>
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/vec3i.h"
15 #include "pxr/imaging/hgi/api.h"
16 #include "pxr/imaging/hgi/enums.h"
17 #include "pxr/imaging/hgi/handle.h"
18 #include "pxr/imaging/hgi/types.h"
19 
20 
22 
23 
24 /// \struct HgiBufferDesc
25 ///
26 /// Describes the properties needed to create a GPU buffer.
27 ///
28 /// <ul>
29 /// <li>debugName:
30 /// This label can be applied as debug label for gpu debugging.</li>
31 /// <li>usage:
32 /// Bits describing the intended usage and properties of the buffer.</li>
33 /// <li>byteSize:
34 /// Length of buffer in bytes</li>
35 /// <Li>vertexStride:
36 /// The size of a vertex in a vertex buffer.
37 /// This property is only required for vertex buffers.</li>
38 /// <li>initialData:
39 /// CPU pointer to initialization data of buffer.
40 /// The memory is consumed immediately during the creation of the HgiBuffer.
41 /// The application may alter or free this memory as soon as the constructor
42 /// of the HgiBuffer has returned.</li>
43 /// </ul>
44 ///
46 {
47  HGI_API
50  , byteSize(0)
51  , vertexStride(0)
52  , initialData(nullptr)
53  {}
54 
55  std::string debugName;
57  size_t byteSize;
58  uint32_t vertexStride;
59  void const* initialData;
60 };
61 
62 HGI_API
63 bool operator==(
64  const HgiBufferDesc& lhs,
65  const HgiBufferDesc& rhs);
66 
67 HGI_API
68 inline bool operator!=(
69  const HgiBufferDesc& lhs,
70  const HgiBufferDesc& rhs);
71 
72 
73 ///
74 /// \class HgiBuffer
75 ///
76 /// Represents a graphics platform independent GPU buffer resource (base class).
77 /// Buffers should be created via Hgi::CreateBuffer.
78 /// The fill the buffer with data you supply `initialData` in the descriptor.
79 /// To update the data inside the buffer later on, use blitCmds.
80 ///
81 class HgiBuffer
82 {
83 public:
84  HGI_API
85  virtual ~HgiBuffer();
86 
87  /// The descriptor describes the object.
88  HGI_API
89  HgiBufferDesc const& GetDescriptor() const;
90 
91  /// Returns the byte size of the GPU buffer.
92  /// This can be helpful if the application wishes to tally up memory usage.
93  HGI_API
94  virtual size_t GetByteSizeOfResource() const = 0;
95 
96  /// This function returns the handle to the Hgi backend's gpu resource, cast
97  /// to a uint64_t. Clients should avoid using this function and instead
98  /// use Hgi base classes so that client code works with any Hgi platform.
99  /// For transitioning code to Hgi, it can however we useful to directly
100  /// access a platform's internal resource handles.
101  /// There is no safety provided in using this. If you by accident pass a
102  /// HgiMetal resource into an OpenGL call, bad things may happen.
103  /// In OpenGL this returns the GLuint resource name.
104  /// In Metal this returns the id<MTLBuffer> as uint64_t.
105  /// In Vulkan this returns the VkBuffer as uint64_t.
106  /// In DX12 this returns the ID3D12Resource pointer as uint64_t.
107  HGI_API
108  virtual uint64_t GetRawResource() const = 0;
109 
110  /// Returns the 'staging area' in which new buffer data is copied before
111  /// it is flushed to GPU.
112  /// Some implementations (e.g. Metal) may have build in support for
113  /// queueing up CPU->GPU copies. Those implementations can return the
114  /// CPU pointer to the buffer's content directly.
115  /// The caller should not assume that the data from the CPU staging area
116  /// is automatically flushed to the GPU. Instead, after copying is finished,
117  /// the caller should use BlitCmds CopyBufferCpuToGpu to ensure the transfer
118  /// from the staging area to the GPU is scheduled.
119  HGI_API
120  virtual void* GetCPUStagingAddress() = 0;
121 
122 protected:
123  HGI_API
124  HgiBuffer(HgiBufferDesc const& desc);
125 
127 
128 private:
129  HgiBuffer() = delete;
130  HgiBuffer & operator=(const HgiBuffer&) = delete;
131  HgiBuffer(const HgiBuffer&) = delete;
132 };
133 
135 using HgiBufferHandleVector = std::vector<HgiBufferHandle>;
136 
137 
139 
140 #endif
HgiBufferDesc _descriptor
Definition: buffer.h:126
virtual HGI_API void * GetCPUStagingAddress()=0
HGI_API HgiBufferDesc const & GetDescriptor() const
The descriptor describes the object.
std::string debugName
Definition: buffer.h:55
virtual HGI_API size_t GetByteSizeOfResource() const =0
HgiBufferUsage usage
Definition: buffer.h:56
uint32_t vertexStride
Definition: buffer.h:58
virtual HGI_API ~HgiBuffer()
std::vector< HgiBufferHandle > HgiBufferHandleVector
Definition: buffer.h:135
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
virtual HGI_API uint64_t GetRawResource() const =0
size_t byteSize
Definition: buffer.h:57
#define HGI_API
Definition: api.h:23
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:664
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HgiBits HgiBufferUsage
Definition: enums.h:315
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HGI_API HgiBufferDesc()
Definition: buffer.h:48
void const * initialData
Definition: buffer.h:59
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542