blob: b666d3fc0ebbded9c191285332f1e18e1cf1ec08 [file] [log] [blame]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04001// 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 Wallez0b186b12017-07-12 12:56:05 -040015#include "common/Platform.h"
Corentin Wallez1bd219d2017-06-19 12:53:38 -040016#include "utils/BackendBinding.h"
Corentin Wallez0b186b12017-07-12 12:56:05 -040017#include "wire/TerribleCommandBuffer.h"
Corentin Wallez1bd219d2017-06-19 12:53:38 -040018
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040019#include <nxt/nxt.h>
20#include <nxt/nxtcpp.h>
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070021#include <nxt/nxt_wsi.h>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040022#include "GLFW/glfw3.h"
23
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024#include <cstring>
25#include <iostream>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040026
Corentin Wallez4b410a32017-04-20 14:42:36 -040027void PrintDeviceError(const char* message, nxt::CallbackUserdata) {
28 std::cout << "Device error: " << message << std::endl;
29}
30
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040031enum class CmdBufType {
32 None,
33 Terrible,
Corentin Wallez682a8252017-05-09 15:34:13 +020034 //TODO(cwallez@chromium.org) double terrible cmdbuf
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040035};
36
Corentin Wallez275817a2017-07-12 12:43:24 -040037// Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on
38// their respective platforms, and Vulkan is preferred to OpenGL
39#if defined(NXT_ENABLE_BACKEND_D3D12)
40 static utils::BackendType backendType = utils::BackendType::D3D12;
41#elif defined(NXT_ENABLE_BACKEND_METAL)
42 static utils::BackendType backendType = utils::BackendType::Metal;
Corentin Wallez275817a2017-07-12 12:43:24 -040043#elif defined(NXT_ENABLE_BACKEND_OPENGL)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070044 static utils::BackendType backendType = utils::BackendType::OpenGL;
45#elif defined(NXT_ENABLE_BACKEND_VULKAN)
Corentin Wallez275817a2017-07-12 12:43:24 -040046 static utils::BackendType backendType = utils::BackendType::Vulkan;
Austin Engfc2bac72017-06-05 17:08:55 -040047#else
Corentin Wallez275817a2017-07-12 12:43:24 -040048 #error
Austin Engfc2bac72017-06-05 17:08:55 -040049#endif
50
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040051static CmdBufType cmdBufType = CmdBufType::Terrible;
Corentin Wallez1bd219d2017-06-19 12:53:38 -040052static utils::BackendBinding* binding = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040053
54static GLFWwindow* window = nullptr;
55
56static nxt::wire::CommandHandler* wireServer = nullptr;
Corentin Wallez682a8252017-05-09 15:34:13 +020057static nxt::wire::CommandHandler* wireClient = nullptr;
58static nxt::wire::TerribleCommandBuffer* c2sBuf = nullptr;
59static nxt::wire::TerribleCommandBuffer* s2cBuf = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040060
Corentin Wallez583e9a82017-05-29 11:30:29 -070061nxt::Device CreateCppNXTDevice() {
Corentin Wallez1bd219d2017-06-19 12:53:38 -040062 binding = utils::CreateBinding(backendType);
63 if (binding == nullptr) {
64 return nxt::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040065 }
66
67 if (!glfwInit()) {
Corentin Wallez583e9a82017-05-29 11:30:29 -070068 return nxt::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040069 }
70
71 binding->SetupGLFWWindowHints();
72 window = glfwCreateWindow(640, 480, "NXT window", nullptr, nullptr);
73 if (!window) {
Corentin Wallez583e9a82017-05-29 11:30:29 -070074 return nxt::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040075 }
76
77 binding->SetWindow(window);
78
79 nxtDevice backendDevice;
80 nxtProcTable backendProcs;
81 binding->GetProcAndDevice(&backendProcs, &backendDevice);
82
Corentin Wallez583e9a82017-05-29 11:30:29 -070083 nxtDevice cDevice = nullptr;
84 nxtProcTable procs;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040085 switch (cmdBufType) {
86 case CmdBufType::None:
Corentin Wallez583e9a82017-05-29 11:30:29 -070087 procs = backendProcs;
88 cDevice = backendDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040089 break;
90
91 case CmdBufType::Terrible:
92 {
Corentin Wallez682a8252017-05-09 15:34:13 +020093 c2sBuf = new nxt::wire::TerribleCommandBuffer();
94 s2cBuf = new nxt::wire::TerribleCommandBuffer();
95
96 wireServer = nxt::wire::NewServerCommandHandler(backendDevice, backendProcs, s2cBuf);
97 c2sBuf->SetHandler(wireServer);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040098
99 nxtDevice clientDevice;
100 nxtProcTable clientProcs;
Corentin Wallez682a8252017-05-09 15:34:13 +0200101 wireClient = nxt::wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf);
102 s2cBuf->SetHandler(wireClient);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400103
Corentin Wallez583e9a82017-05-29 11:30:29 -0700104 procs = clientProcs;
105 cDevice = clientDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400106 }
107 break;
108 }
109
Corentin Wallez583e9a82017-05-29 11:30:29 -0700110 nxtSetProcs(&procs);
111 procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0);
112 return nxt::Device::Acquire(cDevice);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400113}
114
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700115uint64_t GetSwapChainImplementation() {
116 return binding->GetSwapChainImplementation();
117}
118
119nxt::SwapChain GetSwapChain(const nxt::Device &device) {
120 return device.CreateSwapChainBuilder()
121 .SetImplementation(GetSwapChainImplementation())
122 .GetResult();
123}
124
125nxt::RenderPass CreateDefaultRenderPass(const nxt::Device& device) {
126 return device.CreateRenderPassBuilder()
127 .SetAttachmentCount(2)
128 .AttachmentSetFormat(0, nxt::TextureFormat::R8G8B8A8Unorm)
129 .AttachmentSetFormat(1, nxt::TextureFormat::D32FloatS8Uint)
130 .SetSubpassCount(1)
131 .SubpassSetColorAttachment(0, 0, 0)
132 .SubpassSetDepthStencilAttachment(0, 1)
133 .GetResult();
134}
135
136nxt::TextureView CreateDefaultDepthStencilView(const nxt::Device& device) {
137 auto depthStencilTexture = device.CreateTextureBuilder()
138 .SetDimension(nxt::TextureDimension::e2D)
139 .SetExtent(640, 480, 1)
140 .SetFormat(nxt::TextureFormat::D32FloatS8Uint)
141 .SetMipLevels(1)
142 .SetAllowedUsage(nxt::TextureUsageBit::OutputAttachment)
143 .GetResult();
144 depthStencilTexture.FreezeUsage(nxt::TextureUsageBit::OutputAttachment);
145 return depthStencilTexture.CreateTextureViewBuilder()
146 .GetResult();
147}
148
149void GetNextFramebuffer(const nxt::Device& device,
150 const nxt::RenderPass& renderpass,
151 const nxt::SwapChain& swapchain,
152 const nxt::TextureView& depthStencilView,
153 nxt::Texture* backbuffer,
154 nxt::Framebuffer* framebuffer) {
155 *backbuffer = swapchain.GetNextTexture();
156 auto backbufferView = backbuffer->CreateTextureViewBuilder().GetResult();
157 *framebuffer = device.CreateFramebufferBuilder()
158 .SetRenderPass(renderpass)
159 .SetDimensions(640, 480)
160 .SetAttachment(0, backbufferView)
161 .SetAttachment(1, depthStencilView)
162 .GetResult();
163}
164
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400165bool InitSample(int argc, const char** argv) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400166 for (int i = 0; i < argc; i++) {
167 if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
168 i++;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400169 if (i < argc && std::string("d3d12") == argv[i]) {
170 backendType = utils::BackendType::D3D12;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400171 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400172 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400173 if (i < argc && std::string("metal") == argv[i]) {
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400174 backendType = utils::BackendType::Metal;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400175 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400176 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400177 if (i < argc && std::string("null") == argv[i]) {
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400178 backendType = utils::BackendType::Null;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400179 continue;
180 }
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400181 if (i < argc && std::string("opengl") == argv[i]) {
182 backendType = utils::BackendType::OpenGL;
183 continue;
184 }
185 if (i < argc && std::string("vulkan") == argv[i]) {
186 backendType = utils::BackendType::Vulkan;
187 continue;
188 }
189 fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null, vulkan)\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400190 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400191 }
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700192 if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400193 i++;
194 if (i < argc && std::string("none") == argv[i]) {
195 cmdBufType = CmdBufType::None;
196 continue;
197 }
198 if (i < argc && std::string("terrible") == argv[i]) {
199 cmdBufType = CmdBufType::Terrible;
200 continue;
201 }
202 fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n");
203 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400204 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400205 if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
206 printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400207 printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400208 printf(" COMMAND_BUFFER is one of: none, terrible\n");
209 return false;
210 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400211 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400212 return true;
213}
214
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700215void DoFlush() {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400216 if (cmdBufType == CmdBufType::Terrible) {
217 c2sBuf->Flush();
218 s2cBuf->Flush();
219 }
220 glfwPollEvents();
Corentin Wallez931e6e82017-06-16 18:51:14 -0400221}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400222
Corentin Wallez931e6e82017-06-16 18:51:14 -0400223bool ShouldQuit() {
224 return glfwWindowShouldClose(window);
225}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400226
Corentin Wallez931e6e82017-06-16 18:51:14 -0400227GLFWwindow* GetGLFWWindow() {
228 return window;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400229}