All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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_GL_HGI_H
25 #define PXR_IMAGING_HGI_GL_HGI_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hgiGL/api.h"
29 #include "pxr/imaging/hgiGL/garbageCollector.h"
30 #include "pxr/imaging/hgi/hgi.h"
31 #include "pxr/imaging/hgi/tokens.h"
32 
33 #include <functional>
34 #include <vector>
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 class HgiGLDevice;
39 
40 using HgiGLOpsFn = std::function<void(void)>;
41 using HgiGLOpsVector = std::vector<HgiGLOpsFn>;
42 
43 
53 class HgiGL final : public Hgi
54 {
55 public:
56  HGIGL_API
57  HgiGL();
58 
59  HGIGL_API
60  ~HgiGL() override;
61 
62  HGIGL_API
63  HgiGraphicsCmdsUniquePtr CreateGraphicsCmds(
64  HgiGraphicsCmdsDesc const& desc) override;
65 
66  HGIGL_API
67  HgiBlitCmdsUniquePtr CreateBlitCmds() override;
68 
69  HGIGL_API
70  HgiComputeCmdsUniquePtr CreateComputeCmds() override;
71 
72  HGIGL_API
73  HgiTextureHandle CreateTexture(HgiTextureDesc const & desc) override;
74 
75  HGIGL_API
76  void DestroyTexture(HgiTextureHandle* texHandle) override;
77 
78  HGIGL_API
80  HgiTextureViewDesc const& desc) override;
81 
82  HGIGL_API
83  void DestroyTextureView(HgiTextureViewHandle* viewHandle) override;
84 
85  HGIGL_API
86  HgiSamplerHandle CreateSampler(HgiSamplerDesc const & desc) override;
87 
88  HGIGL_API
89  void DestroySampler(HgiSamplerHandle* smpHandle) override;
90 
91  HGIGL_API
92  HgiBufferHandle CreateBuffer(HgiBufferDesc const & desc) override;
93 
94  HGIGL_API
95  void DestroyBuffer(HgiBufferHandle* bufHandle) override;
96 
97  HGIGL_API
99  HgiShaderFunctionDesc const& desc) override;
100 
101  HGIGL_API
103  HgiShaderFunctionHandle* shaderFunctionHandle) override;
104 
105  HGIGL_API
107  HgiShaderProgramDesc const& desc) override;
108 
109  HGIGL_API
111  HgiShaderProgramHandle* shaderProgramHandle) override;
112 
113  HGIGL_API
115  HgiResourceBindingsDesc const& desc) override;
116 
117  HGIGL_API
118  void DestroyResourceBindings(HgiResourceBindingsHandle* resHandle) override;
119 
120  HGIGL_API
122  HgiGraphicsPipelineDesc const& pipeDesc) override;
123 
124  HGIGL_API
126  HgiGraphicsPipelineHandle* pipeHandle) override;
127 
128  HGIGL_API
130  HgiComputePipelineDesc const& pipeDesc) override;
131 
132  HGIGL_API
133  void DestroyComputePipeline(HgiComputePipelineHandle* pipeHandle) override;
134 
135  HGIGL_API
136  TfToken const& GetAPIName() const override;
137 
138  HGIGL_API
139  void StartFrame() override;
140 
141  HGIGL_API
142  void EndFrame() override;
143 
144  //
145  // HgiGL specific
146  //
147 
149  HGIGL_API
150  HgiGLDevice* GetPrimaryDevice() const;
151 
152 protected:
153  HGIGL_API
154  bool _SubmitCmds(HgiCmds* cmds, HgiSubmitWaitType wait) override;
155 
156 private:
157  HgiGL & operator=(const HgiGL&) = delete;
158  HgiGL(const HgiGL&) = delete;
159 
160  // Invalidates the resource handle and places the object in the garbage
161  // collector vector for future destruction.
162  // This is helpful to avoid destroying GPU resources still in-flight.
163  template<class T>
164  void _TrashObject(
165  HgiHandle<T>* handle, std::vector<HgiHandle<T>>* collector) {
166  collector->push_back(HgiHandle<T>(handle->Get(), /*id*/0));
167  *handle = HgiHandle<T>();
168  }
169 
170  HgiGLDevice* _device;
171  HgiGLGarbageCollector _garbageCollector;
172  int _frameDepth;
173 };
174 
175 PXR_NAMESPACE_CLOSE_SCOPE
176 
177 #endif
HGIGL_API void DestroyTexture(HgiTextureHandle *texHandle) override
Destroy a texture in rendering backend.
HGIGL_API HgiGLDevice * GetPrimaryDevice() const
Returns the opengl device.
HGIGL_API HgiShaderFunctionHandle CreateShaderFunction(HgiShaderFunctionDesc const &desc) override
Create a new shader function.
Describes the properties needed to create a GPU texture view from an existing GPU texture object...
Definition: texture.h:238
HGIGL_API HgiBufferHandle CreateBuffer(HgiBufferDesc const &desc) override
Create a buffer in rendering backend.
HGIGL_API HgiTextureViewHandle CreateTextureView(HgiTextureViewDesc const &desc) override
Create a texture view in rendering backend.
Describes the properties needed to create a GPU sampler.
Definition: sampler.h:58
HGIGL_API void DestroyComputePipeline(HgiComputePipelineHandle *pipeHandle) override
Destroy a compute pipeline state object.
Describes the properties needed to create a GPU texture.
Definition: texture.h:107
HGIGL_API void DestroyShaderProgram(HgiShaderProgramHandle *shaderProgramHandle) override
Destroy a shader program.
HGIGL_API HgiComputeCmdsUniquePtr CreateComputeCmds() override
Returns a ComputeCmds object (for temporary use) that is ready to record dispatch commands...
Handles garbage collection of opengl objects by delaying their destruction until those objects are no...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HGIGL_API void StartFrame() override
Optionally called by client app at the start of a new rendering frame.
HGIGL_API HgiTextureHandle CreateTexture(HgiTextureDesc const &desc) override
Create a texture in rendering backend.
HGIGL_API void DestroyShaderFunction(HgiShaderFunctionHandle *shaderFunctionHandle) override
Destroy a shader function.
OpenGL implementation of the Hydra Graphics Interface.
Definition: hgi.h:53
HGIGL_API HgiSamplerHandle CreateSampler(HgiSamplerDesc const &desc) override
Create a sampler in rendering backend.
Describes the properties needed to create a GPU compute pipeline.
HGIGL_API void DestroyGraphicsPipeline(HgiGraphicsPipelineHandle *pipeHandle) override
Destroy a graphics pipeline state object.
Describes the properties needed to create a GPU buffer.
Definition: buffer.h:62
HGIGL_API void EndFrame() override
Optionally called at the end of a rendering frame.
Describes the properties to begin a HgiGraphicsCmds.
HGIGL_API HgiBlitCmdsUniquePtr CreateBlitCmds() override
Returns a BlitCmds object (for temporary use) that is ready to execute resource copy commands...
Describes the properties needed to create a GPU shader program.
Definition: shaderProgram.h:51
Describes a set of resources that are bound to the GPU during encoding.
Hydra Graphics Interface.
Definition: hgi.h:106
HGIGL_API void DestroySampler(HgiSamplerHandle *smpHandle) override
Destroy a sampler in rendering backend.
HGIGL_API HgiGraphicsPipelineHandle CreateGraphicsPipeline(HgiGraphicsPipelineDesc const &pipeDesc) override
Create a new graphics pipeline state object.
HGIGL_API HgiComputePipelineHandle CreateComputePipeline(HgiComputePipelineDesc const &pipeDesc) override
Create a new compute pipeline state object.
HGIGL_API HgiGraphicsCmdsUniquePtr CreateGraphicsCmds(HgiGraphicsCmdsDesc const &desc) override
Returns a GraphicsCmds object (for temporary use) that is ready to record draw commands.
Describes the properties needed to create a GPU shader function.
HGIGL_API HgiResourceBindingsHandle CreateResourceBindings(HgiResourceBindingsDesc const &desc) override
Create a new resource binding object.
HGIGL_API void DestroyBuffer(HgiBufferHandle *bufHandle) override
Destroy a buffer in rendering backend.
HGIGL_API void DestroyTextureView(HgiTextureViewHandle *viewHandle) override
Destroy a texture view in rendering backend.
Describes the properties needed to create a GPU pipeline.
HGIGL_API TfToken const & GetAPIName() const override
Return the name of the api (e.g.
HGIGL_API void DestroyResourceBindings(HgiResourceBindingsHandle *resHandle) override
Destroy a resource binding object.
Graphics commands are recorded in &#39;cmds&#39; objects which are later submitted to hgi.
Definition: cmds.h:44
Handle that contains a hgi object and unique id.
Definition: handle.h:49
HGIGL_API HgiShaderProgramHandle CreateShaderProgram(HgiShaderProgramDesc const &desc) override
Create a new shader program.