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 | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 19 | // Include Windows.h before GLFW to avoid a redefinition of APIENTRY |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame^] | 20 | #if defined(NXT_PLATFORM_WINDOWS) |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 21 | #include <Windows.h> |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame^] | 22 | #elif defined(NXT_PLATFORM_POSIX) |
| 23 | #include <unistd.h> |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 24 | #else |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame^] | 25 | #error "Unsupported platform." |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 26 | #endif |
| 27 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 28 | #include <nxt/nxt.h> |
| 29 | #include <nxt/nxtcpp.h> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 30 | #include "GLFW/glfw3.h" |
| 31 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 32 | #include <cstring> |
| 33 | #include <iostream> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 34 | |
Corentin Wallez | 4b410a3 | 2017-04-20 14:42:36 -0400 | [diff] [blame] | 35 | void PrintDeviceError(const char* message, nxt::CallbackUserdata) { |
| 36 | std::cout << "Device error: " << message << std::endl; |
| 37 | } |
| 38 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 39 | enum class CmdBufType { |
| 40 | None, |
| 41 | Terrible, |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 42 | //TODO(cwallez@chromium.org) double terrible cmdbuf |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 45 | // Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on |
| 46 | // their respective platforms, and Vulkan is preferred to OpenGL |
| 47 | #if defined(NXT_ENABLE_BACKEND_D3D12) |
| 48 | static utils::BackendType backendType = utils::BackendType::D3D12; |
| 49 | #elif defined(NXT_ENABLE_BACKEND_METAL) |
| 50 | static utils::BackendType backendType = utils::BackendType::Metal; |
| 51 | #elif defined(NXT_ENABLE_BACKEND_VULKAN) |
| 52 | static utils::BackendType backendType = utils::BackendType::OpenGL; |
| 53 | #elif defined(NXT_ENABLE_BACKEND_OPENGL) |
| 54 | static utils::BackendType backendType = utils::BackendType::Vulkan; |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame] | 55 | #else |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 56 | #error |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame] | 57 | #endif |
| 58 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 59 | static CmdBufType cmdBufType = CmdBufType::Terrible; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 60 | static utils::BackendBinding* binding = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 61 | |
| 62 | static GLFWwindow* window = nullptr; |
| 63 | |
| 64 | static nxt::wire::CommandHandler* wireServer = nullptr; |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 65 | static nxt::wire::CommandHandler* wireClient = nullptr; |
| 66 | static nxt::wire::TerribleCommandBuffer* c2sBuf = nullptr; |
| 67 | static nxt::wire::TerribleCommandBuffer* s2cBuf = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 68 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 69 | nxt::Device CreateCppNXTDevice() { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 70 | binding = utils::CreateBinding(backendType); |
| 71 | if (binding == nullptr) { |
| 72 | return nxt::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 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 | |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 123 | bool InitSample(int argc, const char** argv) { |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 124 | for (int i = 0; i < argc; i++) { |
| 125 | if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) { |
| 126 | i++; |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 127 | if (i < argc && std::string("d3d12") == argv[i]) { |
| 128 | backendType = utils::BackendType::D3D12; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 129 | continue; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 130 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 131 | if (i < argc && std::string("metal") == argv[i]) { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 132 | backendType = utils::BackendType::Metal; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 133 | continue; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 134 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 135 | if (i < argc && std::string("null") == argv[i]) { |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 136 | backendType = utils::BackendType::Null; |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 137 | continue; |
| 138 | } |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 139 | if (i < argc && std::string("opengl") == argv[i]) { |
| 140 | backendType = utils::BackendType::OpenGL; |
| 141 | continue; |
| 142 | } |
| 143 | if (i < argc && std::string("vulkan") == argv[i]) { |
| 144 | backendType = utils::BackendType::Vulkan; |
| 145 | continue; |
| 146 | } |
| 147 | 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] | 148 | return false; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 149 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 150 | if (std::string("-c") == argv[i] || std::string("--comand-buffer") == argv[i]) { |
| 151 | i++; |
| 152 | if (i < argc && std::string("none") == argv[i]) { |
| 153 | cmdBufType = CmdBufType::None; |
| 154 | continue; |
| 155 | } |
| 156 | if (i < argc && std::string("terrible") == argv[i]) { |
| 157 | cmdBufType = CmdBufType::Terrible; |
| 158 | continue; |
| 159 | } |
| 160 | fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n"); |
| 161 | return false; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 162 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 163 | if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) { |
| 164 | printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]); |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 165 | printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n"); |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 166 | printf(" COMMAND_BUFFER is one of: none, terrible\n"); |
| 167 | return false; |
| 168 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 169 | } |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | |
| 173 | void DoSwapBuffers() { |
| 174 | if (cmdBufType == CmdBufType::Terrible) { |
| 175 | c2sBuf->Flush(); |
| 176 | s2cBuf->Flush(); |
| 177 | } |
| 178 | glfwPollEvents(); |
| 179 | binding->SwapBuffers(); |
| 180 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 181 | |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame^] | 182 | #if defined(NXT_PLATFORM_WINDOWS) |
| 183 | void USleep(uint64_t usecs) { |
| 184 | Sleep(static_cast<DWORD>(usecs / 1000)); |
| 185 | } |
| 186 | #elif defined(NXT_PLATFORM_POSIX) |
| 187 | void USleep(uint64_t usecs) { |
| 188 | usleep(usecs); |
| 189 | } |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 190 | #else |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame^] | 191 | #error "Implement USleep for your platform." |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 192 | #endif |
| 193 | |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 194 | bool ShouldQuit() { |
| 195 | return glfwWindowShouldClose(window); |
| 196 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 197 | |
Corentin Wallez | 931e6e8 | 2017-06-16 18:51:14 -0400 | [diff] [blame] | 198 | GLFWwindow* GetGLFWWindow() { |
| 199 | return window; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 200 | } |