Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 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> |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 12 | |
| 13 | #include "include/gpu/d3d/GrD3DBackendContext.h" |
| 14 | |
| 15 | namespace sk_gpu_test { |
| 16 | |
| 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 | |
| 37 | bool CreateD3DBackendContext(GrD3DBackendContext* ctx, |
| 38 | bool isProtected) { |
| 39 | #if defined(SK_ENABLE_D3D_DEBUG_LAYER) |
| 40 | // Enable the D3D12 debug layer. |
| 41 | { |
Jim Van Verth | 5fba9ae | 2020-09-21 17:18:04 -0400 | [diff] [blame] | 42 | gr_cp<ID3D12Debug> debugController; |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 43 | if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) |
| 44 | { |
| 45 | debugController->EnableDebugLayer(); |
| 46 | } |
| 47 | } |
| 48 | #endif |
| 49 | // Create the device |
Jim Van Verth | 5fba9ae | 2020-09-21 17:18:04 -0400 | [diff] [blame] | 50 | gr_cp<IDXGIFactory4> factory; |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 51 | if (!SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) { |
| 52 | return false; |
| 53 | } |
| 54 | |
Jim Van Verth | 5fba9ae | 2020-09-21 17:18:04 -0400 | [diff] [blame] | 55 | gr_cp<IDXGIAdapter1> hardwareAdapter; |
| 56 | get_hardware_adapter(factory.get(), &hardwareAdapter); |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 57 | |
Jim Van Verth | 5fba9ae | 2020-09-21 17:18:04 -0400 | [diff] [blame] | 58 | gr_cp<ID3D12Device> device; |
| 59 | if (!SUCCEEDED(D3D12CreateDevice(hardwareAdapter.get(), |
Jim Van Verth | 9aa9a68 | 2020-04-01 10:13:48 -0400 | [diff] [blame] | 60 | D3D_FEATURE_LEVEL_11_0, |
| 61 | IID_PPV_ARGS(&device)))) { |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // Create the command queue |
Jim Van Verth | 5fba9ae | 2020-09-21 17:18:04 -0400 | [diff] [blame] | 66 | gr_cp<ID3D12CommandQueue> queue; |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 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 | |
Jim Van Verth | 8ec1330 | 2020-02-26 12:59:56 -0500 | [diff] [blame] | 75 | ctx->fAdapter = hardwareAdapter; |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 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 |