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