HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hgi.h
Go to the documentation of this file.
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"
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 
39 
40 class HgiGLDevice;
41 
42 using HgiGLOpsFn = std::function<void(void)>;
43 using HgiGLOpsVector = std::vector<HgiGLOpsFn>;
45 
46 /// \class HgiGL
47 ///
48 /// OpenGL implementation of the Hydra Graphics Interface.
49 ///
50 /// \section GL Context Management
51 /// HgiGL expects any GL context(s) to be externally managed.
52 /// When HgiGL is constructed and during any of its resource create / destroy
53 /// calls and during command recording operations, it expects that an OpenGL
54 /// context is valid and current.
55 ///
56 /// When an application uses the same HgiGL instance from multiple GL contexts,
57 /// the expectations are that:
58 /// 1. The application has set up sharing amongst the various GL contexts. This
59 /// ensures that any non-container resources created may be shared amongst
60 /// the contexts. These shared resources may be safely deleted from
61 /// any context in the share group.
62 ///
63 /// 2. A context arena (see relevant API below) is used per GL context to
64 /// manage container resources that can't be shared amongst GL contexts.
65 /// Currently, HgiGL's support is limited to framebuffer objects.
66 ///
67 /// In the absence of an application provided context arena, the default arena
68 /// is used with the implied expectation that the same GL context is valid
69 /// and current for the lifetime of the HgiGL instance.
70 ///
71 class HgiGL final : public Hgi
72 {
73 public:
74  HGIGL_API
75  HgiGL();
76 
77  HGIGL_API
78  ~HgiGL() override;
79 
80  /// ------------------------------------------------------------------------
81  /// Virtual API
82  /// ------------------------------------------------------------------------
83 
84  HGIGL_API
85  bool IsBackendSupported() const override;
86 
87  HGIGL_API
89  HgiGraphicsCmdsDesc const& desc) override;
90 
91  HGIGL_API
93 
94  HGIGL_API
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 
176  /// ------------------------------------------------------------------------
177  // HgiGL specific API
178  /// ------------------------------------------------------------------------
179 
180  // Returns the opengl device.
181  HGIGL_API
182  HgiGLDevice* GetPrimaryDevice() const;
183 
184  /// ------------------------------------------------------------------------
185  /// Context arena API
186  /// Please refer to \ref "GL Context Management" for usage expectations.
187  ///
188  /// Creates and return a context arena object handle.
189  HGIGL_API
191 
192  /// Destroy a context arena.
193  /// Note: The context arena must be unset (by calling SetContextArena with
194  /// an empty handle) prior to destruction.
195  HGIGL_API
196  void DestroyContextArena(HgiGLContextArenaHandle* arenaHandle);
197 
198  /// Set the context arena to manage container resources (currently limited to
199  /// framebuffer objects) for graphics commands submitted subsequently.
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 
212  /// Invalidates the resource handle and places the object in the garbage
213  /// collector vector for future destruction.
214  /// This is helpful to avoid destroying GPU resources still in-flight.
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 
228 /// ----------------------------------------------------------------------------
229 /// API Version & History
230 /// ----------------------------------------------------------------------------
231 /// 1 -> 2: Added Context Arena API
232 ///
233 #define HGIGL_API_VERSION 2
234 
236 
237 #endif
HGIGL_API HgiGL()
HGIGL_API void SetContextArena(HgiGLContextArenaHandle const &arenaHandle)
HGIGL_API void DestroyShaderFunction(HgiShaderFunctionHandle *shaderFunctionHandle) override
HGIGL_API HgiComputePipelineHandle CreateComputePipeline(HgiComputePipelineDesc const &pipeDesc) override
HGIGL_API void DestroyShaderProgram(HgiShaderProgramHandle *shaderProgramHandle) override
HGIGL_API HgiTextureViewHandle CreateTextureView(HgiTextureViewDesc const &desc) override
HGIGL_API void DestroyContextArena(HgiGLContextArenaHandle *arenaHandle)
std::function< void(void)> HgiGLOpsFn
Definition: hgi.h:42
int HgiHandle< class HgiTexture > HgiTextureHandle
HGIGL_API HgiGLCapabilities const * GetCapabilities() const override
HGIGL_API HgiBlitCmdsUniquePtr CreateBlitCmds() override
HGIGL_API HgiShaderFunctionHandle CreateShaderFunction(HgiShaderFunctionDesc const &desc) override
HGIGL_API TfToken const & GetAPIName() const override
HgiSubmitWaitType
Definition: enums.h:664
Definition: token.h:87
HGIGL_API HgiSamplerHandle CreateSampler(HgiSamplerDesc const &desc) override
HGIGL_API HgiShaderProgramHandle CreateShaderProgram(HgiShaderProgramDesc const &desc) override
HGIGL_API void DestroyBuffer(HgiBufferHandle *bufHandle) override
T * Get() const
Definition: handle.h:56
HGIGL_API HgiGLDevice * GetPrimaryDevice() const
Definition: hgi.h:71
HGIGL_API void DestroyComputePipeline(HgiComputePipelineHandle *pipeHandle) override
HGIGL_API HgiResourceBindingsHandle CreateResourceBindings(HgiResourceBindingsDesc const &desc) override
#define HGIGL_API
Definition: api.h:40
HGIGL_API HgiIndirectCommandEncoder * GetIndirectCommandEncoder() const override
std::unique_ptr< class HgiGraphicsCmds > HgiGraphicsCmdsUniquePtr
Definition: effectsShader.h:45
HGIGL_API HgiTextureHandle CreateTexture(HgiTextureDesc const &desc) override
HGIGL_API ~HgiGL() override
HGIGL_API void DestroySampler(HgiSamplerHandle *smpHandle) override
HGIGL_API bool _SubmitCmds(HgiCmds *cmds, HgiSubmitWaitType wait) override
HGIGL_API HgiGraphicsCmdsUniquePtr CreateGraphicsCmds(HgiGraphicsCmdsDesc const &desc) override
std::unique_ptr< class HgiComputeCmds > HgiComputeCmdsUniquePtr
Definition: computeCmds.h:36
HGIGL_API void StartFrame() override
*tasks wait()
HGIGL_API HgiGraphicsPipelineHandle CreateGraphicsPipeline(HgiGraphicsPipelineDesc const &pipeDesc) override
Definition: hgi.h:110
HGIGL_API void DestroyTextureView(HgiTextureViewHandle *viewHandle) override
std::unique_ptr< class HgiBlitCmds > HgiBlitCmdsUniquePtr
Definition: blitCmds.h:45
HGIGL_API HgiComputeCmdsUniquePtr CreateComputeCmds(HgiComputeCmdsDesc const &desc) override
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
HGIGL_API HgiBufferHandle CreateBuffer(HgiBufferDesc const &desc) override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HGIGL_API void DestroyResourceBindings(HgiResourceBindingsHandle *resHandle) override
HGIGL_API HgiGLContextArenaHandle CreateContextArena()
HGIGL_API void EndFrame() override
HGIGL_API bool IsBackendSupported() const override
HGIGL_API void DestroyTexture(HgiTextureHandle *texHandle) override
std::vector< HgiGLOpsFn > HgiGLOpsVector
Definition: hgi.h:43
HGIGL_API void DestroyGraphicsPipeline(HgiGraphicsPipelineHandle *pipeHandle) override
Definition: cmds.h:44