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"
30 #include "pxr/imaging/hgi/hgi.h"
31 #include "pxr/imaging/hgi/tokens.h"
32 
33 #include <functional>
34 #include <vector>
35 
37 
38 class HgiGLDevice;
39 
40 using HgiGLOpsFn = std::function<void(void)>;
41 using HgiGLOpsVector = std::vector<HgiGLOpsFn>;
42 
43 
44 /// \class HgiGL
45 ///
46 /// OpenGL implementation of the Hydra Graphics Interface.
47 ///
48 /// HgiGL expects the GL context to be externally managed.
49 /// When HgiGL is constructed and during any of its resource create / destroy
50 /// calls and during command recording operations it expects that the OpenGL
51 /// context is valid and current.
52 ///
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
64  HgiGraphicsCmdsDesc const& desc) override;
65 
66  HGIGL_API
68 
69  HGIGL_API
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 
148  /// Returns the opengl device.
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 
176 
177 #endif
HGIGL_API HgiGL()
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
std::function< void(void)> HgiGLOpsFn
Definition: hgi.h:40
int HgiHandle< class HgiTexture > HgiTextureHandle
HGIGL_API HgiBlitCmdsUniquePtr CreateBlitCmds() override
HGIGL_API HgiShaderFunctionHandle CreateShaderFunction(HgiShaderFunctionDesc const &desc) override
HGIGL_API TfToken const & GetAPIName() const override
HgiSubmitWaitType
Definition: enums.h:516
HGIGL_API HgiComputeCmdsUniquePtr CreateComputeCmds() override
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
Returns the opengl device.
Definition: hgi.h:53
HGIGL_API void DestroyComputePipeline(HgiComputePipelineHandle *pipeHandle) override
HGIGL_API HgiResourceBindingsHandle CreateResourceBindings(HgiResourceBindingsDesc const &desc) override
#define HGIGL_API
Definition: api.h:40
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:106
HGIGL_API void DestroyTextureView(HgiTextureViewHandle *viewHandle) override
std::unique_ptr< class HgiBlitCmds > HgiBlitCmdsUniquePtr
Definition: blitCmds.h:45
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
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 void EndFrame() override
std::unique_ptr< class HgiGraphicsCmds > HgiGraphicsCmdsUniquePtr
Definition: graphicsCmds.h:38
HGIGL_API void DestroyTexture(HgiTextureHandle *texHandle) override
std::vector< HgiGLOpsFn > HgiGLOpsVector
Definition: hgi.h:41
HGIGL_API void DestroyGraphicsPipeline(HgiGraphicsPipelineHandle *pipeHandle) override
Definition: cmds.h:44