blob: 3dcf0e55020ff6e9817f350fdebab03838b21797 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//
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 Wallezb91022d2018-01-19 12:52:25 -050015#include "SampleUtils.h"
16
Kai Ninomiya21006bb2018-06-20 18:54:18 -070017#include "common/Assert.h"
Corentin Wallez95586ff2019-12-05 11:13:01 +000018#include "common/Log.h"
Corentin Wallez0b186b12017-07-12 12:56:05 -040019#include "common/Platform.h"
Corentin Wallez1bd219d2017-06-19 12:53:38 -040020#include "utils/BackendBinding.h"
Corentin Wallezbdc86772018-07-26 15:07:57 +020021#include "utils/TerribleCommandBuffer.h"
Corentin Wallez1bd219d2017-06-19 12:53:38 -040022
Corentin Wallez96496822019-10-15 11:44:38 +000023#include <dawn/dawn_proc.h>
Corentin Wallez3e371b12018-07-18 14:32:45 +020024#include <dawn/dawn_wsi.h>
Corentin Wallezdcb71a12018-08-02 22:27:57 +020025#include <dawn_native/DawnNative.h>
Austin Enge2c85132019-02-11 21:50:16 +000026#include <dawn_wire/WireClient.h>
27#include <dawn_wire/WireServer.h>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040028#include "GLFW/glfw3.h"
29
Corentin Wallezbb5696b2019-02-12 15:48:15 +000030#include <algorithm>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040031#include <cstring>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040032
Corentin Wallez04863c42019-10-25 11:36:47 +000033void PrintDeviceError(WGPUErrorType errorType, const char* message, void*) {
Corentin Wallez95586ff2019-12-05 11:13:01 +000034 const char* errorTypeName = "";
Austin Engcb0cb652019-08-27 21:41:56 +000035 switch (errorType) {
Corentin Wallez04863c42019-10-25 11:36:47 +000036 case WGPUErrorType_Validation:
Corentin Wallez95586ff2019-12-05 11:13:01 +000037 errorTypeName = "Validation";
Austin Engcb0cb652019-08-27 21:41:56 +000038 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000039 case WGPUErrorType_OutOfMemory:
Corentin Wallez95586ff2019-12-05 11:13:01 +000040 errorTypeName = "Out of memory";
Austin Engcb0cb652019-08-27 21:41:56 +000041 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000042 case WGPUErrorType_Unknown:
Corentin Wallez95586ff2019-12-05 11:13:01 +000043 errorTypeName = "Unknown";
Austin Engcb0cb652019-08-27 21:41:56 +000044 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000045 case WGPUErrorType_DeviceLost:
Corentin Wallez95586ff2019-12-05 11:13:01 +000046 errorTypeName = "Device lost";
Austin Engcb0cb652019-08-27 21:41:56 +000047 break;
48 default:
49 UNREACHABLE();
50 return;
51 }
Corentin Wallezdc3317d2019-12-06 18:21:39 +000052 dawn::ErrorLog() << errorTypeName << " error: " << message;
Corentin Wallez4b410a32017-04-20 14:42:36 -040053}
54
Corentin Wallez52cbcb42018-01-19 12:52:03 -050055void PrintGLFWError(int code, const char* message) {
Corentin Wallezdc3317d2019-12-06 18:21:39 +000056 dawn::ErrorLog() << "GLFW error: " << code << " - " << message;
Corentin Wallez52cbcb42018-01-19 12:52:03 -050057}
58
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040059enum class CmdBufType {
60 None,
61 Terrible,
Corentin Wallez682a8252017-05-09 15:34:13 +020062 //TODO(cwallez@chromium.org) double terrible cmdbuf
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040063};
64
Corentin Wallez275817a2017-07-12 12:43:24 -040065// Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on
66// their respective platforms, and Vulkan is preferred to OpenGL
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020067#if defined(DAWN_ENABLE_BACKEND_D3D12)
Corentin Wallez8c88e1d2019-02-05 12:17:20 +000068 static dawn_native::BackendType backendType = dawn_native::BackendType::D3D12;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020069#elif defined(DAWN_ENABLE_BACKEND_METAL)
Corentin Wallez8c88e1d2019-02-05 12:17:20 +000070 static dawn_native::BackendType backendType = dawn_native::BackendType::Metal;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020071#elif defined(DAWN_ENABLE_BACKEND_OPENGL)
Corentin Wallez8c88e1d2019-02-05 12:17:20 +000072 static dawn_native::BackendType backendType = dawn_native::BackendType::OpenGL;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020073#elif defined(DAWN_ENABLE_BACKEND_VULKAN)
Corentin Wallez8c88e1d2019-02-05 12:17:20 +000074 static dawn_native::BackendType backendType = dawn_native::BackendType::Vulkan;
Austin Engfc2bac72017-06-05 17:08:55 -040075#else
Corentin Wallez275817a2017-07-12 12:43:24 -040076 #error
Austin Engfc2bac72017-06-05 17:08:55 -040077#endif
78
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040079static CmdBufType cmdBufType = CmdBufType::Terrible;
Corentin Wallezbb5696b2019-02-12 15:48:15 +000080static std::unique_ptr<dawn_native::Instance> instance;
Corentin Wallez1bd219d2017-06-19 12:53:38 -040081static utils::BackendBinding* binding = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040082
83static GLFWwindow* window = nullptr;
84
Austin Enge2c85132019-02-11 21:50:16 +000085static dawn_wire::WireServer* wireServer = nullptr;
86static dawn_wire::WireClient* wireClient = nullptr;
Corentin Wallezbdc86772018-07-26 15:07:57 +020087static utils::TerribleCommandBuffer* c2sBuf = nullptr;
88static utils::TerribleCommandBuffer* s2cBuf = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040089
Corentin Wallez04863c42019-10-25 11:36:47 +000090wgpu::Device CreateCppDawnDevice() {
Corentin Wallez52cbcb42018-01-19 12:52:03 -050091 glfwSetErrorCallback(PrintGLFWError);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040092 if (!glfwInit()) {
Corentin Wallez04863c42019-10-25 11:36:47 +000093 return wgpu::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040094 }
95
Corentin Wallezbb5696b2019-02-12 15:48:15 +000096 // Create the test window and discover adapters using it (esp. for OpenGL)
97 utils::SetupGLFWWindowHintsForBackend(backendType);
Corentin Wallez6ed9cbf2018-07-18 15:32:04 +020098 window = glfwCreateWindow(640, 480, "Dawn window", nullptr, nullptr);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040099 if (!window) {
Corentin Wallez04863c42019-10-25 11:36:47 +0000100 return wgpu::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400101 }
102
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000103 instance = std::make_unique<dawn_native::Instance>();
104 utils::DiscoverAdapter(instance.get(), window, backendType);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400105
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000106 // Get an adapter for the backend to use, and create the device.
107 dawn_native::Adapter backendAdapter;
108 {
109 std::vector<dawn_native::Adapter> adapters = instance->GetAdapters();
110 auto adapterIt = std::find_if(adapters.begin(), adapters.end(),
111 [](const dawn_native::Adapter adapter) -> bool {
112 return adapter.GetBackendType() == backendType;
113 });
114 ASSERT(adapterIt != adapters.end());
115 backendAdapter = *adapterIt;
116 }
117
Corentin Wallez04863c42019-10-25 11:36:47 +0000118 WGPUDevice backendDevice = backendAdapter.CreateDevice();
Austin Eng45f97302019-03-11 16:52:42 +0000119 DawnProcTable backendProcs = dawn_native::GetProcs();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400120
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000121 binding = utils::CreateBinding(backendType, window, backendDevice);
122 if (binding == nullptr) {
Corentin Wallez04863c42019-10-25 11:36:47 +0000123 return wgpu::Device();
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000124 }
125
126 // Choose whether to use the backend procs and devices directly, or set up the wire.
Corentin Wallez04863c42019-10-25 11:36:47 +0000127 WGPUDevice cDevice = nullptr;
Austin Eng45f97302019-03-11 16:52:42 +0000128 DawnProcTable procs;
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000129
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400130 switch (cmdBufType) {
131 case CmdBufType::None:
Corentin Wallez583e9a82017-05-29 11:30:29 -0700132 procs = backendProcs;
133 cDevice = backendDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400134 break;
135
136 case CmdBufType::Terrible:
137 {
Corentin Wallezbdc86772018-07-26 15:07:57 +0200138 c2sBuf = new utils::TerribleCommandBuffer();
139 s2cBuf = new utils::TerribleCommandBuffer();
Corentin Wallez682a8252017-05-09 15:34:13 +0200140
Austin Eng6a5418a2019-07-19 16:01:48 +0000141 dawn_wire::WireServerDescriptor serverDesc = {};
142 serverDesc.device = backendDevice;
143 serverDesc.procs = &backendProcs;
144 serverDesc.serializer = s2cBuf;
145
146 wireServer = new dawn_wire::WireServer(serverDesc);
Corentin Wallez682a8252017-05-09 15:34:13 +0200147 c2sBuf->SetHandler(wireServer);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400148
Austin Eng6a5418a2019-07-19 16:01:48 +0000149 dawn_wire::WireClientDescriptor clientDesc = {};
150 clientDesc.serializer = c2sBuf;
151
152 wireClient = new dawn_wire::WireClient(clientDesc);
Corentin Wallez04863c42019-10-25 11:36:47 +0000153 WGPUDevice clientDevice = wireClient->GetDevice();
Austin Eng45f97302019-03-11 16:52:42 +0000154 DawnProcTable clientProcs = wireClient->GetProcs();
Corentin Wallez682a8252017-05-09 15:34:13 +0200155 s2cBuf->SetHandler(wireClient);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400156
Corentin Wallez583e9a82017-05-29 11:30:29 -0700157 procs = clientProcs;
158 cDevice = clientDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400159 }
160 break;
161 }
162
Corentin Wallez96496822019-10-15 11:44:38 +0000163 dawnProcSetProcs(&procs);
Austin Eng45ea7e62019-08-27 21:43:56 +0000164 procs.deviceSetUncapturedErrorCallback(cDevice, PrintDeviceError, nullptr);
Corentin Wallez04863c42019-10-25 11:36:47 +0000165 return wgpu::Device::Acquire(cDevice);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400166}
167
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700168uint64_t GetSwapChainImplementation() {
169 return binding->GetSwapChainImplementation();
170}
171
Corentin Wallez04863c42019-10-25 11:36:47 +0000172wgpu::TextureFormat GetPreferredSwapChainTextureFormat() {
Corentin Wallezb91022d2018-01-19 12:52:25 -0500173 DoFlush();
Corentin Wallez04863c42019-10-25 11:36:47 +0000174 return static_cast<wgpu::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
Corentin Wallez2e31e8f2017-09-21 12:54:53 -0400175}
176
Corentin Wallez04863c42019-10-25 11:36:47 +0000177wgpu::SwapChain GetSwapChain(const wgpu::Device& device) {
178 wgpu::SwapChainDescriptor swapChainDesc;
Corentin Wallez7be2a712019-02-15 11:15:58 +0000179 swapChainDesc.implementation = GetSwapChainImplementation();
180 return device.CreateSwapChain(&swapChainDesc);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700181}
182
Corentin Wallez04863c42019-10-25 11:36:47 +0000183wgpu::TextureView CreateDefaultDepthStencilView(const wgpu::Device& device) {
184 wgpu::TextureDescriptor descriptor;
185 descriptor.dimension = wgpu::TextureDimension::e2D;
Corentin Wallez29353d62018-09-18 12:49:22 +0000186 descriptor.size.width = 640;
187 descriptor.size.height = 480;
188 descriptor.size.depth = 1;
Jiawei Shao20301662019-02-21 00:45:19 +0000189 descriptor.arrayLayerCount = 1;
Jiawei Shao8bff3b22018-12-12 09:27:16 +0000190 descriptor.sampleCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +0000191 descriptor.format = wgpu::TextureFormat::Depth24PlusStencil8;
Jiawei Shao20301662019-02-21 00:45:19 +0000192 descriptor.mipLevelCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +0000193 descriptor.usage = wgpu::TextureUsage::OutputAttachment;
Jiawei Shao425428f2018-08-27 08:44:48 +0800194 auto depthStencilTexture = device.CreateTexture(&descriptor);
Kai Ninomiya4078ed82019-08-27 17:56:23 +0000195 return depthStencilTexture.CreateView();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700196}
197
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400198bool InitSample(int argc, const char** argv) {
Corentin Wallez862f8842018-06-07 13:03:29 +0200199 for (int i = 1; i < argc; i++) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400200 if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
201 i++;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400202 if (i < argc && std::string("d3d12") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000203 backendType = dawn_native::BackendType::D3D12;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400204 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400205 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400206 if (i < argc && std::string("metal") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000207 backendType = dawn_native::BackendType::Metal;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400208 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400209 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400210 if (i < argc && std::string("null") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000211 backendType = dawn_native::BackendType::Null;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400212 continue;
213 }
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400214 if (i < argc && std::string("opengl") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000215 backendType = dawn_native::BackendType::OpenGL;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400216 continue;
217 }
218 if (i < argc && std::string("vulkan") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000219 backendType = dawn_native::BackendType::Vulkan;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400220 continue;
221 }
222 fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null, vulkan)\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400223 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400224 }
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700225 if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400226 i++;
227 if (i < argc && std::string("none") == argv[i]) {
228 cmdBufType = CmdBufType::None;
229 continue;
230 }
231 if (i < argc && std::string("terrible") == argv[i]) {
232 cmdBufType = CmdBufType::Terrible;
233 continue;
234 }
235 fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n");
236 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400237 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400238 if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
239 printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400240 printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400241 printf(" COMMAND_BUFFER is one of: none, terrible\n");
242 return false;
243 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400244 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400245 return true;
246}
247
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700248void DoFlush() {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400249 if (cmdBufType == CmdBufType::Terrible) {
Corentin Walleza5696c72018-08-13 08:24:01 +0200250 bool c2sSuccess = c2sBuf->Flush();
251 bool s2cSuccess = s2cBuf->Flush();
252
253 ASSERT(c2sSuccess && s2cSuccess);
Corentin Wallez931e6e82017-06-16 18:51:14 -0400254 }
255 glfwPollEvents();
Corentin Wallez931e6e82017-06-16 18:51:14 -0400256}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400257
Corentin Wallez931e6e82017-06-16 18:51:14 -0400258bool ShouldQuit() {
259 return glfwWindowShouldClose(window);
260}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400261
Corentin Wallez931e6e82017-06-16 18:51:14 -0400262GLFWwindow* GetGLFWWindow() {
263 return window;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400264}