blob: 031f5d56621909616d2fb0a822968618a2b332a9 [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 Wallez3a1746e2020-01-15 13:14:12 +000021#include "utils/GLFWUtils.h"
Corentin Wallezbdc86772018-07-26 15:07:57 +020022#include "utils/TerribleCommandBuffer.h"
Corentin Wallez1bd219d2017-06-19 12:53:38 -040023
Corentin Wallez96496822019-10-15 11:44:38 +000024#include <dawn/dawn_proc.h>
Corentin Wallez3e371b12018-07-18 14:32:45 +020025#include <dawn/dawn_wsi.h>
Corentin Wallezdcb71a12018-08-02 22:27:57 +020026#include <dawn_native/DawnNative.h>
Austin Enge2c85132019-02-11 21:50:16 +000027#include <dawn_wire/WireClient.h>
28#include <dawn_wire/WireServer.h>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040029#include "GLFW/glfw3.h"
30
Corentin Wallezbb5696b2019-02-12 15:48:15 +000031#include <algorithm>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040032#include <cstring>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040033
Corentin Wallez04863c42019-10-25 11:36:47 +000034void PrintDeviceError(WGPUErrorType errorType, const char* message, void*) {
Corentin Wallez95586ff2019-12-05 11:13:01 +000035 const char* errorTypeName = "";
Austin Engcb0cb652019-08-27 21:41:56 +000036 switch (errorType) {
Corentin Wallez04863c42019-10-25 11:36:47 +000037 case WGPUErrorType_Validation:
Corentin Wallez95586ff2019-12-05 11:13:01 +000038 errorTypeName = "Validation";
Austin Engcb0cb652019-08-27 21:41:56 +000039 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000040 case WGPUErrorType_OutOfMemory:
Corentin Wallez95586ff2019-12-05 11:13:01 +000041 errorTypeName = "Out of memory";
Austin Engcb0cb652019-08-27 21:41:56 +000042 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000043 case WGPUErrorType_Unknown:
Corentin Wallez95586ff2019-12-05 11:13:01 +000044 errorTypeName = "Unknown";
Austin Engcb0cb652019-08-27 21:41:56 +000045 break;
Corentin Wallez04863c42019-10-25 11:36:47 +000046 case WGPUErrorType_DeviceLost:
Corentin Wallez95586ff2019-12-05 11:13:01 +000047 errorTypeName = "Device lost";
Austin Engcb0cb652019-08-27 21:41:56 +000048 break;
49 default:
50 UNREACHABLE();
51 return;
52 }
Corentin Wallezdc3317d2019-12-06 18:21:39 +000053 dawn::ErrorLog() << errorTypeName << " error: " << message;
Corentin Wallez4b410a32017-04-20 14:42:36 -040054}
55
Corentin Wallez52cbcb42018-01-19 12:52:03 -050056void PrintGLFWError(int code, const char* message) {
Corentin Wallezdc3317d2019-12-06 18:21:39 +000057 dawn::ErrorLog() << "GLFW error: " << code << " - " << message;
Corentin Wallez52cbcb42018-01-19 12:52:03 -050058}
59
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040060enum class CmdBufType {
61 None,
62 Terrible,
Corentin Wallez682a8252017-05-09 15:34:13 +020063 //TODO(cwallez@chromium.org) double terrible cmdbuf
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040064};
65
Corentin Wallez275817a2017-07-12 12:43:24 -040066// Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on
67// their respective platforms, and Vulkan is preferred to OpenGL
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020068#if defined(DAWN_ENABLE_BACKEND_D3D12)
Corentin Wallezf12c9db2020-01-10 13:28:18 +000069static wgpu::BackendType backendType = wgpu::BackendType::D3D12;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020070#elif defined(DAWN_ENABLE_BACKEND_METAL)
Corentin Wallezf12c9db2020-01-10 13:28:18 +000071static wgpu::BackendType backendType = wgpu::BackendType::Metal;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020072#elif defined(DAWN_ENABLE_BACKEND_VULKAN)
Corentin Wallezf12c9db2020-01-10 13:28:18 +000073static wgpu::BackendType backendType = wgpu::BackendType::Vulkan;
74#elif defined(DAWN_ENABLE_BACKEND_OPENGL)
75static wgpu::BackendType backendType = wgpu::BackendType::OpenGL;
Austin Engfc2bac72017-06-05 17:08:55 -040076#else
Corentin Wallez275817a2017-07-12 12:43:24 -040077 #error
Austin Engfc2bac72017-06-05 17:08:55 -040078#endif
79
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040080static CmdBufType cmdBufType = CmdBufType::Terrible;
Corentin Wallezbb5696b2019-02-12 15:48:15 +000081static std::unique_ptr<dawn_native::Instance> instance;
Corentin Wallez1bd219d2017-06-19 12:53:38 -040082static utils::BackendBinding* binding = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040083
84static GLFWwindow* window = nullptr;
85
Austin Enge2c85132019-02-11 21:50:16 +000086static dawn_wire::WireServer* wireServer = nullptr;
87static dawn_wire::WireClient* wireClient = nullptr;
Corentin Wallezbdc86772018-07-26 15:07:57 +020088static utils::TerribleCommandBuffer* c2sBuf = nullptr;
89static utils::TerribleCommandBuffer* s2cBuf = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040090
Corentin Wallez04863c42019-10-25 11:36:47 +000091wgpu::Device CreateCppDawnDevice() {
Corentin Wallez52cbcb42018-01-19 12:52:03 -050092 glfwSetErrorCallback(PrintGLFWError);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040093 if (!glfwInit()) {
Corentin Wallez04863c42019-10-25 11:36:47 +000094 return wgpu::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040095 }
96
Corentin Wallezbb5696b2019-02-12 15:48:15 +000097 // Create the test window and discover adapters using it (esp. for OpenGL)
98 utils::SetupGLFWWindowHintsForBackend(backendType);
Corentin Wallez6ed9cbf2018-07-18 15:32:04 +020099 window = glfwCreateWindow(640, 480, "Dawn window", nullptr, nullptr);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400100 if (!window) {
Corentin Wallez04863c42019-10-25 11:36:47 +0000101 return wgpu::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400102 }
103
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000104 instance = std::make_unique<dawn_native::Instance>();
105 utils::DiscoverAdapter(instance.get(), window, backendType);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400106
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000107 // Get an adapter for the backend to use, and create the device.
108 dawn_native::Adapter backendAdapter;
109 {
110 std::vector<dawn_native::Adapter> adapters = instance->GetAdapters();
111 auto adapterIt = std::find_if(adapters.begin(), adapters.end(),
112 [](const dawn_native::Adapter adapter) -> bool {
Corentin Wallezf12c9db2020-01-10 13:28:18 +0000113 wgpu::AdapterProperties properties;
114 adapter.GetProperties(&properties);
115 return properties.backendType == backendType;
116 });
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000117 ASSERT(adapterIt != adapters.end());
118 backendAdapter = *adapterIt;
119 }
120
Corentin Wallez04863c42019-10-25 11:36:47 +0000121 WGPUDevice backendDevice = backendAdapter.CreateDevice();
Austin Eng45f97302019-03-11 16:52:42 +0000122 DawnProcTable backendProcs = dawn_native::GetProcs();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400123
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000124 binding = utils::CreateBinding(backendType, window, backendDevice);
125 if (binding == nullptr) {
Corentin Wallez04863c42019-10-25 11:36:47 +0000126 return wgpu::Device();
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000127 }
128
129 // Choose whether to use the backend procs and devices directly, or set up the wire.
Corentin Wallez04863c42019-10-25 11:36:47 +0000130 WGPUDevice cDevice = nullptr;
Austin Eng45f97302019-03-11 16:52:42 +0000131 DawnProcTable procs;
Corentin Wallezbb5696b2019-02-12 15:48:15 +0000132
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400133 switch (cmdBufType) {
134 case CmdBufType::None:
Corentin Wallez583e9a82017-05-29 11:30:29 -0700135 procs = backendProcs;
136 cDevice = backendDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400137 break;
138
139 case CmdBufType::Terrible:
140 {
Corentin Wallezbdc86772018-07-26 15:07:57 +0200141 c2sBuf = new utils::TerribleCommandBuffer();
142 s2cBuf = new utils::TerribleCommandBuffer();
Corentin Wallez682a8252017-05-09 15:34:13 +0200143
Austin Eng6a5418a2019-07-19 16:01:48 +0000144 dawn_wire::WireServerDescriptor serverDesc = {};
145 serverDesc.device = backendDevice;
146 serverDesc.procs = &backendProcs;
147 serverDesc.serializer = s2cBuf;
148
149 wireServer = new dawn_wire::WireServer(serverDesc);
Corentin Wallez682a8252017-05-09 15:34:13 +0200150 c2sBuf->SetHandler(wireServer);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400151
Austin Eng6a5418a2019-07-19 16:01:48 +0000152 dawn_wire::WireClientDescriptor clientDesc = {};
153 clientDesc.serializer = c2sBuf;
154
155 wireClient = new dawn_wire::WireClient(clientDesc);
Corentin Wallez04863c42019-10-25 11:36:47 +0000156 WGPUDevice clientDevice = wireClient->GetDevice();
Jiawei Shaoe6441b62020-01-10 00:02:38 +0000157 DawnProcTable clientProcs = dawn_wire::WireClient::GetProcs();
Corentin Wallez682a8252017-05-09 15:34:13 +0200158 s2cBuf->SetHandler(wireClient);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400159
Corentin Wallez583e9a82017-05-29 11:30:29 -0700160 procs = clientProcs;
161 cDevice = clientDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400162 }
163 break;
164 }
165
Corentin Wallez96496822019-10-15 11:44:38 +0000166 dawnProcSetProcs(&procs);
Austin Eng45ea7e62019-08-27 21:43:56 +0000167 procs.deviceSetUncapturedErrorCallback(cDevice, PrintDeviceError, nullptr);
Corentin Wallez04863c42019-10-25 11:36:47 +0000168 return wgpu::Device::Acquire(cDevice);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400169}
170
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700171uint64_t GetSwapChainImplementation() {
172 return binding->GetSwapChainImplementation();
173}
174
Corentin Wallez04863c42019-10-25 11:36:47 +0000175wgpu::TextureFormat GetPreferredSwapChainTextureFormat() {
Corentin Wallezb91022d2018-01-19 12:52:25 -0500176 DoFlush();
Corentin Wallez04863c42019-10-25 11:36:47 +0000177 return static_cast<wgpu::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
Corentin Wallez2e31e8f2017-09-21 12:54:53 -0400178}
179
Corentin Wallez04863c42019-10-25 11:36:47 +0000180wgpu::SwapChain GetSwapChain(const wgpu::Device& device) {
181 wgpu::SwapChainDescriptor swapChainDesc;
Corentin Wallez7be2a712019-02-15 11:15:58 +0000182 swapChainDesc.implementation = GetSwapChainImplementation();
183 return device.CreateSwapChain(&swapChainDesc);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700184}
185
Corentin Wallez04863c42019-10-25 11:36:47 +0000186wgpu::TextureView CreateDefaultDepthStencilView(const wgpu::Device& device) {
187 wgpu::TextureDescriptor descriptor;
188 descriptor.dimension = wgpu::TextureDimension::e2D;
Corentin Wallez29353d62018-09-18 12:49:22 +0000189 descriptor.size.width = 640;
190 descriptor.size.height = 480;
191 descriptor.size.depth = 1;
Jiawei Shao20301662019-02-21 00:45:19 +0000192 descriptor.arrayLayerCount = 1;
Jiawei Shao8bff3b22018-12-12 09:27:16 +0000193 descriptor.sampleCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +0000194 descriptor.format = wgpu::TextureFormat::Depth24PlusStencil8;
Jiawei Shao20301662019-02-21 00:45:19 +0000195 descriptor.mipLevelCount = 1;
Corentin Wallez04863c42019-10-25 11:36:47 +0000196 descriptor.usage = wgpu::TextureUsage::OutputAttachment;
Jiawei Shao425428f2018-08-27 08:44:48 +0800197 auto depthStencilTexture = device.CreateTexture(&descriptor);
Kai Ninomiya4078ed82019-08-27 17:56:23 +0000198 return depthStencilTexture.CreateView();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700199}
200
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400201bool InitSample(int argc, const char** argv) {
Corentin Wallez862f8842018-06-07 13:03:29 +0200202 for (int i = 1; i < argc; i++) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400203 if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
204 i++;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400205 if (i < argc && std::string("d3d12") == argv[i]) {
Corentin Wallezf12c9db2020-01-10 13:28:18 +0000206 backendType = wgpu::BackendType::D3D12;
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("metal") == argv[i]) {
Corentin Wallezf12c9db2020-01-10 13:28:18 +0000210 backendType = wgpu::BackendType::Metal;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400211 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400212 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400213 if (i < argc && std::string("null") == argv[i]) {
Corentin Wallezf12c9db2020-01-10 13:28:18 +0000214 backendType = wgpu::BackendType::Null;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400215 continue;
216 }
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400217 if (i < argc && std::string("opengl") == argv[i]) {
Corentin Wallezf12c9db2020-01-10 13:28:18 +0000218 backendType = wgpu::BackendType::OpenGL;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400219 continue;
220 }
221 if (i < argc && std::string("vulkan") == argv[i]) {
Corentin Wallezf12c9db2020-01-10 13:28:18 +0000222 backendType = wgpu::BackendType::Vulkan;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400223 continue;
224 }
225 fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null, vulkan)\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400226 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400227 }
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700228 if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400229 i++;
230 if (i < argc && std::string("none") == argv[i]) {
231 cmdBufType = CmdBufType::None;
232 continue;
233 }
234 if (i < argc && std::string("terrible") == argv[i]) {
235 cmdBufType = CmdBufType::Terrible;
236 continue;
237 }
238 fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n");
239 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400240 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400241 if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
242 printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400243 printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400244 printf(" COMMAND_BUFFER is one of: none, terrible\n");
245 return false;
246 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400247 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400248 return true;
249}
250
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700251void DoFlush() {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400252 if (cmdBufType == CmdBufType::Terrible) {
Corentin Walleza5696c72018-08-13 08:24:01 +0200253 bool c2sSuccess = c2sBuf->Flush();
254 bool s2cSuccess = s2cBuf->Flush();
255
256 ASSERT(c2sSuccess && s2cSuccess);
Corentin Wallez931e6e82017-06-16 18:51:14 -0400257 }
258 glfwPollEvents();
Corentin Wallez931e6e82017-06-16 18:51:14 -0400259}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400260
Corentin Wallez931e6e82017-06-16 18:51:14 -0400261bool ShouldQuit() {
262 return glfwWindowShouldClose(window);
263}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400264
Corentin Wallez931e6e82017-06-16 18:51:14 -0400265GLFWwindow* GetGLFWWindow() {
266 return window;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400267}