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 | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame] | 18 | #include "common/Platform.h" |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 19 | #include "utils/BackendBinding.h" |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame] | 20 | #include "wire/TerribleCommandBuffer.h" |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 21 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 22 | #include <nxt/nxt.h> |
| 23 | #include <nxt/nxtcpp.h> |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 24 | #include <nxt/nxt_wsi.h> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 25 | #include "GLFW/glfw3.h" |
| 26 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 27 | #include <cstring> |
| 28 | #include <iostream> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 29 | |
Corentin Wallez | 4b410a3 | 2017-04-20 14:42:36 -0400 | [diff] [blame] | 30 | void PrintDeviceError(const char* message, nxt::CallbackUserdata) { |
| 31 | std::cout << "Device error: " << message << std::endl; |
| 32 | } |
| 33 | |
Corentin Wallez | 52cbcb4 | 2018-01-19 12:52:03 -0500 | [diff] [blame] | 34 | void PrintGLFWError(int code, const char* message) { |
| 35 | std::cout << "GLFW error: " << code << " - " << message << std::endl; |
| 36 | } |
| 37 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 38 | enum class CmdBufType { |
| 39 | None, |
| 40 | Terrible, |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 41 | //TODO(cwallez@chromium.org) double terrible cmdbuf |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 42 | }; |
| 43 | |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 44 | // 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 |
| 46 | #if defined(NXT_ENABLE_BACKEND_D3D12) |
| 47 | static utils::BackendType backendType = utils::BackendType::D3D12; |
| 48 | #elif defined(NXT_ENABLE_BACKEND_METAL) |
| 49 | static utils::BackendType backendType = utils::BackendType::Metal; |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 50 | #elif defined(NXT_ENABLE_BACKEND_OPENGL) |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 51 | static utils::BackendType backendType = utils::BackendType::OpenGL; |
| 52 | #elif defined(NXT_ENABLE_BACKEND_VULKAN) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 53 | static utils::BackendType backendType = utils::BackendType::Vulkan; |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame] | 54 | #else |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 55 | #error |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame] | 56 | #endif |
| 57 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 58 | static CmdBufType cmdBufType = CmdBufType::Terrible; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 59 | static utils::BackendBinding* binding = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 60 | |
| 61 | static GLFWwindow* window = nullptr; |
| 62 | |
| 63 | static nxt::wire::CommandHandler* wireServer = nullptr; |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 64 | static nxt::wire::CommandHandler* wireClient = nullptr; |
| 65 | static nxt::wire::TerribleCommandBuffer* c2sBuf = nullptr; |
| 66 | static nxt::wire::TerribleCommandBuffer* s2cBuf = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 67 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 68 | nxt::Device CreateCppNXTDevice() { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 69 | binding = utils::CreateBinding(backendType); |
| 70 | if (binding == nullptr) { |
| 71 | return nxt::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Corentin Wallez | 52cbcb4 | 2018-01-19 12:52:03 -0500 | [diff] [blame] | 74 | glfwSetErrorCallback(PrintGLFWError); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 75 | if (!glfwInit()) { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 76 | return nxt::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | binding->SetupGLFWWindowHints(); |
| 80 | window = glfwCreateWindow(640, 480, "NXT window", nullptr, nullptr); |
| 81 | if (!window) { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 82 | return nxt::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | binding->SetWindow(window); |
| 86 | |
| 87 | nxtDevice backendDevice; |
| 88 | nxtProcTable backendProcs; |
| 89 | binding->GetProcAndDevice(&backendProcs, &backendDevice); |
| 90 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 91 | nxtDevice cDevice = nullptr; |
| 92 | nxtProcTable procs; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 93 | switch (cmdBufType) { |
| 94 | case CmdBufType::None: |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 95 | procs = backendProcs; |
| 96 | cDevice = backendDevice; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 97 | break; |
| 98 | |
| 99 | case CmdBufType::Terrible: |
| 100 | { |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 101 | c2sBuf = new nxt::wire::TerribleCommandBuffer(); |
| 102 | s2cBuf = new nxt::wire::TerribleCommandBuffer(); |
| 103 | |
| 104 | wireServer = nxt::wire::NewServerCommandHandler(backendDevice, backendProcs, s2cBuf); |
| 105 | c2sBuf->SetHandler(wireServer); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 106 | |
| 107 | nxtDevice clientDevice; |
| 108 | nxtProcTable clientProcs; |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 109 | wireClient = nxt::wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf); |
| 110 | s2cBuf->SetHandler(wireClient); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 111 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 112 | procs = clientProcs; |
| 113 | cDevice = clientDevice; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | } |
| 117 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 118 | nxtSetProcs(&procs); |
| 119 | procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0); |
| 120 | return nxt::Device::Acquire(cDevice); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 123 | uint64_t GetSwapChainImplementation() { |
| 124 | return binding->GetSwapChainImplementation(); |
| 125 | } |
| 126 | |
Corentin Wallez | 2e31e8f | 2017-09-21 12:54:53 -0400 | [diff] [blame] | 127 | nxt::TextureFormat GetPreferredSwapChainTextureFormat() { |
Corentin Wallez | b91022d | 2018-01-19 12:52:25 -0500 | [diff] [blame] | 128 | DoFlush(); |
Corentin Wallez | 2e31e8f | 2017-09-21 12:54:53 -0400 | [diff] [blame] | 129 | return static_cast<nxt::TextureFormat>(binding->GetPreferredSwapChainTextureFormat()); |
| 130 | } |
| 131 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 132 | nxt::SwapChain GetSwapChain(const nxt::Device &device) { |
| 133 | return device.CreateSwapChainBuilder() |
| 134 | .SetImplementation(GetSwapChainImplementation()) |
| 135 | .GetResult(); |
| 136 | } |
| 137 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 138 | nxt::TextureView CreateDefaultDepthStencilView(const nxt::Device& device) { |
| 139 | auto depthStencilTexture = device.CreateTextureBuilder() |
| 140 | .SetDimension(nxt::TextureDimension::e2D) |
| 141 | .SetExtent(640, 480, 1) |
| 142 | .SetFormat(nxt::TextureFormat::D32FloatS8Uint) |
| 143 | .SetMipLevels(1) |
| 144 | .SetAllowedUsage(nxt::TextureUsageBit::OutputAttachment) |
| 145 | .GetResult(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 146 | return depthStencilTexture.CreateTextureViewBuilder() |
| 147 | .GetResult(); |
| 148 | } |
| 149 | |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 150 | void GetNextRenderPassDescriptor(const nxt::Device& device, |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 151 | const nxt::SwapChain& swapchain, |
| 152 | const nxt::TextureView& depthStencilView, |
| 153 | nxt::Texture* backbuffer, |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 154 | nxt::RenderPassDescriptor* info) { |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 155 | *backbuffer = swapchain.GetNextTexture(); |
| 156 | auto backbufferView = backbuffer->CreateTextureViewBuilder().GetResult(); |
Corentin Wallez | 8d6b5d2 | 2018-05-11 13:04:44 -0400 | [diff] [blame] | 157 | *info = device.CreateRenderPassDescriptorBuilder() |
Corentin Wallez | 6f7749c | 2018-05-02 18:10:13 -0400 | [diff] [blame] | 158 | .SetColorAttachment(0, backbufferView, nxt::LoadOp::Clear) |
| 159 | .SetDepthStencilAttachment(depthStencilView, nxt::LoadOp::Clear, nxt::LoadOp::Clear) |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 160 | .GetResult(); |
| 161 | } |
| 162 | |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 163 | bool InitSample(int argc, const char** argv) { |
Corentin Wallez | 862f884 | 2018-06-07 13:03:29 +0200 | [diff] [blame] | 164 | for (int i = 1; i < argc; i++) { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 165 | if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) { |
| 166 | i++; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 167 | if (i < argc && std::string("d3d12") == argv[i]) { |
| 168 | backendType = utils::BackendType::D3D12; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 169 | continue; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 170 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 171 | if (i < argc && std::string("metal") == argv[i]) { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 172 | backendType = utils::BackendType::Metal; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 173 | continue; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 174 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 175 | if (i < argc && std::string("null") == argv[i]) { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 176 | backendType = utils::BackendType::Null; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 177 | continue; |
| 178 | } |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 179 | 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 Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 188 | return false; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 189 | } |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 190 | if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 191 | 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 Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 202 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 203 | if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) { |
| 204 | printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]); |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 205 | printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n"); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 206 | printf(" COMMAND_BUFFER is one of: none, terrible\n"); |
| 207 | return false; |
| 208 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 209 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 213 | void DoFlush() { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 214 | if (cmdBufType == CmdBufType::Terrible) { |
Kai Ninomiya | 21006bb | 2018-06-20 18:54:18 -0700 | [diff] [blame] | 215 | ASSERT(c2sBuf->Flush()); |
| 216 | ASSERT(s2cBuf->Flush()); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 217 | } |
| 218 | glfwPollEvents(); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 219 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 220 | |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 221 | bool ShouldQuit() { |
| 222 | return glfwWindowShouldClose(window); |
| 223 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 224 | |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 225 | GLFWwindow* GetGLFWWindow() { |
| 226 | return window; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 227 | } |