1 // Copyright 2018 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_PLATFORMFUNCTIONS_H_
16 #define DAWNNATIVE_D3D12_PLATFORMFUNCTIONS_H_
17 
18 #include "dawn_native/d3d12/d3d12_platform.h"
19 
20 #include "common/DynamicLib.h"
21 #include "dawn_native/Error.h"
22 
23 #include <d3dcompiler.h>
24 
25 class DynamicLib;
26 
27 namespace dawn_native { namespace d3d12 {
28 
29     // Loads the functions required from the platform dynamically so that we don't need to rely on
30     // them being present in the system. For example linking against d3d12.lib would prevent
31     // dawn_native from loading on Windows 7 system where d3d12.dll doesn't exist.
32     class PlatformFunctions {
33       public:
34         PlatformFunctions();
35         ~PlatformFunctions();
36 
37         MaybeError LoadFunctions();
38         bool IsPIXEventRuntimeLoaded() const;
39         bool IsDXCAvailable() const;
40 
41         // Functions from d3d12.dll
42         PFN_D3D12_CREATE_DEVICE d3d12CreateDevice = nullptr;
43         PFN_D3D12_GET_DEBUG_INTERFACE d3d12GetDebugInterface = nullptr;
44 
45         PFN_D3D12_SERIALIZE_ROOT_SIGNATURE d3d12SerializeRootSignature = nullptr;
46         PFN_D3D12_CREATE_ROOT_SIGNATURE_DESERIALIZER d3d12CreateRootSignatureDeserializer = nullptr;
47         PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE d3d12SerializeVersionedRootSignature = nullptr;
48         PFN_D3D12_CREATE_VERSIONED_ROOT_SIGNATURE_DESERIALIZER
49         d3d12CreateVersionedRootSignatureDeserializer = nullptr;
50 
51         // Functions from dxgi.dll
52         using PFN_DXGI_GET_DEBUG_INTERFACE1 = HRESULT(WINAPI*)(UINT Flags,
53                                                                REFIID riid,
54                                                                _COM_Outptr_ void** pDebug);
55         PFN_DXGI_GET_DEBUG_INTERFACE1 dxgiGetDebugInterface1 = nullptr;
56 
57         using PFN_CREATE_DXGI_FACTORY2 = HRESULT(WINAPI*)(UINT Flags,
58                                                           REFIID riid,
59                                                           _COM_Outptr_ void** ppFactory);
60         PFN_CREATE_DXGI_FACTORY2 createDxgiFactory2 = nullptr;
61 
62         // Functions from dxcompiler.dll
63         using PFN_DXC_CREATE_INSTANCE = HRESULT(WINAPI*)(REFCLSID rclsid,
64                                                          REFIID riid,
65                                                          _COM_Outptr_ void** ppCompiler);
66         PFN_DXC_CREATE_INSTANCE dxcCreateInstance = nullptr;
67 
68         // Functions from d3d3compiler.dll
69         pD3DCompile d3dCompile = nullptr;
70 
71         // Functions from WinPixEventRuntime.dll
72         using PFN_PIX_END_EVENT_ON_COMMAND_LIST =
73             HRESULT(WINAPI*)(ID3D12GraphicsCommandList* commandList);
74 
75         PFN_PIX_END_EVENT_ON_COMMAND_LIST pixEndEventOnCommandList = nullptr;
76 
77         using PFN_PIX_BEGIN_EVENT_ON_COMMAND_LIST = HRESULT(
78             WINAPI*)(ID3D12GraphicsCommandList* commandList, UINT64 color, _In_ PCSTR formatString);
79 
80         PFN_PIX_BEGIN_EVENT_ON_COMMAND_LIST pixBeginEventOnCommandList = nullptr;
81 
82         using PFN_SET_MARKER_ON_COMMAND_LIST = HRESULT(
83             WINAPI*)(ID3D12GraphicsCommandList* commandList, UINT64 color, _In_ PCSTR formatString);
84 
85         PFN_SET_MARKER_ON_COMMAND_LIST pixSetMarkerOnCommandList = nullptr;
86 
87         // Functions from D3D11.dll
88         PFN_D3D11ON12_CREATE_DEVICE d3d11on12CreateDevice = nullptr;
89 
90       private:
91         MaybeError LoadD3D12();
92         MaybeError LoadD3D11();
93         MaybeError LoadDXGI();
94         void LoadDXIL();
95         void LoadDXCompiler();
96         MaybeError LoadFXCompiler();
97         void LoadPIXRuntime();
98 
99         DynamicLib mD3D12Lib;
100         DynamicLib mD3D11Lib;
101         DynamicLib mDXGILib;
102         DynamicLib mDXILLib;
103         DynamicLib mDXCompilerLib;
104         DynamicLib mFXCompilerLib;
105         DynamicLib mPIXEventRuntimeLib;
106     };
107 
108 }}  // namespace dawn_native::d3d12
109 
110 #endif  // DAWNNATIVE_VULKAN_VULKANFUNCTIONS_H_
111