blob: 574c7fd36ef5b76c72a07beea97c60c66e3f8869 [file] [log] [blame]
Jim Van Verth03b8ab22020-02-24 11:36:15 -05001/*
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>
Jim Van Verth03b8ab22020-02-24 11:36:15 -050012
13#include "include/gpu/d3d/GrD3DBackendContext.h"
14
15namespace sk_gpu_test {
16
17void 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
37bool CreateD3DBackendContext(GrD3DBackendContext* ctx,
38 bool isProtected) {
39#if defined(SK_ENABLE_D3D_DEBUG_LAYER)
40 // Enable the D3D12 debug layer.
41 {
Jim Van Verth5fba9ae2020-09-21 17:18:04 -040042 gr_cp<ID3D12Debug> debugController;
Jim Van Verth03b8ab22020-02-24 11:36:15 -050043 if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
44 {
45 debugController->EnableDebugLayer();
46 }
47 }
48#endif
49 // Create the device
Jim Van Verth5fba9ae2020-09-21 17:18:04 -040050 gr_cp<IDXGIFactory4> factory;
Jim Van Verth03b8ab22020-02-24 11:36:15 -050051 if (!SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) {
52 return false;
53 }
54
Jim Van Verth5fba9ae2020-09-21 17:18:04 -040055 gr_cp<IDXGIAdapter1> hardwareAdapter;
56 get_hardware_adapter(factory.get(), &hardwareAdapter);
Jim Van Verth03b8ab22020-02-24 11:36:15 -050057
Jim Van Verth5fba9ae2020-09-21 17:18:04 -040058 gr_cp<ID3D12Device> device;
59 if (!SUCCEEDED(D3D12CreateDevice(hardwareAdapter.get(),
Jim Van Verth9aa9a682020-04-01 10:13:48 -040060 D3D_FEATURE_LEVEL_11_0,
61 IID_PPV_ARGS(&device)))) {
Jim Van Verth03b8ab22020-02-24 11:36:15 -050062 return false;
63 }
64
65 // Create the command queue
Jim Van Verth5fba9ae2020-09-21 17:18:04 -040066 gr_cp<ID3D12CommandQueue> queue;
Jim Van Verth03b8ab22020-02-24 11:36:15 -050067 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
Jim Van Verth8ec13302020-02-26 12:59:56 -050075 ctx->fAdapter = hardwareAdapter;
Jim Van Verth03b8ab22020-02-24 11:36:15 -050076 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