blob: 5fe67508e94131c701af2165e5fa30da17d0702d [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 Wallez52cbcb42018-01-19 12:52:03 -050031void PrintGLFWError(int code, const char* message) {
32 std::cout << "GLFW error: " << code << " - " << message << std::endl;
33}
34
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040035enum class CmdBufType {
36 None,
37 Terrible,
Corentin Wallez682a8252017-05-09 15:34:13 +020038 //TODO(cwallez@chromium.org) double terrible cmdbuf
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040039};
40
Corentin Wallez275817a2017-07-12 12:43:24 -040041// 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 Wallez275817a2017-07-12 12:43:24 -040047#elif defined(NXT_ENABLE_BACKEND_OPENGL)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070048 static utils::BackendType backendType = utils::BackendType::OpenGL;
49#elif defined(NXT_ENABLE_BACKEND_VULKAN)
Corentin Wallez275817a2017-07-12 12:43:24 -040050 static utils::BackendType backendType = utils::BackendType::Vulkan;
Austin Engfc2bac72017-06-05 17:08:55 -040051#else
Corentin Wallez275817a2017-07-12 12:43:24 -040052 #error
Austin Engfc2bac72017-06-05 17:08:55 -040053#endif
54
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040055static CmdBufType cmdBufType = CmdBufType::Terrible;
Corentin Wallez1bd219d2017-06-19 12:53:38 -040056static utils::BackendBinding* binding = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040057
58static GLFWwindow* window = nullptr;
59
60static nxt::wire::CommandHandler* wireServer = nullptr;
Corentin Wallez682a8252017-05-09 15:34:13 +020061static nxt::wire::CommandHandler* wireClient = nullptr;
62static nxt::wire::TerribleCommandBuffer* c2sBuf = nullptr;
63static nxt::wire::TerribleCommandBuffer* s2cBuf = nullptr;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040064
Corentin Wallez583e9a82017-05-29 11:30:29 -070065nxt::Device CreateCppNXTDevice() {
Corentin Wallez1bd219d2017-06-19 12:53:38 -040066 binding = utils::CreateBinding(backendType);
67 if (binding == nullptr) {
68 return nxt::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040069 }
70
Corentin Wallez52cbcb42018-01-19 12:52:03 -050071 glfwSetErrorCallback(PrintGLFWError);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040072 if (!glfwInit()) {
Corentin Wallez583e9a82017-05-29 11:30:29 -070073 return nxt::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040074 }
75
76 binding->SetupGLFWWindowHints();
77 window = glfwCreateWindow(640, 480, "NXT window", nullptr, nullptr);
78 if (!window) {
Corentin Wallez583e9a82017-05-29 11:30:29 -070079 return nxt::Device();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040080 }
81
82 binding->SetWindow(window);
83
84 nxtDevice backendDevice;
85 nxtProcTable backendProcs;
86 binding->GetProcAndDevice(&backendProcs, &backendDevice);
87
Corentin Wallez583e9a82017-05-29 11:30:29 -070088 nxtDevice cDevice = nullptr;
89 nxtProcTable procs;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040090 switch (cmdBufType) {
91 case CmdBufType::None:
Corentin Wallez583e9a82017-05-29 11:30:29 -070092 procs = backendProcs;
93 cDevice = backendDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040094 break;
95
96 case CmdBufType::Terrible:
97 {
Corentin Wallez682a8252017-05-09 15:34:13 +020098 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 Wallezf07e3bd2017-04-20 14:38:20 -0400103
104 nxtDevice clientDevice;
105 nxtProcTable clientProcs;
Corentin Wallez682a8252017-05-09 15:34:13 +0200106 wireClient = nxt::wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf);
107 s2cBuf->SetHandler(wireClient);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400108
Corentin Wallez583e9a82017-05-29 11:30:29 -0700109 procs = clientProcs;
110 cDevice = clientDevice;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400111 }
112 break;
113 }
114
Corentin Wallez583e9a82017-05-29 11:30:29 -0700115 nxtSetProcs(&procs);
116 procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0);
117 return nxt::Device::Acquire(cDevice);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400118}
119
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700120uint64_t GetSwapChainImplementation() {
121 return binding->GetSwapChainImplementation();
122}
123
Corentin Wallez2e31e8f2017-09-21 12:54:53 -0400124nxt::TextureFormat GetPreferredSwapChainTextureFormat() {
125 return static_cast<nxt::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
126}
127
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700128nxt::SwapChain GetSwapChain(const nxt::Device &device) {
129 return device.CreateSwapChainBuilder()
130 .SetImplementation(GetSwapChainImplementation())
131 .GetResult();
132}
133
134nxt::RenderPass CreateDefaultRenderPass(const nxt::Device& device) {
135 return device.CreateRenderPassBuilder()
136 .SetAttachmentCount(2)
Corentin Wallez2e31e8f2017-09-21 12:54:53 -0400137 .AttachmentSetFormat(0, GetPreferredSwapChainTextureFormat())
Kai Ninomiyab9854312017-08-11 14:36:20 -0700138 .AttachmentSetColorLoadOp(0, nxt::LoadOp::Clear)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700139 .AttachmentSetFormat(1, nxt::TextureFormat::D32FloatS8Uint)
Kai Ninomiyab9854312017-08-11 14:36:20 -0700140 .AttachmentSetDepthStencilLoadOps(1, nxt::LoadOp::Clear, nxt::LoadOp::Clear)
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700141 .SetSubpassCount(1)
142 .SubpassSetColorAttachment(0, 0, 0)
143 .SubpassSetDepthStencilAttachment(0, 1)
144 .GetResult();
145}
146
147nxt::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
160void 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 Wallez9347e8f2017-06-19 13:15:13 -0400176bool InitSample(int argc, const char** argv) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400177 for (int i = 0; i < argc; i++) {
178 if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
179 i++;
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400180 if (i < argc && std::string("d3d12") == argv[i]) {
181 backendType = utils::BackendType::D3D12;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400182 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400183 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400184 if (i < argc && std::string("metal") == argv[i]) {
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400185 backendType = utils::BackendType::Metal;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400186 continue;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400187 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400188 if (i < argc && std::string("null") == argv[i]) {
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400189 backendType = utils::BackendType::Null;
Corentin Wallez931e6e82017-06-16 18:51:14 -0400190 continue;
191 }
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400192 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 Wallez931e6e82017-06-16 18:51:14 -0400201 return false;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400202 }
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700203 if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400204 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 Wallezf07e3bd2017-04-20 14:38:20 -0400215 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400216 if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
217 printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400218 printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n");
Corentin Wallez931e6e82017-06-16 18:51:14 -0400219 printf(" COMMAND_BUFFER is one of: none, terrible\n");
220 return false;
221 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400222 }
Corentin Wallez931e6e82017-06-16 18:51:14 -0400223 return true;
224}
225
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700226void DoFlush() {
Corentin Wallez931e6e82017-06-16 18:51:14 -0400227 if (cmdBufType == CmdBufType::Terrible) {
228 c2sBuf->Flush();
229 s2cBuf->Flush();
230 }
231 glfwPollEvents();
Corentin Wallez931e6e82017-06-16 18:51:14 -0400232}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400233
Corentin Wallez931e6e82017-06-16 18:51:14 -0400234bool ShouldQuit() {
235 return glfwWindowShouldClose(window);
236}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400237
Corentin Wallez931e6e82017-06-16 18:51:14 -0400238GLFWwindow* GetGLFWWindow() {
239 return window;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400240}