1 //
2 // Copyright 2020 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_HGIVULKAN_HGI_H
25 #define PXR_IMAGING_HGIVULKAN_HGI_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hgi/hgi.h"
29 #include "pxr/imaging/hgi/tokens.h"
30 
31 #include "pxr/imaging/hgiVulkan/api.h"
32 #include "pxr/imaging/hgiVulkan/capabilities.h"
33 #include "pxr/imaging/hgiVulkan/commandQueue.h"
34 #include "pxr/imaging/hgiVulkan/device.h"
35 #include "pxr/imaging/hgiVulkan/vulkan.h"
36 
37 #include <thread>
38 #include <vector>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 class HgiVulkanGarbageCollector;
43 class HgiVulkanInstance;
44 
45 
46 /// \class HgiVulkan
47 ///
48 /// Vulkan implementation of the Hydra Graphics Interface.
49 ///
50 class HgiVulkan final : public Hgi
51 {
52 public:
53     HGIVULKAN_API
54     HgiVulkan();
55 
56     HGIVULKAN_API
57     ~HgiVulkan() override;
58 
59     HGIVULKAN_API
60     HgiGraphicsCmdsUniquePtr CreateGraphicsCmds(
61         HgiGraphicsCmdsDesc const& desc) override;
62 
63     HGIVULKAN_API
64     HgiBlitCmdsUniquePtr CreateBlitCmds() override;
65 
66     HGIVULKAN_API
67     HgiComputeCmdsUniquePtr CreateComputeCmds() override;
68 
69     HGIVULKAN_API
70     HgiTextureHandle CreateTexture(HgiTextureDesc const & desc) override;
71 
72     HGIVULKAN_API
73     void DestroyTexture(HgiTextureHandle* texHandle) override;
74 
75     HGIVULKAN_API
76     HgiTextureViewHandle CreateTextureView(
77         HgiTextureViewDesc const& desc) override;
78 
79     HGIVULKAN_API
80     void DestroyTextureView(HgiTextureViewHandle* viewHandle) override;
81 
82     HGIVULKAN_API
83     HgiSamplerHandle CreateSampler(HgiSamplerDesc const & desc) override;
84 
85     HGIVULKAN_API
86     void DestroySampler(HgiSamplerHandle* smpHandle) override;
87 
88     HGIVULKAN_API
89     HgiBufferHandle CreateBuffer(HgiBufferDesc const & desc) override;
90 
91     HGIVULKAN_API
92     void DestroyBuffer(HgiBufferHandle* bufHandle) override;
93 
94     HGIVULKAN_API
95     HgiShaderFunctionHandle CreateShaderFunction(
96         HgiShaderFunctionDesc const& desc) override;
97 
98     HGIVULKAN_API
99     void DestroyShaderFunction(
100         HgiShaderFunctionHandle* shaderFunctionHandle) override;
101 
102     HGIVULKAN_API
103     HgiShaderProgramHandle CreateShaderProgram(
104         HgiShaderProgramDesc const& desc) override;
105 
106     HGIVULKAN_API
107     void DestroyShaderProgram(
108         HgiShaderProgramHandle* shaderProgramHandle) override;
109 
110     HGIVULKAN_API
111     HgiResourceBindingsHandle CreateResourceBindings(
112         HgiResourceBindingsDesc const& desc) override;
113 
114     HGIVULKAN_API
115     void DestroyResourceBindings(HgiResourceBindingsHandle* resHandle) override;
116 
117     HGIVULKAN_API
118     HgiGraphicsPipelineHandle CreateGraphicsPipeline(
119         HgiGraphicsPipelineDesc const& pipeDesc) override;
120 
121     HGIVULKAN_API
122     void DestroyGraphicsPipeline(
123         HgiGraphicsPipelineHandle* pipeHandle) override;
124 
125     HGIVULKAN_API
126     HgiComputePipelineHandle CreateComputePipeline(
127         HgiComputePipelineDesc const& pipeDesc) override;
128 
129     HGIVULKAN_API
130     void DestroyComputePipeline(HgiComputePipelineHandle* pipeHandle) override;
131 
132     HGIVULKAN_API
133     TfToken const& GetAPIName() const override;
134 
135     HGIVULKAN_API
136     HgiVulkanCapabilities const* GetCapabilities() const override;
137 
138     HGIVULKAN_API
139     void StartFrame() override;
140 
141     HGIVULKAN_API
142     void EndFrame() override;
143 
144     //
145     // HgiVulkan specific
146     //
147 
148     /// Returns the Hgi vulkan instance.
149     /// Thread safety: Yes.
150     HGIVULKAN_API
151     HgiVulkanInstance* GetVulkanInstance() const;
152 
153     /// Returns the primary (presentation) vulkan device.
154     /// Thread safety: Yes.
155     HGIVULKAN_API
156     HgiVulkanDevice* GetPrimaryDevice() const;
157 
158     /// Returns the garbage collector.
159     /// Thread safety: Yes.
160     HGIVULKAN_API
161     HgiVulkanGarbageCollector* GetGarbageCollector() const;
162 
163     /// Invalidates the resource handle and places the object in the garbage
164     /// collector vector for future destruction.
165     /// This is helpful to avoid destroying GPU resources still in-flight.
166     template<class T, class H>
TrashObject(H * handle,std::vector<T * > * collector)167     void TrashObject(H* handle, std::vector<T*>* collector)
168     {
169         T* object = static_cast<T*>(handle->Get());
170         HgiVulkanDevice* device = object->GetDevice();
171         HgiVulkanCommandQueue* queue = device->GetCommandQueue();
172         object->GetInflightBits() = queue->GetInflightCommandBuffersBits();
173         collector->push_back(object);
174         *handle = H();
175     }
176 
177 protected:
178     HGIVULKAN_API
179     bool _SubmitCmds(HgiCmds* cmds, HgiSubmitWaitType wait) override;
180 
181 private:
182     HgiVulkan & operator=(const HgiVulkan&) = delete;
183     HgiVulkan(const HgiVulkan&) = delete;
184 
185     // Perform low frequency actions, such as garbage collection.
186     // Thread safety: No. Must be called from main thread.
187     void _EndFrameSync();
188 
189     HgiVulkanInstance* _instance;
190     HgiVulkanDevice* _device;
191     HgiVulkanGarbageCollector* _garbageCollector;
192     std::thread::id _threadId;
193     int _frameDepth;
194 };
195 
196 PXR_NAMESPACE_CLOSE_SCOPE
197 
198 #endif
199