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
13 #include "include/gpu/d3d/GrD3DBackendContext.h"
14
15 namespace sk_gpu_test {
16
get_hardware_adapter(IDXGIFactory4 * pFactory,IDXGIAdapter1 ** ppAdapter)17 void get_hardware_adapter(IDXGIFactory4* pFactory, IDXGIAdapter1** ppAdapter) {
18 *ppAdapter = nullptr;
19 for (UINT adapterIndex = 0; ; ++adapterIndex) {
20 IDXGIAdapter1* pAdapter = nullptr;
21 if (DXGI_ERROR_NOT_FOUND == pFactory->EnumAdapters1(adapterIndex, &pAdapter)) {
22 // No more adapters to enumerate.
23 break;
24 }
25
26 // Check to see if the adapter supports Direct3D 12, but don't create the
27 // actual device yet.
28 if (SUCCEEDED(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device),
29 nullptr))) {
30 *ppAdapter = pAdapter;
31 return;
32 }
33 pAdapter->Release();
34 }
35 }
36
CreateD3DBackendContext(GrD3DBackendContext * ctx,bool isProtected)37 bool CreateD3DBackendContext(GrD3DBackendContext* ctx,
38 bool isProtected) {
39 #if defined(SK_ENABLE_D3D_DEBUG_LAYER)
40 // Enable the D3D12 debug layer.
41 {
42 gr_cp<ID3D12Debug> debugController;
43 if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
44 {
45 debugController->EnableDebugLayer();
46 }
47 }
48 #endif
49 // Create the device
50 gr_cp<IDXGIFactory4> factory;
51 if (!SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) {
52 return false;
53 }
54
55 gr_cp<IDXGIAdapter1> hardwareAdapter;
56 get_hardware_adapter(factory.get(), &hardwareAdapter);
57
58 gr_cp<ID3D12Device> device;
59 if (!SUCCEEDED(D3D12CreateDevice(hardwareAdapter.get(),
60 D3D_FEATURE_LEVEL_11_0,
61 IID_PPV_ARGS(&device)))) {
62 return false;
63 }
64
65 // Create the command queue
66 gr_cp<ID3D12CommandQueue> queue;
67 D3D12_COMMAND_QUEUE_DESC queueDesc = {};
68 queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
69 queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
70
71 if (!SUCCEEDED(device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&queue)))) {
72 return false;
73 }
74
75 ctx->fAdapter = hardwareAdapter;
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