hgi.h
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_HGI_H
25 #define PXR_IMAGING_HGI_HGI_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/tf/token.h"
29 #include "pxr/base/tf/type.h"
30 
31 #include "pxr/imaging/hgi/api.h"
32 #include "pxr/imaging/hgi/blitCmds.h"
33 #include "pxr/imaging/hgi/buffer.h"
34 #include "pxr/imaging/hgi/computeCmds.h"
35 #include "pxr/imaging/hgi/computeCmdsDesc.h"
36 #include "pxr/imaging/hgi/graphicsCmds.h"
37 #include "pxr/imaging/hgi/graphicsCmdsDesc.h"
38 #include "pxr/imaging/hgi/graphicsPipeline.h"
39 #include "pxr/imaging/hgi/resourceBindings.h"
40 #include "pxr/imaging/hgi/sampler.h"
41 #include "pxr/imaging/hgi/shaderFunction.h"
42 #include "pxr/imaging/hgi/shaderProgram.h"
43 #include "pxr/imaging/hgi/texture.h"
44 #include "pxr/imaging/hgi/types.h"
45 
46 #include <atomic>
47 #include <memory>
48 
49 PXR_NAMESPACE_OPEN_SCOPE
50 
51 class HgiCapabilities;
53 
54 using HgiUniquePtr = std::unique_ptr<class Hgi>;
55 
56 
110 class Hgi
111 {
112 public:
113  HGI_API
114  Hgi();
115 
116  HGI_API
117  virtual ~Hgi();
118 
127  HGI_API
128  void SubmitCmds(
129  HgiCmds* cmds,
130  HgiSubmitWaitType wait = HgiSubmitWaitTypeNoWait);
131 
133  HGI_API
134  static Hgi* GetPlatformDefaultHgi();
135 
141  HGI_API
142  static HgiUniquePtr CreatePlatformDefaultHgi();
143 
146  HGI_API
147  virtual bool IsBackendSupported() const = 0;
148 
152  HGI_API
153  static bool IsSupported();
154 
161  HGI_API
162  virtual HgiGraphicsCmdsUniquePtr CreateGraphicsCmds(
163  HgiGraphicsCmdsDesc const& desc) = 0;
164 
171  HGI_API
172  virtual HgiBlitCmdsUniquePtr CreateBlitCmds() = 0;
173 
180  HGI_API
181  virtual HgiComputeCmdsUniquePtr CreateComputeCmds(
182  HgiComputeCmdsDesc const& desc) = 0;
183 
186  HGI_API
187  virtual HgiTextureHandle CreateTexture(HgiTextureDesc const & desc) = 0;
188 
191  HGI_API
192  virtual void DestroyTexture(HgiTextureHandle* texHandle) = 0;
193 
199  HGI_API
201  HgiTextureViewDesc const & desc) = 0;
202 
207  HGI_API
208  virtual void DestroyTextureView(HgiTextureViewHandle* viewHandle) = 0;
209 
212  HGI_API
213  virtual HgiSamplerHandle CreateSampler(HgiSamplerDesc const & desc) = 0;
214 
217  HGI_API
218  virtual void DestroySampler(HgiSamplerHandle* smpHandle) = 0;
219 
222  HGI_API
223  virtual HgiBufferHandle CreateBuffer(HgiBufferDesc const & desc) = 0;
224 
227  HGI_API
228  virtual void DestroyBuffer(HgiBufferHandle* bufHandle) = 0;
229 
232  HGI_API
234  HgiShaderFunctionDesc const& desc) = 0;
235 
238  HGI_API
239  virtual void DestroyShaderFunction(
240  HgiShaderFunctionHandle* shaderFunctionHandle) = 0;
241 
244  HGI_API
246  HgiShaderProgramDesc const& desc) = 0;
247 
252  HGI_API
253  virtual void DestroyShaderProgram(
254  HgiShaderProgramHandle* shaderProgramHandle) = 0;
255 
258  HGI_API
260  HgiResourceBindingsDesc const& desc) = 0;
261 
264  HGI_API
265  virtual void DestroyResourceBindings(
266  HgiResourceBindingsHandle* resHandle) = 0;
267 
270  HGI_API
272  HgiGraphicsPipelineDesc const& pipeDesc) = 0;
273 
276  HGI_API
277  virtual void DestroyGraphicsPipeline(
278  HgiGraphicsPipelineHandle* pipeHandle) = 0;
279 
282  HGI_API
284  HgiComputePipelineDesc const& pipeDesc) = 0;
285 
288  HGI_API
289  virtual void DestroyComputePipeline(HgiComputePipelineHandle* pipeHandle)=0;
290 
293  HGI_API
294  virtual TfToken const& GetAPIName() const = 0;
295 
298  HGI_API
299  virtual HgiCapabilities const* GetCapabilities() const = 0;
300 
304  HGI_API
306 
313  HGI_API
314  virtual void StartFrame() = 0;
315 
319  HGI_API
320  virtual void EndFrame() = 0;
321 
322 protected:
323  // Returns a unique id for handle creation.
324  // Thread safety: Thread-safe atomic increment.
325  HGI_API
326  uint64_t GetUniqueId();
327 
328  // Calls Submit on provided Cmds.
329  // Derived classes can override this function if they need customize the
330  // command submission. The default implementation calls cmds->_Submit().
331  HGI_API
332  virtual bool _SubmitCmds(
333  HgiCmds* cmds, HgiSubmitWaitType wait);
334 
335 private:
336  Hgi & operator=(const Hgi&) = delete;
337  Hgi(const Hgi&) = delete;
338 
339  std::atomic<uint64_t> _uniqueIdCounter;
340 };
341 
342 
347 public:
348  virtual Hgi* New() const = 0;
349 };
350 
351 template <class T>
352 class HgiFactory : public HgiFactoryBase {
353 public:
354  Hgi* New() const {
355  return new T;
356  }
357 };
358 
359 
360 PXR_NAMESPACE_CLOSE_SCOPE
361 
362 #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.
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:238
virtual HGI_API HgiTextureViewHandle CreateTextureView(HgiTextureViewDesc const &desc)=0
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:64
Describes the properties needed to create a GPU texture.
Definition: texture.h:107
Base class of all factory types.
Definition: type.h:73
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
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.
Reports the capabilities of the Hgi device.
Definition: capabilities.h:39
Describes the properties needed to create a GPU compute pipeline.
virtual HGI_API HgiTextureHandle CreateTexture(HgiTextureDesc const &desc)=0
Create a texture in rendering backend.
Describes the properties needed to create a GPU buffer.
Definition: buffer.h:62
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:346
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:51
static HGI_API bool IsSupported()
Constructs a temporary Hgi object for the current platform and calls the object's IsBackendSupported(...
Describes a set of resources that are bound to the GPU during encoding.
Hydra Graphics Interface.
Definition: hgi.h:110
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
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.
virtual HGI_API HgiBufferHandle CreateBuffer(HgiBufferDesc const &desc)=0
Create a buffer in rendering backend.
Graphics commands are recorded in 'cmds' objects which are later submitted to hgi.
Definition: cmds.h:44
virtual HGI_API void DestroyShaderProgram(HgiShaderProgramHandle *shaderProgramHandle)=0
Destroy a shader program.
Handle that contains a hgi object and unique id.
Definition: handle.h:49
virtual HGI_API HgiBlitCmdsUniquePtr CreateBlitCmds()=0
Returns a BlitCmds object (for temporary use) that is ready to execute resource copy commands.
virtual HGI_API HgiResourceBindingsHandle CreateResourceBindings(HgiResourceBindingsDesc const &desc)=0
Create a new resource binding object.
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.