Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 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 | |
Corentin Wallez | b91022d | 2018-01-19 12:52:25 -0500 | [diff] [blame] | 15 | #include "SampleUtils.h" |
| 16 | |
Kai Ninomiya | 21006bb | 2018-06-20 18:54:18 -0700 | [diff] [blame] | 17 | #include "common/Assert.h" |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame] | 18 | #include "common/Platform.h" |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 19 | #include "utils/BackendBinding.h" |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 20 | #include "utils/TerribleCommandBuffer.h" |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 21 | |
Corentin Wallez | 046cb46 | 2018-07-18 14:28:38 +0200 | [diff] [blame] | 22 | #include <dawn/dawn.h> |
| 23 | #include <dawn/dawncpp.h> |
Corentin Wallez | 3e371b1 | 2018-07-18 14:32:45 +0200 | [diff] [blame] | 24 | #include <dawn/dawn_wsi.h> |
Corentin Wallez | dcb71a1 | 2018-08-02 22:27:57 +0200 | [diff] [blame] | 25 | #include <dawn_native/DawnNative.h> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 26 | #include "GLFW/glfw3.h" |
| 27 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 28 | #include <cstring> |
| 29 | #include <iostream> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 30 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 31 | void PrintDeviceError(const char* message, dawn::CallbackUserdata) { |
Corentin Wallez | 4b410a3 | 2017-04-20 14:42:36 -0400 | [diff] [blame] | 32 | std::cout << "Device error: " << message << std::endl; |
| 33 | } |
| 34 | |
Corentin Wallez | 52cbcb4 | 2018-01-19 12:52:03 -0500 | [diff] [blame] | 35 | void PrintGLFWError(int code, const char* message) { |
| 36 | std::cout << "GLFW error: " << code << " - " << message << std::endl; |
| 37 | } |
| 38 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 39 | enum class CmdBufType { |
| 40 | None, |
| 41 | Terrible, |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 42 | //TODO(cwallez@chromium.org) double terrible cmdbuf |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 45 | // Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on |
| 46 | // their respective platforms, and Vulkan is preferred to OpenGL |
Corentin Wallez | f1ded9b | 2018-07-18 13:52:46 +0200 | [diff] [blame] | 47 | #if defined(DAWN_ENABLE_BACKEND_D3D12) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 48 | static utils::BackendType backendType = utils::BackendType::D3D12; |
Corentin Wallez | f1ded9b | 2018-07-18 13:52:46 +0200 | [diff] [blame] | 49 | #elif defined(DAWN_ENABLE_BACKEND_METAL) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 50 | static utils::BackendType backendType = utils::BackendType::Metal; |
Corentin Wallez | f1ded9b | 2018-07-18 13:52:46 +0200 | [diff] [blame] | 51 | #elif defined(DAWN_ENABLE_BACKEND_OPENGL) |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 52 | static utils::BackendType backendType = utils::BackendType::OpenGL; |
Corentin Wallez | f1ded9b | 2018-07-18 13:52:46 +0200 | [diff] [blame] | 53 | #elif defined(DAWN_ENABLE_BACKEND_VULKAN) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 54 | static utils::BackendType backendType = utils::BackendType::Vulkan; |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame] | 55 | #else |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 56 | #error |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame] | 57 | #endif |
| 58 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 59 | static CmdBufType cmdBufType = CmdBufType::Terrible; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 60 | static utils::BackendBinding* binding = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 61 | |
| 62 | static GLFWwindow* window = nullptr; |
| 63 | |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 64 | static dawn_wire::CommandHandler* wireServer = nullptr; |
| 65 | static dawn_wire::CommandHandler* wireClient = nullptr; |
| 66 | static utils::TerribleCommandBuffer* c2sBuf = nullptr; |
| 67 | static utils::TerribleCommandBuffer* s2cBuf = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 68 | |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 69 | dawn::Device CreateCppDawnDevice() { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 70 | binding = utils::CreateBinding(backendType); |
| 71 | if (binding == nullptr) { |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 72 | return dawn::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Corentin Wallez | 52cbcb4 | 2018-01-19 12:52:03 -0500 | [diff] [blame] | 75 | glfwSetErrorCallback(PrintGLFWError); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 76 | if (!glfwInit()) { |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 77 | return dawn::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | binding->SetupGLFWWindowHints(); |
Corentin Wallez | 6ed9cbf | 2018-07-18 15:32:04 +0200 | [diff] [blame] | 81 | window = glfwCreateWindow(640, 480, "Dawn window", nullptr, nullptr); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 82 | if (!window) { |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 83 | return dawn::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | binding->SetWindow(window); |
| 87 | |
Corentin Wallez | dcb71a1 | 2018-08-02 22:27:57 +0200 | [diff] [blame] | 88 | dawnDevice backendDevice = binding->CreateDevice(); |
| 89 | dawnProcTable backendProcs = dawn_native::GetProcs(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 90 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 91 | dawnDevice cDevice = nullptr; |
Corentin Wallez | be5ca38 | 2018-07-18 15:15:07 +0200 | [diff] [blame] | 92 | dawnProcTable procs; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 93 | switch (cmdBufType) { |
| 94 | case CmdBufType::None: |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 95 | procs = backendProcs; |
| 96 | cDevice = backendDevice; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 97 | break; |
| 98 | |
| 99 | case CmdBufType::Terrible: |
| 100 | { |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 101 | c2sBuf = new utils::TerribleCommandBuffer(); |
| 102 | s2cBuf = new utils::TerribleCommandBuffer(); |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 103 | |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 104 | wireServer = dawn_wire::NewServerCommandHandler(backendDevice, backendProcs, s2cBuf); |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 105 | c2sBuf->SetHandler(wireServer); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 106 | |
Corentin Wallez | b1669e3 | 2018-07-18 15:12:52 +0200 | [diff] [blame] | 107 | dawnDevice clientDevice; |
Corentin Wallez | be5ca38 | 2018-07-18 15:15:07 +0200 | [diff] [blame] | 108 | dawnProcTable clientProcs; |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 109 | wireClient = dawn_wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf); |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 110 | s2cBuf->SetHandler(wireClient); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 111 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 112 | procs = clientProcs; |
| 113 | cDevice = clientDevice; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | } |
| 117 | |
Corentin Wallez | be5ca38 | 2018-07-18 15:15:07 +0200 | [diff] [blame] | 118 | dawnSetProcs(&procs); |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 119 | procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0); |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 120 | return dawn::Device::Acquire(cDevice); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 123 | uint64_t GetSwapChainImplementation() { |
| 124 | return binding->GetSwapChainImplementation(); |
| 125 | } |
| 126 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 127 | dawn::TextureFormat GetPreferredSwapChainTextureFormat() { |
Corentin Wallez | b91022d | 2018-01-19 12:52:25 -0500 | [diff] [blame] | 128 | DoFlush(); |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 129 | return static_cast<dawn::TextureFormat>(binding->GetPreferredSwapChainTextureFormat()); |
Corentin Wallez | 2e31e8f | 2017-09-21 12:54:53 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 132 | dawn::SwapChain GetSwapChain(const dawn::Device &device) { |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 133 | return device.CreateSwapChainBuilder() |
| 134 | .SetImplementation(GetSwapChainImplementation()) |
| 135 | .GetResult(); |
| 136 | } |
| 137 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 138 | dawn::TextureView CreateDefaultDepthStencilView(const dawn::Device& device) { |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 139 | auto depthStencilTexture = device.CreateTextureBuilder() |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 140 | .SetDimension(dawn::TextureDimension::e2D) |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 141 | .SetExtent(640, 480, 1) |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 142 | .SetFormat(dawn::TextureFormat::D32FloatS8Uint) |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 143 | .SetMipLevels(1) |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 144 | .SetAllowedUsage(dawn::TextureUsageBit::OutputAttachment) |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 145 | .GetResult(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 146 | return depthStencilTexture.CreateTextureViewBuilder() |
| 147 | .GetResult(); |
| 148 | } |
| 149 | |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 150 | void GetNextRenderPassDescriptor(const dawn::Device& device, |
| 151 | const dawn::SwapChain& swapchain, |
| 152 | const dawn::TextureView& depthStencilView, |
| 153 | dawn::Texture* backbuffer, |
| 154 | dawn::RenderPassDescriptor* info) { |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 155 | *backbuffer = swapchain.GetNextTexture(); |
| 156 | auto backbufferView = backbuffer->CreateTextureViewBuilder().GetResult(); |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 157 | *info = device.CreateRenderPassDescriptorBuilder() |
Corentin Wallez | 4828d92 | 2018-07-18 13:45:46 +0200 | [diff] [blame] | 158 | .SetColorAttachment(0, backbufferView, dawn::LoadOp::Clear) |
| 159 | .SetDepthStencilAttachment(depthStencilView, dawn::LoadOp::Clear, dawn::LoadOp::Clear) |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 160 | .GetResult(); |
| 161 | } |
| 162 | |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 163 | bool InitSample(int argc, const char** argv) { |
Corentin Wallez | 862f884 | 2018-06-07 13:03:29 +0200 | [diff] [blame] | 164 | for (int i = 1; i < argc; i++) { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 165 | if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) { |
| 166 | i++; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 167 | if (i < argc && std::string("d3d12") == argv[i]) { |
| 168 | backendType = utils::BackendType::D3D12; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 169 | continue; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 170 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 171 | if (i < argc && std::string("metal") == argv[i]) { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 172 | backendType = utils::BackendType::Metal; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 173 | continue; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 174 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 175 | if (i < argc && std::string("null") == argv[i]) { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 176 | backendType = utils::BackendType::Null; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 177 | continue; |
| 178 | } |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 179 | if (i < argc && std::string("opengl") == argv[i]) { |
| 180 | backendType = utils::BackendType::OpenGL; |
| 181 | continue; |
| 182 | } |
| 183 | if (i < argc && std::string("vulkan") == argv[i]) { |
| 184 | backendType = utils::BackendType::Vulkan; |
| 185 | continue; |
| 186 | } |
| 187 | fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null, vulkan)\n"); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 188 | return false; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 189 | } |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 190 | if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 191 | i++; |
| 192 | if (i < argc && std::string("none") == argv[i]) { |
| 193 | cmdBufType = CmdBufType::None; |
| 194 | continue; |
| 195 | } |
| 196 | if (i < argc && std::string("terrible") == argv[i]) { |
| 197 | cmdBufType = CmdBufType::Terrible; |
| 198 | continue; |
| 199 | } |
| 200 | fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n"); |
| 201 | return false; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 202 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 203 | if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) { |
| 204 | printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]); |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 205 | printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n"); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 206 | printf(" COMMAND_BUFFER is one of: none, terrible\n"); |
| 207 | return false; |
| 208 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 209 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 213 | void DoFlush() { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 214 | if (cmdBufType == CmdBufType::Terrible) { |
Corentin Wallez | a5696c7 | 2018-08-13 08:24:01 +0200 | [diff] [blame^] | 215 | bool c2sSuccess = c2sBuf->Flush(); |
| 216 | bool s2cSuccess = s2cBuf->Flush(); |
| 217 | |
| 218 | ASSERT(c2sSuccess && s2cSuccess); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 219 | } |
| 220 | glfwPollEvents(); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 221 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 222 | |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 223 | bool ShouldQuit() { |
| 224 | return glfwWindowShouldClose(window); |
| 225 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 226 | |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 227 | GLFWwindow* GetGLFWWindow() { |
| 228 | return window; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 229 | } |