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 | 95586ff | 2019-12-05 11:13:01 +0000 | [diff] [blame] | 18 | #include "common/Log.h" |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame] | 19 | #include "common/Platform.h" |
Stephen White | e8d9843 | 2021-02-24 16:53:53 +0000 | [diff] [blame] | 20 | #include "common/SystemUtils.h" |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 21 | #include "utils/BackendBinding.h" |
Corentin Wallez | 3a1746e | 2020-01-15 13:14:12 +0000 | [diff] [blame] | 22 | #include "utils/GLFWUtils.h" |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 23 | #include "utils/TerribleCommandBuffer.h" |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 24 | |
Corentin Wallez | 9649682 | 2019-10-15 11:44:38 +0000 | [diff] [blame] | 25 | #include <dawn/dawn_proc.h> |
Corentin Wallez | 3e371b1 | 2018-07-18 14:32:45 +0200 | [diff] [blame] | 26 | #include <dawn/dawn_wsi.h> |
Corentin Wallez | dcb71a1 | 2018-08-02 22:27:57 +0200 | [diff] [blame] | 27 | #include <dawn_native/DawnNative.h> |
Austin Eng | e2c8513 | 2019-02-11 21:50:16 +0000 | [diff] [blame] | 28 | #include <dawn_wire/WireClient.h> |
| 29 | #include <dawn_wire/WireServer.h> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 30 | #include "GLFW/glfw3.h" |
| 31 | |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 33 | #include <cstring> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 34 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 35 | void PrintDeviceError(WGPUErrorType errorType, const char* message, void*) { |
Corentin Wallez | 95586ff | 2019-12-05 11:13:01 +0000 | [diff] [blame] | 36 | const char* errorTypeName = ""; |
Austin Eng | cb0cb65 | 2019-08-27 21:41:56 +0000 | [diff] [blame] | 37 | switch (errorType) { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 38 | case WGPUErrorType_Validation: |
Corentin Wallez | 95586ff | 2019-12-05 11:13:01 +0000 | [diff] [blame] | 39 | errorTypeName = "Validation"; |
Austin Eng | cb0cb65 | 2019-08-27 21:41:56 +0000 | [diff] [blame] | 40 | break; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 41 | case WGPUErrorType_OutOfMemory: |
Corentin Wallez | 95586ff | 2019-12-05 11:13:01 +0000 | [diff] [blame] | 42 | errorTypeName = "Out of memory"; |
Austin Eng | cb0cb65 | 2019-08-27 21:41:56 +0000 | [diff] [blame] | 43 | break; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 44 | case WGPUErrorType_Unknown: |
Corentin Wallez | 95586ff | 2019-12-05 11:13:01 +0000 | [diff] [blame] | 45 | errorTypeName = "Unknown"; |
Austin Eng | cb0cb65 | 2019-08-27 21:41:56 +0000 | [diff] [blame] | 46 | break; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 47 | case WGPUErrorType_DeviceLost: |
Corentin Wallez | 95586ff | 2019-12-05 11:13:01 +0000 | [diff] [blame] | 48 | errorTypeName = "Device lost"; |
Austin Eng | cb0cb65 | 2019-08-27 21:41:56 +0000 | [diff] [blame] | 49 | break; |
| 50 | default: |
| 51 | UNREACHABLE(); |
| 52 | return; |
| 53 | } |
Corentin Wallez | dc3317d | 2019-12-06 18:21:39 +0000 | [diff] [blame] | 54 | dawn::ErrorLog() << errorTypeName << " error: " << message; |
Corentin Wallez | 4b410a3 | 2017-04-20 14:42:36 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Corentin Wallez | 52cbcb4 | 2018-01-19 12:52:03 -0500 | [diff] [blame] | 57 | void PrintGLFWError(int code, const char* message) { |
Corentin Wallez | dc3317d | 2019-12-06 18:21:39 +0000 | [diff] [blame] | 58 | dawn::ErrorLog() << "GLFW error: " << code << " - " << message; |
Corentin Wallez | 52cbcb4 | 2018-01-19 12:52:03 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 61 | enum class CmdBufType { |
| 62 | None, |
| 63 | Terrible, |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 64 | // TODO(cwallez@chromium.org): double terrible cmdbuf |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 65 | }; |
| 66 | |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 67 | // Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on |
| 68 | // their respective platforms, and Vulkan is preferred to OpenGL |
Corentin Wallez | f1ded9b | 2018-07-18 13:52:46 +0200 | [diff] [blame] | 69 | #if defined(DAWN_ENABLE_BACKEND_D3D12) |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 70 | static wgpu::BackendType backendType = wgpu::BackendType::D3D12; |
Corentin Wallez | f1ded9b | 2018-07-18 13:52:46 +0200 | [diff] [blame] | 71 | #elif defined(DAWN_ENABLE_BACKEND_METAL) |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 72 | static wgpu::BackendType backendType = wgpu::BackendType::Metal; |
Corentin Wallez | f1ded9b | 2018-07-18 13:52:46 +0200 | [diff] [blame] | 73 | #elif defined(DAWN_ENABLE_BACKEND_VULKAN) |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 74 | static wgpu::BackendType backendType = wgpu::BackendType::Vulkan; |
| 75 | #elif defined(DAWN_ENABLE_BACKEND_OPENGL) |
| 76 | static wgpu::BackendType backendType = wgpu::BackendType::OpenGL; |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame] | 77 | #else |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 78 | # error |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame] | 79 | #endif |
| 80 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 81 | static CmdBufType cmdBufType = CmdBufType::Terrible; |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 82 | static std::unique_ptr<dawn_native::Instance> instance; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 83 | static utils::BackendBinding* binding = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 84 | |
| 85 | static GLFWwindow* window = nullptr; |
| 86 | |
Austin Eng | e2c8513 | 2019-02-11 21:50:16 +0000 | [diff] [blame] | 87 | static dawn_wire::WireServer* wireServer = nullptr; |
| 88 | static dawn_wire::WireClient* wireClient = nullptr; |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 89 | static utils::TerribleCommandBuffer* c2sBuf = nullptr; |
| 90 | static utils::TerribleCommandBuffer* s2cBuf = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 91 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 92 | wgpu::Device CreateCppDawnDevice() { |
Stephen White | e8d9843 | 2021-02-24 16:53:53 +0000 | [diff] [blame] | 93 | if (GetEnvironmentVar("ANGLE_DEFAULT_PLATFORM").empty()) { |
| 94 | SetEnvironmentVar("ANGLE_DEFAULT_PLATFORM", "swiftshader"); |
| 95 | } |
| 96 | |
Corentin Wallez | 52cbcb4 | 2018-01-19 12:52:03 -0500 | [diff] [blame] | 97 | glfwSetErrorCallback(PrintGLFWError); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 98 | if (!glfwInit()) { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 99 | return wgpu::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 102 | // Create the test window and discover adapters using it (esp. for OpenGL) |
| 103 | utils::SetupGLFWWindowHintsForBackend(backendType); |
Corentin Wallez | 47a6a94 | 2020-10-17 23:31:37 +0000 | [diff] [blame] | 104 | glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE); |
Corentin Wallez | 6ed9cbf | 2018-07-18 15:32:04 +0200 | [diff] [blame] | 105 | window = glfwCreateWindow(640, 480, "Dawn window", nullptr, nullptr); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 106 | if (!window) { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 107 | return wgpu::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 110 | instance = std::make_unique<dawn_native::Instance>(); |
| 111 | utils::DiscoverAdapter(instance.get(), window, backendType); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 112 | |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 113 | // Get an adapter for the backend to use, and create the device. |
| 114 | dawn_native::Adapter backendAdapter; |
| 115 | { |
| 116 | std::vector<dawn_native::Adapter> adapters = instance->GetAdapters(); |
| 117 | auto adapterIt = std::find_if(adapters.begin(), adapters.end(), |
| 118 | [](const dawn_native::Adapter adapter) -> bool { |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 119 | wgpu::AdapterProperties properties; |
| 120 | adapter.GetProperties(&properties); |
| 121 | return properties.backendType == backendType; |
| 122 | }); |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 123 | ASSERT(adapterIt != adapters.end()); |
| 124 | backendAdapter = *adapterIt; |
| 125 | } |
| 126 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 127 | WGPUDevice backendDevice = backendAdapter.CreateDevice(); |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 128 | DawnProcTable backendProcs = dawn_native::GetProcs(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 129 | |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 130 | binding = utils::CreateBinding(backendType, window, backendDevice); |
| 131 | if (binding == nullptr) { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 132 | return wgpu::Device(); |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Choose whether to use the backend procs and devices directly, or set up the wire. |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 136 | WGPUDevice cDevice = nullptr; |
Austin Eng | 45f9730 | 2019-03-11 16:52:42 +0000 | [diff] [blame] | 137 | DawnProcTable procs; |
Corentin Wallez | bb5696b | 2019-02-12 15:48:15 +0000 | [diff] [blame] | 138 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 139 | switch (cmdBufType) { |
| 140 | case CmdBufType::None: |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 141 | procs = backendProcs; |
| 142 | cDevice = backendDevice; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 143 | break; |
| 144 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 145 | case CmdBufType::Terrible: { |
| 146 | c2sBuf = new utils::TerribleCommandBuffer(); |
| 147 | s2cBuf = new utils::TerribleCommandBuffer(); |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 148 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 149 | dawn_wire::WireServerDescriptor serverDesc = {}; |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 150 | serverDesc.procs = &backendProcs; |
| 151 | serverDesc.serializer = s2cBuf; |
Austin Eng | 6a5418a | 2019-07-19 16:01:48 +0000 | [diff] [blame] | 152 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 153 | wireServer = new dawn_wire::WireServer(serverDesc); |
| 154 | c2sBuf->SetHandler(wireServer); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 155 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 156 | dawn_wire::WireClientDescriptor clientDesc = {}; |
| 157 | clientDesc.serializer = c2sBuf; |
Austin Eng | 6a5418a | 2019-07-19 16:01:48 +0000 | [diff] [blame] | 158 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 159 | wireClient = new dawn_wire::WireClient(clientDesc); |
Austin Eng | 8750913 | 2020-10-16 15:21:16 +0000 | [diff] [blame] | 160 | procs = dawn_wire::client::GetProcs(); |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 161 | s2cBuf->SetHandler(wireClient); |
Austin Eng | 7fe5aa2 | 2021-02-05 23:36:30 +0000 | [diff] [blame] | 162 | |
| 163 | auto deviceReservation = wireClient->ReserveDevice(); |
| 164 | wireServer->InjectDevice(backendDevice, deviceReservation.id, |
| 165 | deviceReservation.generation); |
| 166 | |
| 167 | cDevice = deviceReservation.device; |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 168 | } break; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Corentin Wallez | 9649682 | 2019-10-15 11:44:38 +0000 | [diff] [blame] | 171 | dawnProcSetProcs(&procs); |
Austin Eng | 45ea7e6 | 2019-08-27 21:43:56 +0000 | [diff] [blame] | 172 | procs.deviceSetUncapturedErrorCallback(cDevice, PrintDeviceError, nullptr); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 173 | return wgpu::Device::Acquire(cDevice); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 176 | uint64_t GetSwapChainImplementation() { |
| 177 | return binding->GetSwapChainImplementation(); |
| 178 | } |
| 179 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 180 | wgpu::TextureFormat GetPreferredSwapChainTextureFormat() { |
Corentin Wallez | b91022d | 2018-01-19 12:52:25 -0500 | [diff] [blame] | 181 | DoFlush(); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 182 | return static_cast<wgpu::TextureFormat>(binding->GetPreferredSwapChainTextureFormat()); |
Corentin Wallez | 2e31e8f | 2017-09-21 12:54:53 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 185 | wgpu::SwapChain GetSwapChain(const wgpu::Device& device) { |
| 186 | wgpu::SwapChainDescriptor swapChainDesc; |
Corentin Wallez | 7be2a71 | 2019-02-15 11:15:58 +0000 | [diff] [blame] | 187 | swapChainDesc.implementation = GetSwapChainImplementation(); |
Corentin Wallez | d87e676 | 2020-01-23 17:20:38 +0000 | [diff] [blame] | 188 | return device.CreateSwapChain(nullptr, &swapChainDesc); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 191 | wgpu::TextureView CreateDefaultDepthStencilView(const wgpu::Device& device) { |
| 192 | wgpu::TextureDescriptor descriptor; |
| 193 | descriptor.dimension = wgpu::TextureDimension::e2D; |
Corentin Wallez | 29353d6 | 2018-09-18 12:49:22 +0000 | [diff] [blame] | 194 | descriptor.size.width = 640; |
| 195 | descriptor.size.height = 480; |
shrekshao | b00de7f | 2021-03-22 21:12:36 +0000 | [diff] [blame^] | 196 | descriptor.size.depthOrArrayLayers = 1; |
Jiawei Shao | 8bff3b2 | 2018-12-12 09:27:16 +0000 | [diff] [blame] | 197 | descriptor.sampleCount = 1; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 198 | descriptor.format = wgpu::TextureFormat::Depth24PlusStencil8; |
Jiawei Shao | 2030166 | 2019-02-21 00:45:19 +0000 | [diff] [blame] | 199 | descriptor.mipLevelCount = 1; |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 200 | descriptor.usage = wgpu::TextureUsage::RenderAttachment; |
Jiawei Shao | 425428f | 2018-08-27 08:44:48 +0800 | [diff] [blame] | 201 | auto depthStencilTexture = device.CreateTexture(&descriptor); |
Kai Ninomiya | 4078ed8 | 2019-08-27 17:56:23 +0000 | [diff] [blame] | 202 | return depthStencilTexture.CreateView(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 205 | bool InitSample(int argc, const char** argv) { |
Corentin Wallez | 862f884 | 2018-06-07 13:03:29 +0200 | [diff] [blame] | 206 | for (int i = 1; i < argc; i++) { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 207 | if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) { |
| 208 | i++; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 209 | if (i < argc && std::string("d3d12") == argv[i]) { |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 210 | backendType = wgpu::BackendType::D3D12; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 211 | continue; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 212 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 213 | if (i < argc && std::string("metal") == argv[i]) { |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 214 | backendType = wgpu::BackendType::Metal; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 215 | continue; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 216 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 217 | if (i < argc && std::string("null") == argv[i]) { |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 218 | backendType = wgpu::BackendType::Null; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 219 | continue; |
| 220 | } |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 221 | if (i < argc && std::string("opengl") == argv[i]) { |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 222 | backendType = wgpu::BackendType::OpenGL; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 223 | continue; |
| 224 | } |
Stephen White | 70102b7 | 2020-11-24 20:57:23 +0000 | [diff] [blame] | 225 | if (i < argc && std::string("opengles") == argv[i]) { |
| 226 | backendType = wgpu::BackendType::OpenGLES; |
| 227 | continue; |
| 228 | } |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 229 | if (i < argc && std::string("vulkan") == argv[i]) { |
Corentin Wallez | f12c9db | 2020-01-10 13:28:18 +0000 | [diff] [blame] | 230 | backendType = wgpu::BackendType::Vulkan; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 231 | continue; |
| 232 | } |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 233 | fprintf(stderr, |
Stephen White | 70102b7 | 2020-11-24 20:57:23 +0000 | [diff] [blame] | 234 | "--backend expects a backend name (opengl, opengles, metal, d3d12, null, " |
| 235 | "vulkan)\n"); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 236 | return false; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 237 | } |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 238 | if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 239 | i++; |
| 240 | if (i < argc && std::string("none") == argv[i]) { |
| 241 | cmdBufType = CmdBufType::None; |
| 242 | continue; |
| 243 | } |
| 244 | if (i < argc && std::string("terrible") == argv[i]) { |
| 245 | cmdBufType = CmdBufType::Terrible; |
| 246 | continue; |
| 247 | } |
| 248 | fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n"); |
| 249 | return false; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 250 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 251 | if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) { |
| 252 | printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]); |
Jiawei Shao | d41aa43 | 2021-03-15 08:02:53 +0000 | [diff] [blame] | 253 | printf(" BACKEND is one of: d3d12, metal, null, opengl, opengles, vulkan\n"); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 254 | printf(" COMMAND_BUFFER is one of: none, terrible\n"); |
| 255 | return false; |
| 256 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 257 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 261 | void DoFlush() { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 262 | if (cmdBufType == CmdBufType::Terrible) { |
Corentin Wallez | a5696c7 | 2018-08-13 08:24:01 +0200 | [diff] [blame] | 263 | bool c2sSuccess = c2sBuf->Flush(); |
| 264 | bool s2cSuccess = s2cBuf->Flush(); |
| 265 | |
| 266 | ASSERT(c2sSuccess && s2cSuccess); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 267 | } |
| 268 | glfwPollEvents(); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 269 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 270 | |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 271 | bool ShouldQuit() { |
| 272 | return glfwWindowShouldClose(window); |
| 273 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 274 | |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 275 | GLFWwindow* GetGLFWWindow() { |
| 276 | return window; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 277 | } |