1 /*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/gpu/d3d/D3DTestUtils.h"
9
10 #ifdef SK_DIRECT3D
11 #include <d3d12sdklayers.h>
12 #include <dxgi1_4.h>
13
14 #include "include/gpu/d3d/GrD3DBackendContext.h"
15
16 namespace sk_gpu_test {
17
get_hardware_adapter(IDXGIFactory4 * pFactory,IDXGIAdapter1 ** ppAdapter)18 void get_hardware_adapter(IDXGIFactory4* pFactory, IDXGIAdapter1** ppAdapter) {
19 *ppAdapter = nullptr;
20 for (UINT adapterIndex = 0; ; ++adapterIndex) {
21 IDXGIAdapter1* pAdapter = nullptr;
22 if (DXGI_ERROR_NOT_FOUND == pFactory->EnumAdapters1(adapterIndex, &pAdapter)) {
23 // No more adapters to enumerate.
24 break;
25 }
26
27 // Check to see if the adapter supports Direct3D 12, but don't create the
28 // actual device yet.
29 if (SUCCEEDED(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device),
30 nullptr))) {
31 *ppAdapter = pAdapter;
32 return;
33 }
34 pAdapter->Release();
35 }
36 }
37
CreateD3DBackendContext(GrD3DBackendContext * ctx,bool isProtected)38 bool CreateD3DBackendContext(GrD3DBackendContext* ctx,
39 bool isProtected) {
40 #if defined(SK_ENABLE_D3D_DEBUG_LAYER)
41 // Enable the D3D12 debug layer.
42 {
43 gr_cp<ID3D12Debug> debugController;
44 if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
45 {
46 debugController->EnableDebugLayer();
47 }
48 }
49 #endif
50 // Create the device
51 gr_cp<IDXGIFactory4> factory;
52 if (!SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) {
53 return false;
54 }
55
56 gr_cp<IDXGIAdapter1> hardwareAdapter;
57 get_hardware_adapter(factory.Get(), &hardwareAdapter);
58
59 gr_cp<ID3D12Device> device;
60 if (!SUCCEEDED(D3D12CreateDevice(hardwareAdapter.Get(),
61 D3D_FEATURE_LEVEL_11_0,
62 IID_PPV_ARGS(&device)))) {
63 return false;
64 }
65
66 // Create the command queue
67 gr_cp<ID3D12CommandQueue> queue;
68 D3D12_COMMAND_QUEUE_DESC queueDesc = {};
69 queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
70 queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
71
72 if (!SUCCEEDED(device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&queue)))) {
73 return false;
74 }
75
76 ctx->fDevice = device;
77 ctx->fQueue = queue;
78 // TODO: set up protected memory
79 ctx->fProtectedContext = /*isProtected ? GrProtected::kYes :*/ GrProtected::kNo;
80
81 return true;
82 }
83
84 }
85
86 #endif
87