• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google Inc.
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/sk_app/DawnWindowContext.h"
9 #include "tools/sk_app/win/WindowContextFactory_win.h"
10 #include "dawn/webgpu_cpp.h"
11 #include "dawn/dawn_wsi.h"
12 #include "dawn_native/DawnNative.h"
13 #include "dawn_native/D3D12Backend.h"
14 
15 namespace sk_app {
16 
17 class DawnD3D12WindowContext : public DawnWindowContext {
18 public:
19     DawnD3D12WindowContext(HWND hwnd, const DisplayParams& params);
20     ~DawnD3D12WindowContext() override;
21     wgpu::Device onInitializeContext() override;
22     void onDestroyContext() override;
23     DawnSwapChainImplementation createSwapChainImplementation(
24             int width, int height, const DisplayParams& params) override;
25     void onSwapBuffers() override;
26 private:
27     HWND                 fWindow;
28 };
29 
30 // NOTE: this texture format must match the one in D3D12's swap chain impl
DawnD3D12WindowContext(HWND hwnd,const DisplayParams & params)31 DawnD3D12WindowContext::DawnD3D12WindowContext(HWND hwnd, const DisplayParams& params)
32     : DawnWindowContext(params, wgpu::TextureFormat::RGBA8Unorm)
33     , fWindow(hwnd) {
34     RECT rect;
35     GetClientRect(hwnd, &rect);
36     this->initializeContext(rect.right - rect.left, rect.bottom - rect.top);
37 }
38 
~DawnD3D12WindowContext()39 DawnD3D12WindowContext::~DawnD3D12WindowContext() {
40     this->destroyContext();
41 }
42 
createSwapChainImplementation(int width,int height,const DisplayParams & params)43 DawnSwapChainImplementation DawnD3D12WindowContext::createSwapChainImplementation(
44         int width, int height, const DisplayParams& params) {
45     return dawn_native::d3d12::CreateNativeSwapChainImpl(fDevice.Get(), fWindow);
46 }
47 
onInitializeContext()48 wgpu::Device DawnD3D12WindowContext::onInitializeContext() {
49     return this->createDevice(dawn_native::BackendType::D3D12);
50 }
51 
onDestroyContext()52 void DawnD3D12WindowContext::onDestroyContext() {
53 }
54 
onSwapBuffers()55 void DawnD3D12WindowContext::onSwapBuffers() {
56 }
57 
58 namespace window_context_factory {
59 
MakeDawnD3D12ForWin(HWND hwnd,const DisplayParams & params)60 std::unique_ptr<WindowContext> MakeDawnD3D12ForWin(HWND hwnd,
61                                                    const DisplayParams& params) {
62     std::unique_ptr<WindowContext> ctx(new DawnD3D12WindowContext(hwnd, params));
63     if (!ctx->isValid()) {
64         return nullptr;
65     }
66     return ctx;
67 }
68 
69 }
70 
71 }   //namespace sk_app
72