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 | |
| 15 | #include <nxt/nxt.h> |
| 16 | #include <nxt/nxtcpp.h> |
| 17 | #include <shaderc/shaderc.hpp> |
| 18 | #include "GLFW/glfw3.h" |
| 19 | |
| 20 | #include "BackendBinding.h" |
| 21 | #include "../src/wire/TerribleCommandBuffer.h" |
| 22 | |
| 23 | #include <cstring> |
| 24 | #include <iostream> |
| 25 | #include <sstream> |
| 26 | #include <iomanip> |
| 27 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 28 | #ifdef _WIN32 |
| 29 | #include <Windows.h> |
| 30 | #else |
| 31 | #include <unistd.h> |
| 32 | #endif |
| 33 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 34 | BackendBinding* CreateMetalBinding(); |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame^] | 35 | BackendBinding* CreateD3D12Binding(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 36 | |
| 37 | namespace backend { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 38 | namespace opengl { |
| 39 | void Init(void* (*getProc)(const char*), nxtProcTable* procs, nxtDevice* device); |
| 40 | void HACKCLEAR(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | class OpenGLBinding : public BackendBinding { |
| 45 | public: |
| 46 | void SetupGLFWWindowHints() override { |
| 47 | #ifdef __APPLE__ |
| 48 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); |
| 49 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); |
| 50 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); |
| 51 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); |
| 52 | #else |
| 53 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); |
| 54 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5); |
| 55 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); |
| 56 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); |
| 57 | #endif |
| 58 | } |
| 59 | void GetProcAndDevice(nxtProcTable* procs, nxtDevice* device) override { |
| 60 | glfwMakeContextCurrent(window); |
| 61 | backend::opengl::Init(reinterpret_cast<void*(*)(const char*)>(glfwGetProcAddress), procs, device); |
| 62 | } |
| 63 | void SwapBuffers() override { |
| 64 | glfwSwapBuffers(window); |
| 65 | backend::opengl::HACKCLEAR(); |
| 66 | } |
| 67 | }; |
| 68 | |
Corentin Wallez | 79a62bf | 2017-05-24 16:04:55 +0200 | [diff] [blame] | 69 | namespace backend { |
| 70 | namespace null { |
| 71 | void Init(nxtProcTable* procs, nxtDevice* device); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | class NullBinding : public BackendBinding { |
| 76 | public: |
| 77 | void SetupGLFWWindowHints() override { |
| 78 | } |
| 79 | void GetProcAndDevice(nxtProcTable* procs, nxtDevice* device) override { |
| 80 | backend::null::Init(procs, device); |
| 81 | } |
| 82 | void SwapBuffers() override { |
| 83 | } |
| 84 | }; |
| 85 | |
Corentin Wallez | 4b410a3 | 2017-04-20 14:42:36 -0400 | [diff] [blame] | 86 | void PrintDeviceError(const char* message, nxt::CallbackUserdata) { |
| 87 | std::cout << "Device error: " << message << std::endl; |
| 88 | } |
| 89 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 90 | enum class BackendType { |
| 91 | OpenGL, |
| 92 | Metal, |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame^] | 93 | D3D12, |
Corentin Wallez | 79a62bf | 2017-05-24 16:04:55 +0200 | [diff] [blame] | 94 | Null, |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | enum class CmdBufType { |
| 98 | None, |
| 99 | Terrible, |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 100 | //TODO(cwallez@chromium.org) double terrible cmdbuf |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 101 | }; |
| 102 | |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame^] | 103 | #if defined(__APPLE__) |
| 104 | static BackendType backendType = BackendType::Metal; |
| 105 | #elif defined(_WIN32) |
| 106 | static BackendType backendType = BackendType::D3D12; |
| 107 | #else |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 108 | static BackendType backendType = BackendType::OpenGL; |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame^] | 109 | #endif |
| 110 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 111 | static CmdBufType cmdBufType = CmdBufType::Terrible; |
| 112 | static BackendBinding* binding = nullptr; |
| 113 | |
| 114 | static GLFWwindow* window = nullptr; |
| 115 | |
| 116 | static nxt::wire::CommandHandler* wireServer = nullptr; |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 117 | static nxt::wire::CommandHandler* wireClient = nullptr; |
| 118 | static nxt::wire::TerribleCommandBuffer* c2sBuf = nullptr; |
| 119 | static nxt::wire::TerribleCommandBuffer* s2cBuf = nullptr; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 120 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 121 | nxt::Device CreateCppNXTDevice() { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 122 | switch (backendType) { |
| 123 | case BackendType::OpenGL: |
| 124 | binding = new OpenGLBinding; |
| 125 | break; |
| 126 | case BackendType::Metal: |
| 127 | #if defined(__APPLE__) |
| 128 | binding = CreateMetalBinding(); |
| 129 | #else |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame^] | 130 | fprintf(stderr, "Metal backend not present on this platform\n"); |
| 131 | #endif |
| 132 | break; |
| 133 | case BackendType::D3D12: |
| 134 | #if defined(_WIN32) |
| 135 | binding = CreateD3D12Binding(); |
| 136 | #else |
| 137 | fprintf(stderr, "D3D12 backend not present on this platform\n"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 138 | #endif |
| 139 | break; |
Corentin Wallez | 79a62bf | 2017-05-24 16:04:55 +0200 | [diff] [blame] | 140 | case BackendType::Null: |
| 141 | binding = new NullBinding; |
| 142 | break; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (!glfwInit()) { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 146 | return nxt::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | binding->SetupGLFWWindowHints(); |
| 150 | window = glfwCreateWindow(640, 480, "NXT window", nullptr, nullptr); |
| 151 | if (!window) { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 152 | return nxt::Device(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | binding->SetWindow(window); |
| 156 | |
| 157 | nxtDevice backendDevice; |
| 158 | nxtProcTable backendProcs; |
| 159 | binding->GetProcAndDevice(&backendProcs, &backendDevice); |
| 160 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 161 | nxtDevice cDevice = nullptr; |
| 162 | nxtProcTable procs; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 163 | switch (cmdBufType) { |
| 164 | case CmdBufType::None: |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 165 | procs = backendProcs; |
| 166 | cDevice = backendDevice; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case CmdBufType::Terrible: |
| 170 | { |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 171 | c2sBuf = new nxt::wire::TerribleCommandBuffer(); |
| 172 | s2cBuf = new nxt::wire::TerribleCommandBuffer(); |
| 173 | |
| 174 | wireServer = nxt::wire::NewServerCommandHandler(backendDevice, backendProcs, s2cBuf); |
| 175 | c2sBuf->SetHandler(wireServer); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 176 | |
| 177 | nxtDevice clientDevice; |
| 178 | nxtProcTable clientProcs; |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 179 | wireClient = nxt::wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf); |
| 180 | s2cBuf->SetHandler(wireClient); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 181 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 182 | procs = clientProcs; |
| 183 | cDevice = clientDevice; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 184 | } |
| 185 | break; |
| 186 | } |
| 187 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 188 | nxtSetProcs(&procs); |
| 189 | procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0); |
| 190 | return nxt::Device::Acquire(cDevice); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | nxt::ShaderModule CreateShaderModule(const nxt::Device& device, nxt::ShaderStage stage, const char* source) { |
| 194 | shaderc::Compiler compiler; |
| 195 | shaderc::CompileOptions options; |
| 196 | |
| 197 | shaderc_shader_kind kind; |
| 198 | switch (stage) { |
| 199 | case nxt::ShaderStage::Vertex: |
| 200 | kind = shaderc_glsl_vertex_shader; |
| 201 | break; |
| 202 | case nxt::ShaderStage::Fragment: |
| 203 | kind = shaderc_glsl_fragment_shader; |
| 204 | break; |
| 205 | case nxt::ShaderStage::Compute: |
| 206 | kind = shaderc_glsl_compute_shader; |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | auto result = compiler.CompileGlslToSpv(source, strlen(source), kind, "myshader?", options); |
| 211 | if (result.GetCompilationStatus() != shaderc_compilation_status_success) { |
| 212 | std::cerr << result.GetErrorMessage(); |
| 213 | return {}; |
| 214 | } |
| 215 | |
| 216 | size_t size = (result.cend() - result.cbegin()); |
| 217 | |
| 218 | #ifdef DUMP_SPIRV_ASSEMBLY |
| 219 | { |
| 220 | auto resultAsm = compiler.CompileGlslToSpvAssembly(source, strlen(source), kind, "myshader?", options); |
| 221 | size_t sizeAsm = (resultAsm.cend() - resultAsm.cbegin()); |
| 222 | |
| 223 | char* buffer = reinterpret_cast<char*>(malloc(sizeAsm + 1)); |
| 224 | memcpy(buffer, resultAsm.cbegin(), sizeAsm); |
| 225 | buffer[sizeAsm] = '\0'; |
| 226 | printf("SPIRV ASSEMBLY DUMP START\n%s\nSPIRV ASSEMBLY DUMP END\n", buffer); |
| 227 | free(buffer); |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | #ifdef DUMP_SPIRV_JS_ARRAY |
| 232 | printf("SPIRV JS ARRAY DUMP START\n"); |
| 233 | for (size_t i = 0; i < size; i++) { |
| 234 | printf("%#010x", result.cbegin()[i]); |
| 235 | if ((i + 1) % 4 == 0) { |
| 236 | printf(",\n"); |
| 237 | } else { |
| 238 | printf(", "); |
| 239 | } |
| 240 | } |
| 241 | printf("\n"); |
| 242 | printf("SPIRV JS ARRAY DUMP END\n"); |
| 243 | #endif |
| 244 | |
| 245 | return device.CreateShaderModuleBuilder() |
| 246 | .SetSource(size, result.cbegin()) |
| 247 | .GetResult(); |
| 248 | } |
| 249 | |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 250 | void CreateDefaultRenderPass(const nxt::Device& device, nxt::RenderPass* renderPass, nxt::Framebuffer* framebuffer) { |
| 251 | *renderPass = device.CreateRenderPassBuilder() |
| 252 | .SetAttachmentCount(1) |
| 253 | .AttachmentSetFormat(0, nxt::TextureFormat::R8G8B8A8Unorm) |
| 254 | .SetSubpassCount(1) |
| 255 | .SubpassSetColorAttachment(0, 0, 0) |
| 256 | .GetResult(); |
| 257 | *framebuffer = device.CreateFramebufferBuilder() |
| 258 | .SetRenderPass(*renderPass) |
| 259 | .SetDimensions(640, 480) |
| 260 | .GetResult(); |
| 261 | } |
| 262 | |
Corentin Wallez | 001c2ea | 2017-06-05 13:12:16 -0400 | [diff] [blame] | 263 | nxt::Buffer CreateFrozenBufferFromData(const nxt::Device& device, const void* data, uint32_t size, nxt::BufferUsageBit usage) { |
Austin Eng | 5a67d19 | 2017-06-01 13:37:47 -0400 | [diff] [blame] | 264 | nxt::Buffer buffer = device.CreateBufferBuilder() |
| 265 | .SetAllowedUsage(nxt::BufferUsageBit::Mapped | usage) |
| 266 | .SetInitialUsage(nxt::BufferUsageBit::Mapped) |
| 267 | .SetSize(size) |
| 268 | .GetResult(); |
| 269 | buffer.SetSubData(0, size / sizeof(uint32_t), reinterpret_cast<const uint32_t*>(data)); |
| 270 | buffer.FreezeUsage(usage); |
| 271 | return buffer; |
| 272 | } |
| 273 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 274 | extern "C" { |
| 275 | bool InitUtils(int argc, const char** argv) { |
| 276 | for (int i = 0; i < argc; i++) { |
| 277 | if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) { |
| 278 | i++; |
| 279 | if (i < argc && std::string("opengl") == argv[i]) { |
| 280 | backendType = BackendType::OpenGL; |
| 281 | continue; |
| 282 | } |
| 283 | if (i < argc && std::string("metal") == argv[i]) { |
| 284 | backendType = BackendType::Metal; |
| 285 | continue; |
| 286 | } |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame^] | 287 | if (i < argc && std::string("d3d12") == argv[i]) { |
| 288 | backendType = BackendType::D3D12; |
| 289 | continue; |
| 290 | } |
Corentin Wallez | 79a62bf | 2017-05-24 16:04:55 +0200 | [diff] [blame] | 291 | if (i < argc && std::string("null") == argv[i]) { |
| 292 | backendType = BackendType::Null; |
| 293 | continue; |
| 294 | } |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame^] | 295 | fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null)\n"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | if (std::string("-c") == argv[i] || std::string("--comand-buffer") == argv[i]) { |
| 299 | i++; |
| 300 | if (i < argc && std::string("none") == argv[i]) { |
| 301 | cmdBufType = CmdBufType::None; |
| 302 | continue; |
| 303 | } |
| 304 | if (i < argc && std::string("terrible") == argv[i]) { |
| 305 | cmdBufType = CmdBufType::Terrible; |
| 306 | continue; |
| 307 | } |
| 308 | fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n"); |
| 309 | return false; |
| 310 | } |
| 311 | if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) { |
| 312 | printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]); |
Austin Eng | fc2bac7 | 2017-06-05 17:08:55 -0400 | [diff] [blame^] | 313 | printf(" BACKEND is one of: opengl, metal, d3d12, null\n"); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 314 | printf(" COMMAND_BUFFER is one of: none, terrible\n"); |
| 315 | return false; |
| 316 | } |
| 317 | } |
| 318 | return true; |
| 319 | } |
| 320 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 321 | nxtDevice CreateNXTDevice() { |
| 322 | return CreateCppNXTDevice().Release(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | nxtShaderModule CreateShaderModule(nxtDevice device, nxtShaderStage stage, const char* source) { |
| 326 | return CreateShaderModule(device, static_cast<nxt::ShaderStage>(stage), source).Release(); |
| 327 | } |
| 328 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 329 | void DoSwapBuffers() { |
Corentin Wallez | 682a825 | 2017-05-09 15:34:13 +0200 | [diff] [blame] | 330 | if (cmdBufType == CmdBufType::Terrible) { |
| 331 | c2sBuf->Flush(); |
| 332 | s2cBuf->Flush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 333 | } |
| 334 | glfwPollEvents(); |
| 335 | binding->SwapBuffers(); |
| 336 | } |
| 337 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 338 | #ifdef _WIN32 |
| 339 | void USleep(uint64_t usecs) { |
| 340 | Sleep(usecs / 1000); |
| 341 | } |
| 342 | #else |
| 343 | void USleep(uint64_t usecs) { |
| 344 | usleep(usecs); |
| 345 | } |
| 346 | #endif |
| 347 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 348 | bool ShouldQuit() { |
| 349 | return glfwWindowShouldClose(window); |
| 350 | } |
| 351 | |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 352 | GLFWwindow* GetGLFWWindow() { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 353 | return window; |
| 354 | } |
| 355 | } |