blob: 0b3c2d6ca13d3ef24e5c353826be9418dfaf04c4 [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 Wallez046cb462018-07-18 14:28:38 +020022#include <dawn/dawn.h>
23#include <dawn/dawncpp.h>
Corentin Wallez3e371b12018-07-18 14:32:45 +020024#include <dawn/dawn_wsi.h>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040025#include "GLFW/glfw3.h"
26
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040027#include <cstring>
28#include <iostream>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040029
Corentin Wallez4828d922018-07-18 13:45:46 +020030void PrintDeviceError(const char* message, dawn::CallbackUserdata) {
Corentin Wallez4b410a32017-04-20 14:42:36 -040031 std::cout << "Device error: " << message << std::endl;
32}
33
Corentin Wallez52cbcb42018-01-19 12:52:03 -050034void PrintGLFWError(int code, const char* message) {
35 std::cout << "GLFW error: " << code << " - " << message << std::endl;
36}
37
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040038enum class CmdBufType {
39 None,
40 Terrible,
Corentin Wallez682a8252017-05-09 15:34:13 +020041 //TODO(cwallez@chromium.org) double terrible cmdbuf
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040042};
43
Corentin Wallez275817a2017-07-12 12:43:24 -040044// Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on
45// their respective platforms, and Vulkan is preferred to OpenGL
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020046#if defined(DAWN_ENABLE_BACKEND_D3D12)
Corentin Wallez275817a2017-07-12 12:43:24 -040047 static utils::BackendType backendType = utils::BackendType::D3D12;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020048#elif defined(DAWN_ENABLE_BACKEND_METAL)
Corentin Wallez275817a2017-07-12 12:43:24 -040049 static utils::BackendType backendType = utils::BackendType::Metal;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020050#elif defined(DAWN_ENABLE_BACKEND_OPENGL)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070051 static utils::BackendType backendType = utils::BackendType::OpenGL;
Corentin Wallezf1ded9b2018-07-18 13:52:46 +020052#elif defined(DAWN_ENABLE_BACKEND_VULKAN)
Corentin Wallez275817a2017-07-12 12:43:24 -040053 static utils::BackendType backendType = utils::BackendType::Vulkan;
Austin Engfc2bac72017-06-05 17:08:55 -040054#else
Corentin Wallez275817a2017-07-12 12:43:24 -040055 #error
Austin Engfc2bac72017-06-05 17:08:55 -040056#endif
57
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040058static CmdBufType cmdBufType = CmdBufType::Terrible;
Corentin Wallez1bd219d2017-06-19 12:53:38 -040059static utils::BackendBinding* binding = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040060
61static GLFWwindow* window = nullptr;
62
Corentin Wallezbdc86772018-07-26 15:07:57 +020063static dawn_wire::CommandHandler* wireServer = nullptr;
64static dawn_wire::CommandHandler* wireClient = nullptr;
65static utils::TerribleCommandBuffer* c2sBuf = nullptr;
66static utils::TerribleCommandBuffer* s2cBuf = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040067
Corentin Wallez39039fa2018-07-18 14:06:10 +020068dawn::Device CreateCppDawnDevice() {
Corentin Wallez1bd219d2017-06-19 12:53:38 -040069 binding = utils::CreateBinding(backendType);
70 if (binding == nullptr) {
Corentin Wallez4828d922018-07-18 13:45:46 +020071 return dawn::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040072 }
73
Corentin Wallez52cbcb42018-01-19 12:52:03 -050074 glfwSetErrorCallback(PrintGLFWError);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040075 if (!glfwInit()) {
Corentin Wallez4828d922018-07-18 13:45:46 +020076 return dawn::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040077 }
78
79 binding->SetupGLFWWindowHints();
Corentin Wallez6ed9cbf2018-07-18 15:32:04 +020080 window = glfwCreateWindow(640, 480, "Dawn window", nullptr, nullptr);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040081 if (!window) {
Corentin Wallez4828d922018-07-18 13:45:46 +020082 return dawn::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040083 }
84
85 binding->SetWindow(window);
86
Corentin Wallezb1669e32018-07-18 15:12:52 +020087 dawnDevice backendDevice;
Corentin Wallezbe5ca382018-07-18 15:15:07 +020088 dawnProcTable backendProcs;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040089 binding->GetProcAndDevice(&backendProcs, &backendDevice);
90
Corentin Wallezb1669e32018-07-18 15:12:52 +020091 dawnDevice cDevice = nullptr;
Corentin Wallezbe5ca382018-07-18 15:15:07 +020092 dawnProcTable procs;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040093 switch (cmdBufType) {
94 case CmdBufType::None:
Corentin Wallez583e9a82017-05-29 11:30:29 -070095 procs = backendProcs;
96 cDevice = backendDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040097 break;
98
99 case CmdBufType::Terrible:
100 {
Corentin Wallezbdc86772018-07-26 15:07:57 +0200101 c2sBuf = new utils::TerribleCommandBuffer();
102 s2cBuf = new utils::TerribleCommandBuffer();
Corentin Wallez682a8252017-05-09 15:34:13 +0200103
Corentin Wallezbdc86772018-07-26 15:07:57 +0200104 wireServer = dawn_wire::NewServerCommandHandler(backendDevice, backendProcs, s2cBuf);
Corentin Wallez682a8252017-05-09 15:34:13 +0200105 c2sBuf->SetHandler(wireServer);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400106
Corentin Wallezb1669e32018-07-18 15:12:52 +0200107 dawnDevice clientDevice;
Corentin Wallezbe5ca382018-07-18 15:15:07 +0200108 dawnProcTable clientProcs;
Corentin Wallezbdc86772018-07-26 15:07:57 +0200109 wireClient = dawn_wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf);
Corentin Wallez682a8252017-05-09 15:34:13 +0200110 s2cBuf->SetHandler(wireClient);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400111
Corentin Wallez583e9a82017-05-29 11:30:29 -0700112 procs = clientProcs;
113 cDevice = clientDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400114 }
115 break;
116 }
117
Corentin Wallezbe5ca382018-07-18 15:15:07 +0200118 dawnSetProcs(&procs);
Corentin Wallez583e9a82017-05-29 11:30:29 -0700119 procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0);
Corentin Wallez4828d922018-07-18 13:45:46 +0200120 return dawn::Device::Acquire(cDevice);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400121}
122
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700123uint64_t GetSwapChainImplementation() {
124 return binding->GetSwapChainImplementation();
125}
126
Corentin Wallez4828d922018-07-18 13:45:46 +0200127dawn::TextureFormat GetPreferredSwapChainTextureFormat() {
Corentin Wallezb91022d2018-01-19 12:52:25 -0500128 DoFlush();
Corentin Wallez4828d922018-07-18 13:45:46 +0200129 return static_cast<dawn::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
Corentin Wallez2e31e8f2017-09-21 12:54:53 -0400130}
131
Corentin Wallez4828d922018-07-18 13:45:46 +0200132dawn::SwapChain GetSwapChain(const dawn::Device &device) {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700133 return device.CreateSwapChainBuilder()
134 .SetImplementation(GetSwapChainImplementation())
135 .GetResult();
136}
137
Corentin Wallez4828d922018-07-18 13:45:46 +0200138dawn::TextureView CreateDefaultDepthStencilView(const dawn::Device& device) {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700139 auto depthStencilTexture = device.CreateTextureBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200140 .SetDimension(dawn::TextureDimension::e2D)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700141 .SetExtent(640, 480, 1)
Corentin Wallez4828d922018-07-18 13:45:46 +0200142 .SetFormat(dawn::TextureFormat::D32FloatS8Uint)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700143 .SetMipLevels(1)
Corentin Wallez4828d922018-07-18 13:45:46 +0200144 .SetAllowedUsage(dawn::TextureUsageBit::OutputAttachment)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700145 .GetResult();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700146 return depthStencilTexture.CreateTextureViewBuilder()
147 .GetResult();
148}
149
Corentin Wallez4828d922018-07-18 13:45:46 +0200150void GetNextRenderPassDescriptor(const dawn::Device& device,
151 const dawn::SwapChain& swapchain,
152 const dawn::TextureView& depthStencilView,
153 dawn::Texture* backbuffer,
154 dawn::RenderPassDescriptor* info) {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700155 *backbuffer = swapchain.GetNextTexture();
156 auto backbufferView = backbuffer->CreateTextureViewBuilder().GetResult();
Corentin Wallez8d6b5d22018-05-11 13:04:44 -0400157 *info = device.CreateRenderPassDescriptorBuilder()
Corentin Wallez4828d922018-07-18 13:45:46 +0200158 .SetColorAttachment(0, backbufferView, dawn::LoadOp::Clear)
159 .SetDepthStencilAttachment(depthStencilView, dawn::LoadOp::Clear, dawn::LoadOp::Clear)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700160 .GetResult();
161}
162
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400163bool InitSample(int argc, const char** argv) {
Corentin Wallez862f8842018-06-07 13:03:29 +0200164 for (int i = 1; i < argc; i++) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400165 if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
166 i++;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400167 if (i < argc && std::string("d3d12") == argv[i]) {
168 backendType = utils::BackendType::D3D12;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400169 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400170 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400171 if (i < argc && std::string("metal") == argv[i]) {
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400172 backendType = utils::BackendType::Metal;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400173 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400174 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400175 if (i < argc && std::string("null") == argv[i]) {
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400176 backendType = utils::BackendType::Null;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400177 continue;
178 }
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400179 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 Wallez931e6e82017-06-16 18:51:14 -0400188 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400189 }
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700190 if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400191 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 Wallezf07e3bd2017-04-20 14:38:20 -0400202 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400203 if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
204 printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400205 printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400206 printf(" COMMAND_BUFFER is one of: none, terrible\n");
207 return false;
208 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400209 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400210 return true;
211}
212
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700213void DoFlush() {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400214 if (cmdBufType == CmdBufType::Terrible) {
Kai Ninomiya21006bb2018-06-20 18:54:18 -0700215 ASSERT(c2sBuf->Flush());
216 ASSERT(s2cBuf->Flush());
Corentin Wallez931e6e82017-06-16 18:51:14 -0400217 }
218 glfwPollEvents();
Corentin Wallez931e6e82017-06-16 18:51:14 -0400219}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400220
Corentin Wallez931e6e82017-06-16 18:51:14 -0400221bool ShouldQuit() {
222 return glfwWindowShouldClose(window);
223}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400224
Corentin Wallez931e6e82017-06-16 18:51:14 -0400225GLFWwindow* GetGLFWWindow() {
226 return window;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400227}