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/capabilities.h"
30 #include "pxr/imaging/hgiGL/garbageCollector.h"
31 #include "pxr/imaging/hgi/hgi.h"
32 #include "pxr/imaging/hgi/tokens.h"
33 
34 #include <functional>
35 #include <memory>
36 #include <vector>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 class HgiGLDevice;
41 
42 using HgiGLOpsFn = std::function<void(void)>;
43 using HgiGLOpsVector = std::vector<HgiGLOpsFn>;
45 
71 class HgiGL final : public Hgi
72 {
73 public:
74  HGIGL_API
75  HgiGL();
76 
77  HGIGL_API
78  ~HgiGL() override;
79 
83 
84  HGIGL_API
85  bool IsBackendSupported() const override;
86 
87  HGIGL_API
88  HgiGraphicsCmdsUniquePtr CreateGraphicsCmds(
89  HgiGraphicsCmdsDesc const& desc) override;
90 
91  HGIGL_API
92  HgiBlitCmdsUniquePtr CreateBlitCmds() override;
93 
94  HGIGL_API
95  HgiComputeCmdsUniquePtr CreateComputeCmds() override;
96 
97  HGIGL_API
98  HgiTextureHandle CreateTexture(HgiTextureDesc const & desc) override;
99 
100  HGIGL_API
101  void DestroyTexture(HgiTextureHandle* texHandle) override;
102 
103  HGIGL_API
105  HgiTextureViewDesc const& desc) override;
106 
107  HGIGL_API
108  void DestroyTextureView(HgiTextureViewHandle* viewHandle) override;
109 
110  HGIGL_API
111  HgiSamplerHandle CreateSampler(HgiSamplerDesc const & desc) override;
112 
113  HGIGL_API
114  void DestroySampler(HgiSamplerHandle* smpHandle) override;
115 
116  HGIGL_API
117  HgiBufferHandle CreateBuffer(HgiBufferDesc const & desc) override;
118 
119  HGIGL_API
120  void DestroyBuffer(HgiBufferHandle* bufHandle) override;
121 
122  HGIGL_API
124  HgiShaderFunctionDesc const& desc) override;
125 
126  HGIGL_API
128  HgiShaderFunctionHandle* shaderFunctionHandle) override;
129 
130  HGIGL_API
132  HgiShaderProgramDesc const& desc) override;
133 
134  HGIGL_API
136  HgiShaderProgramHandle* shaderProgramHandle) override;
137 
138  HGIGL_API
140  HgiResourceBindingsDesc const& desc) override;
141 
142  HGIGL_API
143  void DestroyResourceBindings(HgiResourceBindingsHandle* resHandle) override;
144 
145  HGIGL_API
147  HgiGraphicsPipelineDesc const& pipeDesc) override;
148 
149  HGIGL_API
151  HgiGraphicsPipelineHandle* pipeHandle) override;
152 
153  HGIGL_API
155  HgiComputePipelineDesc const& pipeDesc) override;
156 
157  HGIGL_API
158  void DestroyComputePipeline(HgiComputePipelineHandle* pipeHandle) override;
159 
160  HGIGL_API
161  TfToken const& GetAPIName() const override;
162 
163  HGIGL_API
164  HgiGLCapabilities const* GetCapabilities() const override;
165 
166  HGIGL_API
167  void StartFrame() override;
168 
169  HGIGL_API
170  void EndFrame() override;
171 
173  // HgiGL specific API
175 
176  // Returns the opengl device.
177  HGIGL_API
178  HgiGLDevice* GetPrimaryDevice() const;
179 
185  HGIGL_API
187 
191  HGIGL_API
192  void DestroyContextArena(HgiGLContextArenaHandle* arenaHandle);
193 
196  HGIGL_API
197  void SetContextArena(HgiGLContextArenaHandle const& arenaHandle);
198  // -------------------------------------------------------------------------
199 
200 protected:
201  HGIGL_API
202  bool _SubmitCmds(HgiCmds* cmds, HgiSubmitWaitType wait) override;
203 
204 private:
205  HgiGL & operator=(const HgiGL&) = delete;
206  HgiGL(const HgiGL&) = delete;
207 
211  template<class T>
212  void _TrashObject(
213  HgiHandle<T>* handle, std::vector<HgiHandle<T>>* collector) {
214  collector->push_back(HgiHandle<T>(handle->Get(), /*id*/0));
215  *handle = HgiHandle<T>();
216  }
217 
218  HgiGLDevice* _device;
219  std::unique_ptr<HgiGLCapabilities> _capabilities;
220  HgiGLGarbageCollector _garbageCollector;
221  int _frameDepth;
222 };
223 
229 #define HGIGL_API_VERSION 2
230 
231 PXR_NAMESPACE_CLOSE_SCOPE
232 
233 #endif
HGIGL_API void DestroyTexture(HgiTextureHandle *texHandle) override
Destroy a texture in rendering backend.
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 HgiGLDevice * GetPrimaryDevice() const
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.
HGIGL_API void SetContextArena(HgiGLContextArenaHandle const &arenaHandle)
Set the context arena to manage container resources (currently limited to framebuffer objects) for gr...
HGIGL_API HgiGLCapabilities const * GetCapabilities() const override
Returns the device-specific capabilities structure.
Describes the properties needed to create a GPU sampler.
Definition: sampler.h:64
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
OpenGL implementation of GPU device.
Definition: device.h:46
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 DestroyContextArena(HgiGLContextArenaHandle *arenaHandle)
Destroy a context arena.
HGIGL_API bool IsBackendSupported() const override
HGIGL_API void DestroyShaderFunction(HgiShaderFunctionHandle *shaderFunctionHandle) override
Destroy a shader function.
OpenGL implementation of the Hydra Graphics Interface.
Definition: hgi.h:71
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 HgiGLContextArenaHandle CreateContextArena()
Reports the capabilities of the HgiGL device.
Definition: capabilities.h:37
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:108
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 'cmds' 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.