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(
96  HgiComputeCmdsDesc const& desc) override;
97 
98  HGIGL_API
99  HgiTextureHandle CreateTexture(HgiTextureDesc const & desc) override;
100 
101  HGIGL_API
102  void DestroyTexture(HgiTextureHandle* texHandle) override;
103 
104  HGIGL_API
106  HgiTextureViewDesc const& desc) override;
107 
108  HGIGL_API
109  void DestroyTextureView(HgiTextureViewHandle* viewHandle) override;
110 
111  HGIGL_API
112  HgiSamplerHandle CreateSampler(HgiSamplerDesc const & desc) override;
113 
114  HGIGL_API
115  void DestroySampler(HgiSamplerHandle* smpHandle) override;
116 
117  HGIGL_API
118  HgiBufferHandle CreateBuffer(HgiBufferDesc const & desc) override;
119 
120  HGIGL_API
121  void DestroyBuffer(HgiBufferHandle* bufHandle) override;
122 
123  HGIGL_API
125  HgiShaderFunctionDesc const& desc) override;
126 
127  HGIGL_API
129  HgiShaderFunctionHandle* shaderFunctionHandle) override;
130 
131  HGIGL_API
133  HgiShaderProgramDesc const& desc) override;
134 
135  HGIGL_API
137  HgiShaderProgramHandle* shaderProgramHandle) override;
138 
139  HGIGL_API
141  HgiResourceBindingsDesc const& desc) override;
142 
143  HGIGL_API
144  void DestroyResourceBindings(HgiResourceBindingsHandle* resHandle) override;
145 
146  HGIGL_API
148  HgiGraphicsPipelineDesc const& pipeDesc) override;
149 
150  HGIGL_API
152  HgiGraphicsPipelineHandle* pipeHandle) override;
153 
154  HGIGL_API
156  HgiComputePipelineDesc const& pipeDesc) override;
157 
158  HGIGL_API
159  void DestroyComputePipeline(HgiComputePipelineHandle* pipeHandle) override;
160 
161  HGIGL_API
162  TfToken const& GetAPIName() const override;
163 
164  HGIGL_API
165  HgiGLCapabilities const* GetCapabilities() const override;
166 
167  HGIGL_API
169 
170  HGIGL_API
171  void StartFrame() override;
172 
173  HGIGL_API
174  void EndFrame() override;
175 
177  // HgiGL specific API
179 
180  // Returns the opengl device.
181  HGIGL_API
182  HgiGLDevice* GetPrimaryDevice() const;
183 
189  HGIGL_API
191 
195  HGIGL_API
196  void DestroyContextArena(HgiGLContextArenaHandle* arenaHandle);
197 
200  HGIGL_API
201  void SetContextArena(HgiGLContextArenaHandle const& arenaHandle);
202  // -------------------------------------------------------------------------
203 
204 protected:
205  HGIGL_API
206  bool _SubmitCmds(HgiCmds* cmds, HgiSubmitWaitType wait) override;
207 
208 private:
209  HgiGL & operator=(const HgiGL&) = delete;
210  HgiGL(const HgiGL&) = delete;
211 
215  template<class T>
216  void _TrashObject(
217  HgiHandle<T>* handle, std::vector<HgiHandle<T>>* collector) {
218  collector->push_back(HgiHandle<T>(handle->Get(), /*id*/0));
219  *handle = HgiHandle<T>();
220  }
221 
222  HgiGLDevice* _device;
223  std::unique_ptr<HgiGLCapabilities> _capabilities;
224  HgiGLGarbageCollector _garbageCollector;
225  int _frameDepth;
226 };
227 
233 #define HGIGL_API_VERSION 2
234 
235 PXR_NAMESPACE_CLOSE_SCOPE
236 
237 #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.
The indirect command encoder is used to record the drawing primitives for a batch and capture the res...
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.
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.
HGIGL_API HgiIndirectCommandEncoder * GetIndirectCommandEncoder() const override
Returns the device-specific indirect command buffer encoder or nullptr if not supported.
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:110
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.
Describes the properties to construct a HgiComputeCmds.
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.
HGIGL_API HgiComputeCmdsUniquePtr CreateComputeCmds(HgiComputeCmdsDesc const &desc) override
Returns a ComputeCmds object (for temporary use) that is ready to record dispatch commands.