1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_D3D12_DEVICED3D12_H_
16 #define DAWNNATIVE_D3D12_DEVICED3D12_H_
17 
18 #include "dawn_native/dawn_platform.h"
19 
20 #include "common/SerialQueue.h"
21 #include "dawn_native/Device.h"
22 #include "dawn_native/d3d12/CommandRecordingContext.h"
23 #include "dawn_native/d3d12/D3D12Info.h"
24 #include "dawn_native/d3d12/Forward.h"
25 #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
26 
27 #include <memory>
28 
29 namespace dawn_native { namespace d3d12 {
30 
31     class CommandAllocatorManager;
32     class DescriptorHeapAllocator;
33     class ShaderVisibleDescriptorAllocator;
34     class MapRequestTracker;
35     class PlatformFunctions;
36     class ResourceAllocatorManager;
37     class ResidencyManager;
38 
39 #define ASSERT_SUCCESS(hr)            \
40     {                                 \
41         HRESULT succeeded = hr;       \
42         ASSERT(SUCCEEDED(succeeded)); \
43     }
44 
45     // Definition of backend types
46     class Device : public DeviceBase {
47       public:
48         Device(Adapter* adapter, const DeviceDescriptor* descriptor);
49         ~Device();
50 
51         MaybeError Initialize();
52 
53         CommandBufferBase* CreateCommandBuffer(CommandEncoder* encoder,
54                                                const CommandBufferDescriptor* descriptor) override;
55 
56         Serial GetCompletedCommandSerial() const final override;
57         Serial GetLastSubmittedCommandSerial() const final override;
58         MaybeError TickImpl() override;
59 
60         ComPtr<ID3D12Device> GetD3D12Device() const;
61         ComPtr<ID3D12CommandQueue> GetCommandQueue() const;
62         ID3D12SharingContract* GetSharingContract() const;
63 
64         ComPtr<ID3D12CommandSignature> GetDispatchIndirectSignature() const;
65         ComPtr<ID3D12CommandSignature> GetDrawIndirectSignature() const;
66         ComPtr<ID3D12CommandSignature> GetDrawIndexedIndirectSignature() const;
67 
68         DescriptorHeapAllocator* GetDescriptorHeapAllocator() const;
69         MapRequestTracker* GetMapRequestTracker() const;
70         CommandAllocatorManager* GetCommandAllocatorManager() const;
71         ResidencyManager* GetResidencyManager() const;
72 
73         const PlatformFunctions* GetFunctions() const;
74         ComPtr<IDXGIFactory4> GetFactory() const;
75 
76         ResultOrError<CommandRecordingContext*> GetPendingCommandContext();
77         Serial GetPendingCommandSerial() const override;
78 
79         const D3D12DeviceInfo& GetDeviceInfo() const;
80 
81         MaybeError NextSerial();
82         MaybeError WaitForSerial(Serial serial);
83 
84         void ReferenceUntilUnused(ComPtr<IUnknown> object);
85 
86         MaybeError ExecutePendingCommandContext();
87 
88         ResultOrError<std::unique_ptr<StagingBufferBase>> CreateStagingBuffer(size_t size) override;
89         MaybeError CopyFromStagingToBuffer(StagingBufferBase* source,
90                                            uint64_t sourceOffset,
91                                            BufferBase* destination,
92                                            uint64_t destinationOffset,
93                                            uint64_t size) override;
94 
95         ResultOrError<ResourceHeapAllocation> AllocateMemory(
96             D3D12_HEAP_TYPE heapType,
97             const D3D12_RESOURCE_DESC& resourceDescriptor,
98             D3D12_RESOURCE_STATES initialUsage);
99 
100         void DeallocateMemory(ResourceHeapAllocation& allocation);
101 
102         ShaderVisibleDescriptorAllocator* GetShaderVisibleDescriptorAllocator() const;
103 
104         TextureBase* WrapSharedHandle(const ExternalImageDescriptor* descriptor,
105                                       HANDLE sharedHandle,
106                                       uint64_t acquireMutexKey,
107                                       bool isSwapChainTexture);
108         ResultOrError<ComPtr<IDXGIKeyedMutex>> CreateKeyedMutexForTexture(
109             ID3D12Resource* d3d12Resource);
110         void ReleaseKeyedMutexForTexture(ComPtr<IDXGIKeyedMutex> dxgiKeyedMutex);
111 
112         void InitTogglesFromDriver();
113 
114       private:
115         ResultOrError<BindGroupBase*> CreateBindGroupImpl(
116             const BindGroupDescriptor* descriptor) override;
117         ResultOrError<BindGroupLayoutBase*> CreateBindGroupLayoutImpl(
118             const BindGroupLayoutDescriptor* descriptor) override;
119         ResultOrError<BufferBase*> CreateBufferImpl(const BufferDescriptor* descriptor) override;
120         ResultOrError<ComputePipelineBase*> CreateComputePipelineImpl(
121             const ComputePipelineDescriptor* descriptor) override;
122         ResultOrError<PipelineLayoutBase*> CreatePipelineLayoutImpl(
123             const PipelineLayoutDescriptor* descriptor) override;
124         ResultOrError<QueueBase*> CreateQueueImpl() override;
125         ResultOrError<RenderPipelineBase*> CreateRenderPipelineImpl(
126             const RenderPipelineDescriptor* descriptor) override;
127         ResultOrError<SamplerBase*> CreateSamplerImpl(const SamplerDescriptor* descriptor) override;
128         ResultOrError<ShaderModuleBase*> CreateShaderModuleImpl(
129             const ShaderModuleDescriptor* descriptor) override;
130         ResultOrError<SwapChainBase*> CreateSwapChainImpl(
131             const SwapChainDescriptor* descriptor) override;
132         ResultOrError<NewSwapChainBase*> CreateSwapChainImpl(
133             Surface* surface,
134             NewSwapChainBase* previousSwapChain,
135             const SwapChainDescriptor* descriptor) override;
136         ResultOrError<TextureBase*> CreateTextureImpl(const TextureDescriptor* descriptor) override;
137         ResultOrError<TextureViewBase*> CreateTextureViewImpl(
138             TextureBase* texture,
139             const TextureViewDescriptor* descriptor) override;
140 
141         void Destroy() override;
142         MaybeError WaitForIdleForDestruction() override;
143 
144         Serial mCompletedSerial = 0;
145         Serial mLastSubmittedSerial = 0;
146         ComPtr<ID3D12Fence> mFence;
147         HANDLE mFenceEvent = nullptr;
148 
149         ComPtr<ID3D12Device> mD3d12Device;  // Device is owned by adapter and will not be outlived.
150         ComPtr<ID3D12CommandQueue> mCommandQueue;
151         ComPtr<ID3D12SharingContract> mD3d12SharingContract;
152 
153         // 11on12 device and device context corresponding to mCommandQueue
154         ComPtr<ID3D11On12Device> mD3d11On12Device;
155         ComPtr<ID3D11DeviceContext2> mD3d11On12DeviceContext;
156 
157         ComPtr<ID3D12CommandSignature> mDispatchIndirectSignature;
158         ComPtr<ID3D12CommandSignature> mDrawIndirectSignature;
159         ComPtr<ID3D12CommandSignature> mDrawIndexedIndirectSignature;
160 
161         CommandRecordingContext mPendingCommands;
162 
163         SerialQueue<ComPtr<IUnknown>> mUsedComObjectRefs;
164 
165         std::unique_ptr<CommandAllocatorManager> mCommandAllocatorManager;
166         std::unique_ptr<DescriptorHeapAllocator> mDescriptorHeapAllocator;
167         std::unique_ptr<MapRequestTracker> mMapRequestTracker;
168         std::unique_ptr<ResourceAllocatorManager> mResourceAllocatorManager;
169         std::unique_ptr<ResidencyManager> mResidencyManager;
170         std::unique_ptr<ShaderVisibleDescriptorAllocator> mShaderVisibleDescriptorAllocator;
171     };
172 
173 }}  // namespace dawn_native::d3d12
174 
175 #endif  // DAWNNATIVE_D3D12_DEVICED3D12_H_
176