• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dawn_native/d3d12/NativeSwapChainImplD3D12.h"
16 
17 #include "common/Assert.h"
18 #include "dawn_native/d3d12/DeviceD3D12.h"
19 #include "dawn_native/d3d12/TextureD3D12.h"
20 
21 namespace dawn_native { namespace d3d12 {
22 
23     namespace {
D3D12SwapChainBufferUsage(WGPUTextureUsage allowedUsages)24         DXGI_USAGE D3D12SwapChainBufferUsage(WGPUTextureUsage allowedUsages) {
25             DXGI_USAGE usage = DXGI_CPU_ACCESS_NONE;
26             if (allowedUsages & WGPUTextureUsage_TextureBinding) {
27                 usage |= DXGI_USAGE_SHADER_INPUT;
28             }
29             if (allowedUsages & WGPUTextureUsage_StorageBinding) {
30                 usage |= DXGI_USAGE_UNORDERED_ACCESS;
31             }
32             if (allowedUsages & WGPUTextureUsage_RenderAttachment) {
33                 usage |= DXGI_USAGE_RENDER_TARGET_OUTPUT;
34             }
35             return usage;
36         }
37 
38         static constexpr unsigned int kFrameCount = 3;
39     }  // anonymous namespace
40 
NativeSwapChainImpl(Device * device,HWND window)41     NativeSwapChainImpl::NativeSwapChainImpl(Device* device, HWND window)
42         : mWindow(window), mDevice(device), mInterval(1) {
43     }
44 
~NativeSwapChainImpl()45     NativeSwapChainImpl::~NativeSwapChainImpl() {
46     }
47 
Init(DawnWSIContextD3D12 *)48     void NativeSwapChainImpl::Init(DawnWSIContextD3D12* /*context*/) {
49     }
50 
Configure(WGPUTextureFormat format,WGPUTextureUsage usage,uint32_t width,uint32_t height)51     DawnSwapChainError NativeSwapChainImpl::Configure(WGPUTextureFormat format,
52                                                       WGPUTextureUsage usage,
53                                                       uint32_t width,
54                                                       uint32_t height) {
55         ASSERT(width > 0);
56         ASSERT(height > 0);
57         ASSERT(format == static_cast<WGPUTextureFormat>(GetPreferredFormat()));
58 
59         ComPtr<IDXGIFactory4> factory = mDevice->GetFactory();
60         ComPtr<ID3D12CommandQueue> queue = mDevice->GetCommandQueue();
61 
62         mInterval = mDevice->IsToggleEnabled(Toggle::TurnOffVsync) == true ? 0 : 1;
63 
64         // Create the D3D12 swapchain, assuming only two buffers for now
65         DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
66         swapChainDesc.Width = width;
67         swapChainDesc.Height = height;
68         swapChainDesc.Format = D3D12TextureFormat(GetPreferredFormat());
69         swapChainDesc.BufferUsage = D3D12SwapChainBufferUsage(usage);
70         swapChainDesc.BufferCount = kFrameCount;
71         swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
72         swapChainDesc.SampleDesc.Count = 1;
73         swapChainDesc.SampleDesc.Quality = 0;
74 
75         ComPtr<IDXGISwapChain1> swapChain1;
76         ASSERT_SUCCESS(factory->CreateSwapChainForHwnd(queue.Get(), mWindow, &swapChainDesc,
77                                                        nullptr, nullptr, &swapChain1));
78 
79         ASSERT_SUCCESS(swapChain1.As(&mSwapChain));
80 
81         // Gather the resources that will be used to present to the swapchain
82         mBuffers.resize(kFrameCount);
83         for (uint32_t i = 0; i < kFrameCount; ++i) {
84             ASSERT_SUCCESS(mSwapChain->GetBuffer(i, IID_PPV_ARGS(&mBuffers[i])));
85         }
86 
87         // Set the initial serial of buffers to 0 so that we don't wait on them when they are first
88         // used
89         mBufferSerials.resize(kFrameCount, ExecutionSerial(0));
90 
91         return DAWN_SWAP_CHAIN_NO_ERROR;
92     }
93 
GetNextTexture(DawnSwapChainNextTexture * nextTexture)94     DawnSwapChainError NativeSwapChainImpl::GetNextTexture(DawnSwapChainNextTexture* nextTexture) {
95         mCurrentBuffer = mSwapChain->GetCurrentBackBufferIndex();
96         nextTexture->texture.ptr = mBuffers[mCurrentBuffer].Get();
97 
98         // TODO(crbug.com/dawn/269) Currently we force the CPU to wait for the GPU to be finished
99         // with the buffer. Ideally the synchronization should be all done on the GPU.
100         ASSERT(mDevice->WaitForSerial(mBufferSerials[mCurrentBuffer]).IsSuccess());
101 
102         return DAWN_SWAP_CHAIN_NO_ERROR;
103     }
104 
Present()105     DawnSwapChainError NativeSwapChainImpl::Present() {
106         // This assumes the texture has already been transition to the PRESENT state.
107 
108         ASSERT_SUCCESS(mSwapChain->Present(mInterval, 0));
109         // TODO(crbug.com/dawn/833): Make the serial ticking implicit.
110         ASSERT(mDevice->NextSerial().IsSuccess());
111 
112         mBufferSerials[mCurrentBuffer] = mDevice->GetPendingCommandSerial();
113         return DAWN_SWAP_CHAIN_NO_ERROR;
114     }
115 
GetPreferredFormat() const116     wgpu::TextureFormat NativeSwapChainImpl::GetPreferredFormat() const {
117         return wgpu::TextureFormat::RGBA8Unorm;
118     }
119 
120 }}  // namespace dawn_native::d3d12
121