2017-04-20 18:38:20 +00:00
|
|
|
// Copyright 2017 The NXT Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-07-12 16:56:05 +00:00
|
|
|
#include "common/Platform.h"
|
2017-06-19 16:53:38 +00:00
|
|
|
#include "utils/BackendBinding.h"
|
2017-07-12 16:56:05 +00:00
|
|
|
#include "wire/TerribleCommandBuffer.h"
|
2017-06-19 16:53:38 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <nxt/nxt.h>
|
|
|
|
#include <nxt/nxtcpp.h>
|
2017-07-28 01:30:57 +00:00
|
|
|
#include <nxt/nxt_wsi.h>
|
2017-04-20 18:38:20 +00:00
|
|
|
#include "GLFW/glfw3.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
2017-04-20 18:42:36 +00:00
|
|
|
void PrintDeviceError(const char* message, nxt::CallbackUserdata) {
|
|
|
|
std::cout << "Device error: " << message << std::endl;
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
enum class CmdBufType {
|
|
|
|
None,
|
|
|
|
Terrible,
|
2017-05-09 13:34:13 +00:00
|
|
|
//TODO(cwallez@chromium.org) double terrible cmdbuf
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2017-07-12 16:43:24 +00:00
|
|
|
// Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on
|
|
|
|
// their respective platforms, and Vulkan is preferred to OpenGL
|
|
|
|
#if defined(NXT_ENABLE_BACKEND_D3D12)
|
|
|
|
static utils::BackendType backendType = utils::BackendType::D3D12;
|
|
|
|
#elif defined(NXT_ENABLE_BACKEND_METAL)
|
|
|
|
static utils::BackendType backendType = utils::BackendType::Metal;
|
|
|
|
#elif defined(NXT_ENABLE_BACKEND_OPENGL)
|
2017-07-28 01:30:57 +00:00
|
|
|
static utils::BackendType backendType = utils::BackendType::OpenGL;
|
|
|
|
#elif defined(NXT_ENABLE_BACKEND_VULKAN)
|
2017-07-12 16:43:24 +00:00
|
|
|
static utils::BackendType backendType = utils::BackendType::Vulkan;
|
2017-06-05 21:08:55 +00:00
|
|
|
#else
|
2017-07-12 16:43:24 +00:00
|
|
|
#error
|
2017-06-05 21:08:55 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
static CmdBufType cmdBufType = CmdBufType::Terrible;
|
2017-06-19 16:53:38 +00:00
|
|
|
static utils::BackendBinding* binding = nullptr;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
static GLFWwindow* window = nullptr;
|
|
|
|
|
|
|
|
static nxt::wire::CommandHandler* wireServer = nullptr;
|
2017-05-09 13:34:13 +00:00
|
|
|
static nxt::wire::CommandHandler* wireClient = nullptr;
|
|
|
|
static nxt::wire::TerribleCommandBuffer* c2sBuf = nullptr;
|
|
|
|
static nxt::wire::TerribleCommandBuffer* s2cBuf = nullptr;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-05-29 18:30:29 +00:00
|
|
|
nxt::Device CreateCppNXTDevice() {
|
2017-06-19 16:53:38 +00:00
|
|
|
binding = utils::CreateBinding(backendType);
|
|
|
|
if (binding == nullptr) {
|
|
|
|
return nxt::Device();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!glfwInit()) {
|
2017-05-29 18:30:29 +00:00
|
|
|
return nxt::Device();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binding->SetupGLFWWindowHints();
|
|
|
|
window = glfwCreateWindow(640, 480, "NXT window", nullptr, nullptr);
|
|
|
|
if (!window) {
|
2017-05-29 18:30:29 +00:00
|
|
|
return nxt::Device();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binding->SetWindow(window);
|
|
|
|
|
|
|
|
nxtDevice backendDevice;
|
|
|
|
nxtProcTable backendProcs;
|
|
|
|
binding->GetProcAndDevice(&backendProcs, &backendDevice);
|
|
|
|
|
2017-05-29 18:30:29 +00:00
|
|
|
nxtDevice cDevice = nullptr;
|
|
|
|
nxtProcTable procs;
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (cmdBufType) {
|
|
|
|
case CmdBufType::None:
|
2017-05-29 18:30:29 +00:00
|
|
|
procs = backendProcs;
|
|
|
|
cDevice = backendDevice;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CmdBufType::Terrible:
|
|
|
|
{
|
2017-05-09 13:34:13 +00:00
|
|
|
c2sBuf = new nxt::wire::TerribleCommandBuffer();
|
|
|
|
s2cBuf = new nxt::wire::TerribleCommandBuffer();
|
|
|
|
|
|
|
|
wireServer = nxt::wire::NewServerCommandHandler(backendDevice, backendProcs, s2cBuf);
|
|
|
|
c2sBuf->SetHandler(wireServer);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
nxtDevice clientDevice;
|
|
|
|
nxtProcTable clientProcs;
|
2017-05-09 13:34:13 +00:00
|
|
|
wireClient = nxt::wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf);
|
|
|
|
s2cBuf->SetHandler(wireClient);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-05-29 18:30:29 +00:00
|
|
|
procs = clientProcs;
|
|
|
|
cDevice = clientDevice;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-29 18:30:29 +00:00
|
|
|
nxtSetProcs(&procs);
|
|
|
|
procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0);
|
|
|
|
return nxt::Device::Acquire(cDevice);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
uint64_t GetSwapChainImplementation() {
|
|
|
|
return binding->GetSwapChainImplementation();
|
|
|
|
}
|
|
|
|
|
|
|
|
nxt::SwapChain GetSwapChain(const nxt::Device &device) {
|
|
|
|
return device.CreateSwapChainBuilder()
|
|
|
|
.SetImplementation(GetSwapChainImplementation())
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
nxt::RenderPass CreateDefaultRenderPass(const nxt::Device& device) {
|
|
|
|
return device.CreateRenderPassBuilder()
|
|
|
|
.SetAttachmentCount(2)
|
|
|
|
.AttachmentSetFormat(0, nxt::TextureFormat::R8G8B8A8Unorm)
|
2017-08-11 21:36:20 +00:00
|
|
|
.AttachmentSetColorLoadOp(0, nxt::LoadOp::Clear)
|
2017-07-28 01:30:57 +00:00
|
|
|
.AttachmentSetFormat(1, nxt::TextureFormat::D32FloatS8Uint)
|
2017-08-11 21:36:20 +00:00
|
|
|
.AttachmentSetDepthStencilLoadOps(1, nxt::LoadOp::Clear, nxt::LoadOp::Clear)
|
2017-07-28 01:30:57 +00:00
|
|
|
.SetSubpassCount(1)
|
|
|
|
.SubpassSetColorAttachment(0, 0, 0)
|
|
|
|
.SubpassSetDepthStencilAttachment(0, 1)
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
nxt::TextureView CreateDefaultDepthStencilView(const nxt::Device& device) {
|
|
|
|
auto depthStencilTexture = device.CreateTextureBuilder()
|
|
|
|
.SetDimension(nxt::TextureDimension::e2D)
|
|
|
|
.SetExtent(640, 480, 1)
|
|
|
|
.SetFormat(nxt::TextureFormat::D32FloatS8Uint)
|
|
|
|
.SetMipLevels(1)
|
|
|
|
.SetAllowedUsage(nxt::TextureUsageBit::OutputAttachment)
|
|
|
|
.GetResult();
|
|
|
|
depthStencilTexture.FreezeUsage(nxt::TextureUsageBit::OutputAttachment);
|
|
|
|
return depthStencilTexture.CreateTextureViewBuilder()
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetNextFramebuffer(const nxt::Device& device,
|
|
|
|
const nxt::RenderPass& renderpass,
|
|
|
|
const nxt::SwapChain& swapchain,
|
|
|
|
const nxt::TextureView& depthStencilView,
|
|
|
|
nxt::Texture* backbuffer,
|
|
|
|
nxt::Framebuffer* framebuffer) {
|
|
|
|
*backbuffer = swapchain.GetNextTexture();
|
|
|
|
auto backbufferView = backbuffer->CreateTextureViewBuilder().GetResult();
|
|
|
|
*framebuffer = device.CreateFramebufferBuilder()
|
|
|
|
.SetRenderPass(renderpass)
|
|
|
|
.SetDimensions(640, 480)
|
|
|
|
.SetAttachment(0, backbufferView)
|
|
|
|
.SetAttachment(1, depthStencilView)
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
2017-06-19 17:15:13 +00:00
|
|
|
bool InitSample(int argc, const char** argv) {
|
2017-06-16 22:51:14 +00:00
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
if (std::string("-b") == argv[i] || std::string("--backend") == argv[i]) {
|
|
|
|
i++;
|
2017-06-19 16:53:38 +00:00
|
|
|
if (i < argc && std::string("d3d12") == argv[i]) {
|
|
|
|
backendType = utils::BackendType::D3D12;
|
2017-06-16 22:51:14 +00:00
|
|
|
continue;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2017-06-16 22:51:14 +00:00
|
|
|
if (i < argc && std::string("metal") == argv[i]) {
|
2017-06-19 16:53:38 +00:00
|
|
|
backendType = utils::BackendType::Metal;
|
2017-06-16 22:51:14 +00:00
|
|
|
continue;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2017-06-19 16:53:38 +00:00
|
|
|
if (i < argc && std::string("null") == argv[i]) {
|
|
|
|
backendType = utils::BackendType::Null;
|
2017-06-16 22:51:14 +00:00
|
|
|
continue;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2017-06-19 16:53:38 +00:00
|
|
|
if (i < argc && std::string("opengl") == argv[i]) {
|
|
|
|
backendType = utils::BackendType::OpenGL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i < argc && std::string("vulkan") == argv[i]) {
|
|
|
|
backendType = utils::BackendType::Vulkan;
|
2017-06-16 22:51:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-19 16:53:38 +00:00
|
|
|
fprintf(stderr, "--backend expects a backend name (opengl, metal, d3d12, null, vulkan)\n");
|
2017-06-16 22:51:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-28 01:30:57 +00:00
|
|
|
if (std::string("-c") == argv[i] || std::string("--command-buffer") == argv[i]) {
|
2017-06-16 22:51:14 +00:00
|
|
|
i++;
|
|
|
|
if (i < argc && std::string("none") == argv[i]) {
|
|
|
|
cmdBufType = CmdBufType::None;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i < argc && std::string("terrible") == argv[i]) {
|
|
|
|
cmdBufType = CmdBufType::Terrible;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "--command-buffer expects a command buffer name (none, terrible)\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (std::string("-h") == argv[i] || std::string("--help") == argv[i]) {
|
|
|
|
printf("Usage: %s [-b BACKEND] [-c COMMAND_BUFFER]\n", argv[0]);
|
2017-06-19 16:53:38 +00:00
|
|
|
printf(" BACKEND is one of: d3d12, metal, null, opengl, vulkan\n");
|
2017-06-16 22:51:14 +00:00
|
|
|
printf(" COMMAND_BUFFER is one of: none, terrible\n");
|
|
|
|
return false;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-16 22:51:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
void DoFlush() {
|
2017-06-16 22:51:14 +00:00
|
|
|
if (cmdBufType == CmdBufType::Terrible) {
|
|
|
|
c2sBuf->Flush();
|
|
|
|
s2cBuf->Flush();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2017-06-16 22:51:14 +00:00
|
|
|
glfwPollEvents();
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-06-16 22:51:14 +00:00
|
|
|
bool ShouldQuit() {
|
|
|
|
return glfwWindowShouldClose(window);
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-06-16 22:51:14 +00:00
|
|
|
GLFWwindow* GetGLFWWindow() {
|
|
|
|
return window;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|