2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-01-19 17:52:25 +00:00
|
|
|
#include "SampleUtils.h"
|
|
|
|
|
2018-06-21 01:54:18 +00:00
|
|
|
#include "common/Assert.h"
|
2017-07-12 16:56:05 +00:00
|
|
|
#include "common/Platform.h"
|
2017-06-19 16:53:38 +00:00
|
|
|
#include "utils/BackendBinding.h"
|
2018-07-26 13:07:57 +00:00
|
|
|
#include "utils/TerribleCommandBuffer.h"
|
2017-06-19 16:53:38 +00:00
|
|
|
|
2018-07-18 12:28:38 +00:00
|
|
|
#include <dawn/dawn.h>
|
|
|
|
#include <dawn/dawncpp.h>
|
2018-07-18 12:32:45 +00:00
|
|
|
#include <dawn/dawn_wsi.h>
|
2018-08-02 20:27:57 +00:00
|
|
|
#include <dawn_native/DawnNative.h>
|
2017-04-20 18:38:20 +00:00
|
|
|
#include "GLFW/glfw3.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
void PrintDeviceError(const char* message, dawn::CallbackUserdata) {
|
2017-04-20 18:42:36 +00:00
|
|
|
std::cout << "Device error: " << message << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-01-19 17:52:03 +00:00
|
|
|
void PrintGLFWError(int code, const char* message) {
|
|
|
|
std::cout << "GLFW error: " << code << " - " << 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
|
2018-07-18 11:52:46 +00:00
|
|
|
#if defined(DAWN_ENABLE_BACKEND_D3D12)
|
2017-07-12 16:43:24 +00:00
|
|
|
static utils::BackendType backendType = utils::BackendType::D3D12;
|
2018-07-18 11:52:46 +00:00
|
|
|
#elif defined(DAWN_ENABLE_BACKEND_METAL)
|
2017-07-12 16:43:24 +00:00
|
|
|
static utils::BackendType backendType = utils::BackendType::Metal;
|
2018-07-18 11:52:46 +00:00
|
|
|
#elif defined(DAWN_ENABLE_BACKEND_OPENGL)
|
2017-07-28 01:30:57 +00:00
|
|
|
static utils::BackendType backendType = utils::BackendType::OpenGL;
|
2018-07-18 11:52:46 +00:00
|
|
|
#elif defined(DAWN_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;
|
|
|
|
|
2018-07-26 13:07:57 +00:00
|
|
|
static dawn_wire::CommandHandler* wireServer = nullptr;
|
|
|
|
static dawn_wire::CommandHandler* wireClient = nullptr;
|
|
|
|
static utils::TerribleCommandBuffer* c2sBuf = nullptr;
|
|
|
|
static utils::TerribleCommandBuffer* s2cBuf = nullptr;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 12:06:10 +00:00
|
|
|
dawn::Device CreateCppDawnDevice() {
|
2017-06-19 16:53:38 +00:00
|
|
|
binding = utils::CreateBinding(backendType);
|
|
|
|
if (binding == nullptr) {
|
2018-07-18 11:45:46 +00:00
|
|
|
return dawn::Device();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 17:52:03 +00:00
|
|
|
glfwSetErrorCallback(PrintGLFWError);
|
2017-04-20 18:38:20 +00:00
|
|
|
if (!glfwInit()) {
|
2018-07-18 11:45:46 +00:00
|
|
|
return dawn::Device();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binding->SetupGLFWWindowHints();
|
2018-07-18 13:32:04 +00:00
|
|
|
window = glfwCreateWindow(640, 480, "Dawn window", nullptr, nullptr);
|
2017-04-20 18:38:20 +00:00
|
|
|
if (!window) {
|
2018-07-18 11:45:46 +00:00
|
|
|
return dawn::Device();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binding->SetWindow(window);
|
|
|
|
|
2018-08-02 20:27:57 +00:00
|
|
|
dawnDevice backendDevice = binding->CreateDevice();
|
|
|
|
dawnProcTable backendProcs = dawn_native::GetProcs();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnDevice cDevice = nullptr;
|
2018-07-18 13:15:07 +00:00
|
|
|
dawnProcTable 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:
|
|
|
|
{
|
2018-07-26 13:07:57 +00:00
|
|
|
c2sBuf = new utils::TerribleCommandBuffer();
|
|
|
|
s2cBuf = new utils::TerribleCommandBuffer();
|
2017-05-09 13:34:13 +00:00
|
|
|
|
2018-07-26 13:07:57 +00:00
|
|
|
wireServer = dawn_wire::NewServerCommandHandler(backendDevice, backendProcs, s2cBuf);
|
2017-05-09 13:34:13 +00:00
|
|
|
c2sBuf->SetHandler(wireServer);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
dawnDevice clientDevice;
|
2018-07-18 13:15:07 +00:00
|
|
|
dawnProcTable clientProcs;
|
2018-07-26 13:07:57 +00:00
|
|
|
wireClient = dawn_wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf);
|
2017-05-09 13:34:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-18 13:15:07 +00:00
|
|
|
dawnSetProcs(&procs);
|
2017-05-29 18:30:29 +00:00
|
|
|
procs.deviceSetErrorCallback(cDevice, PrintDeviceError, 0);
|
2018-07-18 11:45:46 +00:00
|
|
|
return dawn::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();
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::TextureFormat GetPreferredSwapChainTextureFormat() {
|
2018-01-19 17:52:25 +00:00
|
|
|
DoFlush();
|
2018-07-18 11:45:46 +00:00
|
|
|
return static_cast<dawn::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
|
2017-09-21 16:54:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::SwapChain GetSwapChain(const dawn::Device &device) {
|
2017-07-28 01:30:57 +00:00
|
|
|
return device.CreateSwapChainBuilder()
|
|
|
|
.SetImplementation(GetSwapChainImplementation())
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::TextureView CreateDefaultDepthStencilView(const dawn::Device& device) {
|
2018-08-27 00:44:48 +00:00
|
|
|
dawn::TextureDescriptor descriptor;
|
|
|
|
descriptor.dimension = dawn::TextureDimension::e2D;
|
|
|
|
descriptor.width = 640;
|
|
|
|
descriptor.height = 480;
|
|
|
|
descriptor.depth = 1;
|
|
|
|
descriptor.arrayLayer = 1;
|
|
|
|
descriptor.format = dawn::TextureFormat::D32FloatS8Uint;
|
|
|
|
descriptor.mipLevel = 1;
|
|
|
|
descriptor.usage = dawn::TextureUsageBit::OutputAttachment;
|
|
|
|
auto depthStencilTexture = device.CreateTexture(&descriptor);
|
2017-07-28 01:30:57 +00:00
|
|
|
return depthStencilTexture.CreateTextureViewBuilder()
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
void GetNextRenderPassDescriptor(const dawn::Device& device,
|
|
|
|
const dawn::SwapChain& swapchain,
|
|
|
|
const dawn::TextureView& depthStencilView,
|
|
|
|
dawn::Texture* backbuffer,
|
|
|
|
dawn::RenderPassDescriptor* info) {
|
2017-07-28 01:30:57 +00:00
|
|
|
*backbuffer = swapchain.GetNextTexture();
|
|
|
|
auto backbufferView = backbuffer->CreateTextureViewBuilder().GetResult();
|
2018-05-11 17:04:44 +00:00
|
|
|
*info = device.CreateRenderPassDescriptorBuilder()
|
2018-07-18 11:45:46 +00:00
|
|
|
.SetColorAttachment(0, backbufferView, dawn::LoadOp::Clear)
|
|
|
|
.SetDepthStencilAttachment(depthStencilView, dawn::LoadOp::Clear, dawn::LoadOp::Clear)
|
2017-07-28 01:30:57 +00:00
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
2017-06-19 17:15:13 +00:00
|
|
|
bool InitSample(int argc, const char** argv) {
|
2018-06-07 11:03:29 +00:00
|
|
|
for (int i = 1; i < argc; i++) {
|
2017-06-16 22:51:14 +00:00
|
|
|
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) {
|
2018-08-13 06:24:01 +00:00
|
|
|
bool c2sSuccess = c2sBuf->Flush();
|
|
|
|
bool s2cSuccess = s2cBuf->Flush();
|
|
|
|
|
|
|
|
ASSERT(c2sSuccess && s2cSuccess);
|
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
|
|
|
}
|