hgi.h
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_HGI_H
8 #define PXR_IMAGING_HGI_HGI_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/base/tf/token.h"
12 #include "pxr/base/tf/type.h"
13 
14 #include "pxr/imaging/hgi/api.h"
15 #include "pxr/imaging/hgi/blitCmds.h"
16 #include "pxr/imaging/hgi/buffer.h"
17 #include "pxr/imaging/hgi/computeCmds.h"
18 #include "pxr/imaging/hgi/computeCmdsDesc.h"
19 #include "pxr/imaging/hgi/graphicsCmds.h"
20 #include "pxr/imaging/hgi/graphicsCmdsDesc.h"
21 #include "pxr/imaging/hgi/graphicsPipeline.h"
22 #include "pxr/imaging/hgi/resourceBindings.h"
23 #include "pxr/imaging/hgi/sampler.h"
24 #include "pxr/imaging/hgi/shaderFunction.h"
25 #include "pxr/imaging/hgi/shaderProgram.h"
26 #include "pxr/imaging/hgi/texture.h"
27 #include "pxr/imaging/hgi/types.h"
28 #include "pxr/imaging/hgi/version.h"
29 
30 #include <atomic>
31 #include <memory>
32 
33 PXR_NAMESPACE_OPEN_SCOPE
34 
35 class HgiCapabilities;
37 
38 using HgiUniquePtr = std::unique_ptr<class Hgi>;
39 
40 
94 class Hgi
95 {
96 public:
97  HGI_API
98  Hgi();
99 
100  HGI_API
101  virtual ~Hgi();
102 
111  HGI_API
112  void SubmitCmds(
113  HgiCmds* cmds,
114  HgiSubmitWaitType wait = HgiSubmitWaitTypeNoWait);
115 
117  HGI_API
118  static Hgi* GetPlatformDefaultHgi();
119 
125  HGI_API
126  static HgiUniquePtr CreatePlatformDefaultHgi();
127 
141  HGI_API
142  static HgiUniquePtr CreateNamedHgi(const TfToken& hgiToken);
143 
146  HGI_API
147  virtual bool IsBackendSupported() const = 0;
148 
158  HGI_API
159  static bool IsSupported(const TfToken& hgiToken = TfToken());
160 
167  HGI_API
168  virtual HgiGraphicsCmdsUniquePtr CreateGraphicsCmds(
169  HgiGraphicsCmdsDesc const& desc) = 0;
170 
177  HGI_API
178  virtual HgiBlitCmdsUniquePtr CreateBlitCmds() = 0;
179 
186  HGI_API
187  virtual HgiComputeCmdsUniquePtr CreateComputeCmds(
188  HgiComputeCmdsDesc const& desc) = 0;
189 
192  HGI_API
193  HgiTextureHandle CreateTexture(HgiTextureDesc const & desc);
194 
197  HGI_API
198  virtual void DestroyTexture(HgiTextureHandle* texHandle) = 0;
199 
205  HGI_API
207  HgiTextureViewDesc const & desc);
208 
213  HGI_API
214  virtual void DestroyTextureView(HgiTextureViewHandle* viewHandle) = 0;
215 
218  HGI_API
219  virtual HgiSamplerHandle CreateSampler(HgiSamplerDesc const & desc) = 0;
220 
223  HGI_API
224  virtual void DestroySampler(HgiSamplerHandle* smpHandle) = 0;
225 
228  HGI_API
230 
233  HGI_API
234  virtual void DestroyBuffer(HgiBufferHandle* bufHandle) = 0;
235 
238  HGI_API
240  HgiShaderFunctionDesc const& desc) = 0;
241 
244  HGI_API
245  virtual void DestroyShaderFunction(
246  HgiShaderFunctionHandle* shaderFunctionHandle) = 0;
247 
250  HGI_API
252  HgiShaderProgramDesc const& desc) = 0;
253 
258  HGI_API
259  virtual void DestroyShaderProgram(
260  HgiShaderProgramHandle* shaderProgramHandle) = 0;
261 
264  HGI_API
266  HgiResourceBindingsDesc const& desc);
267 
270  HGI_API
271  virtual void DestroyResourceBindings(
272  HgiResourceBindingsHandle* resHandle) = 0;
273 
276  HGI_API
278  HgiGraphicsPipelineDesc const& pipeDesc) = 0;
279 
282  HGI_API
283  virtual void DestroyGraphicsPipeline(
284  HgiGraphicsPipelineHandle* pipeHandle) = 0;
285 
288  HGI_API
290  HgiComputePipelineDesc const& pipeDesc) = 0;
291 
294  HGI_API
295  virtual void DestroyComputePipeline(HgiComputePipelineHandle* pipeHandle)=0;
296 
299  HGI_API
300  virtual TfToken const& GetAPIName() const = 0;
301 
304  HGI_API
305  virtual HgiCapabilities const* GetCapabilities() const = 0;
306 
310  HGI_API
312 
319  HGI_API
320  virtual void StartFrame() = 0;
321 
325  HGI_API
326  virtual void EndFrame() = 0;
327 
333  HGI_API
334  virtual void GarbageCollect() = 0;
335 
336 protected:
337  // Returns a unique id for handle creation.
338  // Thread safety: Thread-safe atomic increment.
339  HGI_API
340  uint64_t GetUniqueId();
341 
342  // Calls Submit on provided Cmds.
343  // Derived classes can override this function if they need customize the
344  // command submission. The default implementation calls cmds->_Submit().
345  HGI_API
346  virtual bool _SubmitCmds(
347  HgiCmds* cmds, HgiSubmitWaitType wait);
348 
349  HGI_API
350  virtual HgiTextureHandle _CreateTexture(HgiTextureDesc const & desc) = 0;
351 
352  HGI_API
353  virtual HgiTextureViewHandle _CreateTextureView(
354  HgiTextureViewDesc const & desc) = 0;
355 
356  HGI_API
357  virtual HgiBufferHandle _CreateBuffer(HgiBufferDesc const & desc) = 0;
358 
359  HGI_API
360  virtual HgiResourceBindingsHandle _CreateResourceBindings(
361  HgiResourceBindingsDesc const& desc) = 0;
362 
363 private:
364  Hgi & operator=(const Hgi&) = delete;
365  Hgi(const Hgi&) = delete;
366 
367  std::atomic<uint64_t> _uniqueIdCounter;
368 };
369 
370 
375 public:
376  virtual Hgi* New() const = 0;
377 };
378 
379 template <class T>
380 class HgiFactory : public HgiFactoryBase {
381 public:
382  Hgi* New() const {
383  return new T;
384  }
385 };
386 
387 
388 PXR_NAMESPACE_CLOSE_SCOPE
389 
390 #endif
virtual HGI_API void DestroyBuffer(HgiBufferHandle *bufHandle)=0
Destroy a buffer in rendering backend.
virtual HGI_API void EndFrame()=0
Optionally called at the end of a rendering frame.
virtual HGI_API HgiComputePipelineHandle CreateComputePipeline(HgiComputePipelineDesc const &pipeDesc)=0
Create a new compute pipeline state object.
virtual HGI_API void DestroyShaderFunction(HgiShaderFunctionHandle *shaderFunctionHandle)=0
Destroy a shader function.
static HGI_API bool IsSupported(const TfToken &hgiToken=TfToken())
Constructs a temporary Hgi object and calls the object's IsBackendSupported() function.
virtual HGI_API HgiShaderFunctionHandle CreateShaderFunction(HgiShaderFunctionDesc const &desc)=0
Create a new shader function.
virtual HGI_API void DestroyComputePipeline(HgiComputePipelineHandle *pipeHandle)=0
Destroy a compute pipeline state object.
Describes the properties needed to create a GPU texture view from an existing GPU texture object.
Definition: texture.h:228
virtual HGI_API void GarbageCollect()=0
Perform any necessary garbage collection, if applicable.
HGI_API HgiTextureViewHandle CreateTextureView(HgiTextureViewDesc const &desc)
Create a texture view in rendering backend.
The indirect command encoder is used to record the drawing primitives for a batch and capture the res...
virtual HGI_API HgiGraphicsCmdsUniquePtr CreateGraphicsCmds(HgiGraphicsCmdsDesc const &desc)=0
Returns a GraphicsCmds object (for temporary use) that is ready to record draw commands.
HGI_API void SubmitCmds(HgiCmds *cmds, HgiSubmitWaitType wait=HgiSubmitWaitTypeNoWait)
Submit one HgiCmds objects.
Describes the properties needed to create a GPU sampler.
Definition: sampler.h:61
Describes the properties needed to create a GPU texture.
Definition: texture.h:90
Base class of all factory types.
Definition: type.h:56
static HGI_API HgiUniquePtr CreateNamedHgi(const TfToken &hgiToken)
Helper function to return a Hgi object of choice supported by current platform and build configuratio...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:80
virtual HGI_API void DestroyResourceBindings(HgiResourceBindingsHandle *resHandle)=0
Destroy a resource binding object.
virtual HGI_API HgiGraphicsPipelineHandle CreateGraphicsPipeline(HgiGraphicsPipelineDesc const &pipeDesc)=0
Create a new graphics pipeline state object.
HGI_API HgiBufferHandle CreateBuffer(HgiBufferDesc const &desc)
Create a buffer in rendering backend.
Reports the capabilities of the Hgi device.
Definition: capabilities.h:22
Describes the properties needed to create a GPU compute pipeline.
Describes the properties needed to create a GPU buffer.
Definition: buffer.h:45
Describes the properties to begin a HgiGraphicsCmds.
virtual HGI_API bool IsBackendSupported() const =0
Determine if Hgi instance can run on current hardware.
Hgi factory for plugin system.
Definition: hgi.h:374
virtual HGI_API void DestroyTextureView(HgiTextureViewHandle *viewHandle)=0
Destroy a texture view in rendering backend.
Describes the properties needed to create a GPU shader program.
Definition: shaderProgram.h:34
Describes a set of resources that are bound to the GPU during encoding.
Hydra Graphics Interface.
Definition: hgi.h:94
virtual HGI_API void DestroyTexture(HgiTextureHandle *texHandle)=0
Destroy a texture in rendering backend.
static HGI_API HgiUniquePtr CreatePlatformDefaultHgi()
Helper function to return a Hgi object for the current platform.
virtual HGI_API HgiShaderProgramHandle CreateShaderProgram(HgiShaderProgramDesc const &desc)=0
Create a new shader program.
static HGI_API Hgi * GetPlatformDefaultHgi()
*** DEPRECATED *** Please use: CreatePlatformDefaultHgi
HGI_API HgiResourceBindingsHandle CreateResourceBindings(HgiResourceBindingsDesc const &desc)
Create a new resource binding object.
virtual HGI_API HgiIndirectCommandEncoder * GetIndirectCommandEncoder() const =0
Returns the device-specific indirect command buffer encoder or nullptr if not supported.
Describes the properties needed to create a GPU shader function.
virtual HGI_API HgiSamplerHandle CreateSampler(HgiSamplerDesc const &desc)=0
Create a sampler in rendering backend.
Describes the properties to construct a HgiComputeCmds.
virtual HGI_API HgiCapabilities const * GetCapabilities() const =0
Returns the device-specific capabilities structure.
virtual HGI_API void DestroySampler(HgiSamplerHandle *smpHandle)=0
Destroy a sampler in rendering backend.
virtual HGI_API void StartFrame()=0
Optionally called by client app at the start of a new rendering frame.
virtual HGI_API HgiComputeCmdsUniquePtr CreateComputeCmds(HgiComputeCmdsDesc const &desc)=0
Returns a ComputeCmds object (for temporary use) that is ready to record dispatch commands.
Describes the properties needed to create a GPU pipeline.
HGI_API HgiTextureHandle CreateTexture(HgiTextureDesc const &desc)
Create a texture in rendering backend.
Graphics commands are recorded in 'cmds' objects which are later submitted to hgi.
Definition: cmds.h:28
virtual HGI_API void DestroyShaderProgram(HgiShaderProgramHandle *shaderProgramHandle)=0
Destroy a shader program.
virtual HGI_API HgiBlitCmdsUniquePtr CreateBlitCmds()=0
Returns a BlitCmds object (for temporary use) that is ready to execute resource copy commands.
virtual HGI_API TfToken const & GetAPIName() const =0
Return the name of the api (e.g.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
virtual HGI_API void DestroyGraphicsPipeline(HgiGraphicsPipelineHandle *pipeHandle)=0
Destroy a graphics pipeline state object.