blob: 46d6c9a10c30b07b219410b3cd29f426b87ffc71 [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 Wallez0b186b12017-07-12 12:56:05 -040018#include "common/Platform.h"
Corentin Wallez1bd219d2017-06-19 12:53:38 -040019#include "utils/BackendBinding.h"
Corentin Wallezbdc86772018-07-26 15:07:57 +020020#include "utils/TerribleCommandBuffer.h"
Corentin Wallez1bd219d2017-06-19 12:53:38 -040021
Corentin Wallez96496822019-10-15 11:44:38 +000022#include <dawn/dawn_proc.h>
Corentin Wallez3e371b12018-07-18 14:32:45 +020023#include <dawn/dawn_wsi.h>
Corentin Wallezdcb71a12018-08-02 22:27:57 +020024#include <dawn_native/DawnNative.h>
Austin Enge2c85132019-02-11 21:50:16 +000025#include <dawn_wire/WireClient.h>
26#include <dawn_wire/WireServer.h>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040027#include "GLFW/glfw3.h"
28
Corentin Wallezbb5696b2019-02-12 15:48:15 +000029#include <algorithm>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030#include <cstring>
31#include <iostream>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040032
Corentin Wallez04863c42019-10-25 11:36:47 +000033void PrintDeviceError(WGPUErrorType errorType, const char* message, void*) {
Austin Engcb0cb652019-08-27 21:41:56 +000034 switch (errorType) {
Corentin Wallez04863c42019-10-25 11:36:47 +000035 case WGPUErrorType_Validation:
Austin Engcb0cb652019-08-27 21:41:56 +000036 std::cout << "Validation ";
37 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000038 case WGPUErrorType_OutOfMemory:
Austin Engcb0cb652019-08-27 21:41:56 +000039 std::cout << "Out of memory ";
40 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000041 case WGPUErrorType_Unknown:
Austin Engcb0cb652019-08-27 21:41:56 +000042 std::cout << "Unknown ";
43 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000044 case WGPUErrorType_DeviceLost:
Austin Engcb0cb652019-08-27 21:41:56 +000045 std::cout << "Device lost ";
46 break;
47 default:
48 UNREACHABLE();
49 return;
50 }
51 std::cout << "error: " << message << std::endl;
Corentin Wallez4b410a32017-04-20 14:42:36 -040052}
53
Corentin Wallez52cbcb42018-01-19 12:52:03 -050054void PrintGLFWError(int code, const char* message) {
55 std::cout << "GLFW error: " << code << " - " << message << std::endl;
56}
57
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040058enum class CmdBufType {
59 None,
60 Terrible,
Corentin Wallez682a8252017-05-09 15:34:13 +020061 //TODO(cwallez@chromium.org) double terrible cmdbuf
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040062};
63
Corentin Wallez275817a2017-07-12 12:43:24 -040064// Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on
65// their respective platforms, and Vulkan is preferred to OpenGL
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020066#if defined(DAWN_ENABLE_BACKEND_D3D12)
Corentin Wallez8c88e1d2019-02-05 12:17:20 +000067 static dawn_native::BackendType backendType = dawn_native::BackendType::D3D12;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020068#elif defined(DAWN_ENABLE_BACKEND_METAL)
Corentin Wallez8c88e1d2019-02-05 12:17:20 +000069 static dawn_native::BackendType backendType = dawn_native::BackendType::Metal;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020070#elif defined(DAWN_ENABLE_BACKEND_OPENGL)
Corentin Wallez8c88e1d2019-02-05 12:17:20 +000071 static dawn_native::BackendType backendType = dawn_native::BackendType::OpenGL;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020072#elif defined(DAWN_ENABLE_BACKEND_VULKAN)
Corentin Wallez8c88e1d2019-02-05 12:17:20 +000073 static dawn_native::BackendType backendType = dawn_native::BackendType::Vulkan;
Austin Engfc2bac72017-06-05 17:08:55 -040074#else
Corentin Wallez275817a2017-07-12 12:43:24 -040075 #error
Austin Engfc2bac72017-06-05 17:08:55 -040076#endif
77
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040078static CmdBufType cmdBufType = CmdBufType::Terrible;
Corentin Wallezbb5696b2019-02-12 15:48:15 +000079static std::unique_ptr<dawn_native::Instance> instance;
Corentin Wallez1bd219d2017-06-19 12:53:38 -040080static utils::BackendBinding* binding = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040081
82static GLFWwindow* window = nullptr;
83
Austin Enge2c85132019-02-11 21:50:16 +000084static dawn_wire::WireServer* wireServer = nullptr;
85static dawn_wire::WireClient* wireClient = nullptr;
Corentin Wallezbdc86772018-07-26 15:07:57 +020086static utils::TerribleCommandBuffer* c2sBuf = nullptr;
87static utils::TerribleCommandBuffer* s2cBuf = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040088
Corentin Wallez04863c42019-10-25 11:36:47 +000089wgpu::Device CreateCppDawnDevice() {
Corentin Wallez52cbcb42018-01-19 12:52:03 -050090 glfwSetErrorCallback(PrintGLFWError);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040091 if (!glfwInit()) {
Corentin Wallez04863c42019-10-25 11:36:47 +000092 return wgpu::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040093 }
94
Corentin Wallezbb5696b2019-02-12 15:48:15 +000095 // Create the test window and discover adapters using it (esp. for OpenGL)
96 utils::SetupGLFWWindowHintsForBackend(backendType);
Corentin Wallez6ed9cbf2018-07-18 15:32:04 +020097 window = glfwCreateWindow(640, 480, "Dawn window", nullptr, nullptr);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040098 if (!window) {
Corentin Wallez04863c42019-10-25 11:36:47 +000099 return wgpu::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400100 }
101
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000102 instance = std::make_unique<dawn_native::Instance>();
103 utils::DiscoverAdapter(instance.get(), window, backendType);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400104
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000105 // Get an adapter for the backend to use, and create the device.
106 dawn_native::Adapter backendAdapter;
107 {
108 std::vector<dawn_native::Adapter> adapters = instance->GetAdapters();
109 auto adapterIt = std::find_if(adapters.begin(), adapters.end(),
110 [](const dawn_native::Adapter adapter) -> bool {
111 return adapter.GetBackendType() == backendType;
112 });
113 ASSERT(adapterIt != adapters.end());
114 backendAdapter = *adapterIt;
115 }
116
Corentin Wallez04863c42019-10-25 11:36:47 +0000117 WGPUDevice backendDevice = backendAdapter.CreateDevice();
Austin Eng45f97302019-03-11 16:52:42 +0000118 DawnProcTable backendProcs = dawn_native::GetProcs();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400119
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000120 binding = utils::CreateBinding(backendType, window, backendDevice);
121 if (binding == nullptr) {
Corentin Wallez04863c42019-10-25 11:36:47 +0000122 return wgpu::Device();
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000123 }
124
125 // Choose whether to use the backend procs and devices directly, or set up the wire.
Corentin Wallez04863c42019-10-25 11:36:47 +0000126 WGPUDevice cDevice = nullptr;
Austin Eng45f97302019-03-11 16:52:42 +0000127 DawnProcTable procs;
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000128
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400129 switch (cmdBufType) {
130 case CmdBufType::None:
Corentin Wallez583e9a82017-05-29 11:30:29 -0700131 procs = backendProcs;
132 cDevice = backendDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400133 break;
134
135 case CmdBufType::Terrible:
136 {
Corentin Wallezbdc86772018-07-26 15:07:57 +0200137 c2sBuf = new utils::TerribleCommandBuffer();
138 s2cBuf = new utils::TerribleCommandBuffer();
Corentin Wallez682a8252017-05-09 15:34:13 +0200139
Austin Eng6a5418a2019-07-19 16:01:48 +0000140 dawn_wire::WireServerDescriptor serverDesc = {};
141 serverDesc.device = backendDevice;
142 serverDesc.procs = &backendProcs;
143 serverDesc.serializer = s2cBuf;
144
145 wireServer = new dawn_wire::WireServer(serverDesc);
Corentin Wallez682a8252017-05-09 15:34:13 +0200146 c2sBuf->SetHandler(wireServer);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400147
Austin Eng6a5418a2019-07-19 16:01:48 +0000148 dawn_wire::WireClientDescriptor clientDesc = {};
149 clientDesc.serializer = c2sBuf;
150
151 wireClient = new dawn_wire::WireClient(clientDesc);
Corentin Wallez04863c42019-10-25 11:36:47 +0000152 WGPUDevice clientDevice = wireClient->GetDevice();
Austin Eng45f97302019-03-11 16:52:42 +0000153 DawnProcTable clientProcs = wireClient->GetProcs();
Corentin Wallez682a8252017-05-09 15:34:13 +0200154 s2cBuf->SetHandler(wireClient);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400155
Corentin Wallez583e9a82017-05-29 11:30:29 -0700156 procs = clientProcs;
157 cDevice = clientDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400158 }
159 break;
160 }
161
Corentin Wallez96496822019-10-15 11:44:38 +0000162 dawnProcSetProcs(&procs);
Austin Eng45ea7e62019-08-27 21:43:56 +0000163 procs.deviceSetUncapturedErrorCallback(cDevice, PrintDeviceError, nullptr);
Corentin Wallez04863c42019-10-25 11:36:47 +0000164 return wgpu::Device::Acquire(cDevice);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400165}
166
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700167uint64_t GetSwapChainImplementation() {
168 return binding->GetSwapChainImplementation();
169}
170
Corentin Wallez04863c42019-10-25 11:36:47 +0000171wgpu::TextureFormat GetPreferredSwapChainTextureFormat() {
Corentin Wallezb91022d2018-01-19 12:52:25 -0500172 DoFlush();
Corentin Wallez04863c42019-10-25 11:36:47 +0000173 return static_cast<wgpu::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
Corentin Wallez2e31e8f2017-09-21 12:54:53 -0400174}
175
Corentin Wallez04863c42019-10-25 11:36:47 +0000176wgpu::SwapChain GetSwapChain(const wgpu::Device& device) {
177 wgpu::SwapChainDescriptor swapChainDesc;
Corentin Wallez7be2a712019-02-15 11:15:58 +0000178 swapChainDesc.implementation = GetSwapChainImplementation();
179 return device.CreateSwapChain(&swapChainDesc);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700180}
181
Corentin Wallez04863c42019-10-25 11:36:47 +0000182wgpu::TextureView CreateDefaultDepthStencilView(const wgpu::Device& device) {
183 wgpu::TextureDescriptor descriptor;
184 descriptor.dimension = wgpu::TextureDimension::e2D;
Corentin Wallez29353d62018-09-18 12:49:22 +0000185 descriptor.size.width = 640;
186 descriptor.size.height = 480;
187 descriptor.size.depth = 1;
Jiawei Shao20301662019-02-21 00:45:19 +0000188 descriptor.arrayLayerCount = 1;
Jiawei Shao8bff3b22018-12-12 09:27:16 +0000189 descriptor.sampleCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +0000190 descriptor.format = wgpu::TextureFormat::Depth24PlusStencil8;
Jiawei Shao20301662019-02-21 00:45:19 +0000191 descriptor.mipLevelCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +0000192 descriptor.usage = wgpu::TextureUsage::OutputAttachment;
Jiawei Shao425428f2018-08-27 08:44:48 +0800193 auto depthStencilTexture = device.CreateTexture(&descriptor);
Kai Ninomiya4078ed82019-08-27 17:56:23 +0000194 return depthStencilTexture.CreateView();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700195}
196
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400197bool InitSample(int argc, const char** argv) {
Corentin Wallez862f8842018-06-07 13:03:29 +0200198 for (int i = 1; i < argc; i++) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400199 if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
200 i++;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400201 if (i < argc && std::string("d3d12") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000202 backendType = dawn_native::BackendType::D3D12;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400203 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400204 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400205 if (i < argc && std::string("metal") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000206 backendType = dawn_native::BackendType::Metal;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400207 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400208 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400209 if (i < argc && std::string("null") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000210 backendType = dawn_native::BackendType::Null;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400211 continue;
212 }
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400213 if (i < argc && std::string("opengl") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000214 backendType = dawn_native::BackendType::OpenGL;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400215 continue;
216 }
217 if (i < argc && std::string("vulkan") == argv[i]) {
Corentin Wallez8c88e1d2019-02-05 12:17:20 +0000218 backendType = dawn_native::BackendType::Vulkan;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400219 continue;
220 }
221 fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null, vulkan)\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400222 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400223 }
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700224 if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400225 i++;
226 if (i < argc && std::string("none") == argv[i]) {
227 cmdBufType = CmdBufType::None;
228 continue;
229 }
230 if (i < argc && std::string("terrible") == argv[i]) {
231 cmdBufType = CmdBufType::Terrible;
232 continue;
233 }
234 fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n");
235 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400236 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400237 if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
238 printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400239 printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400240 printf(" COMMAND_BUFFER is one of: none, terrible\n");
241 return false;
242 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400243 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400244 return true;
245}
246
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700247void DoFlush() {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400248 if (cmdBufType == CmdBufType::Terrible) {
Corentin Walleza5696c72018-08-13 08:24:01 +0200249 bool c2sSuccess = c2sBuf->Flush();
250 bool s2cSuccess = s2cBuf->Flush();
251
252 ASSERT(c2sSuccess && s2cSuccess);
Corentin Wallez931e6e82017-06-16 18:51:14 -0400253 }
254 glfwPollEvents();
Corentin Wallez931e6e82017-06-16 18:51:14 -0400255}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400256
Corentin Wallez931e6e82017-06-16 18:51:14 -0400257bool ShouldQuit() {
258 return glfwWindowShouldClose(window);
259}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400260
Corentin Wallez931e6e82017-06-16 18:51:14 -0400261GLFWwindow* GetGLFWWindow() {
262 return window;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400263}