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"
|
2019-12-05 11:13:01 +00:00
|
|
|
#include "common/Log.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
|
|
|
|
2019-10-15 11:44:38 +00:00
|
|
|
#include <dawn/dawn_proc.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>
|
2019-02-11 21:50:16 +00:00
|
|
|
#include <dawn_wire/WireClient.h>
|
|
|
|
#include <dawn_wire/WireServer.h>
|
2017-04-20 18:38:20 +00:00
|
|
|
#include "GLFW/glfw3.h"
|
|
|
|
|
2019-02-12 15:48:15 +00:00
|
|
|
#include <algorithm>
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
void PrintDeviceError(WGPUErrorType errorType, const char* message, void*) {
|
2019-12-05 11:13:01 +00:00
|
|
|
const char* errorTypeName = "";
|
2019-08-27 21:41:56 +00:00
|
|
|
switch (errorType) {
|
2019-10-25 11:36:47 +00:00
|
|
|
case WGPUErrorType_Validation:
|
2019-12-05 11:13:01 +00:00
|
|
|
errorTypeName = "Validation";
|
2019-08-27 21:41:56 +00:00
|
|
|
break;
|
2019-10-25 11:36:47 +00:00
|
|
|
case WGPUErrorType_OutOfMemory:
|
2019-12-05 11:13:01 +00:00
|
|
|
errorTypeName = "Out of memory";
|
2019-08-27 21:41:56 +00:00
|
|
|
break;
|
2019-10-25 11:36:47 +00:00
|
|
|
case WGPUErrorType_Unknown:
|
2019-12-05 11:13:01 +00:00
|
|
|
errorTypeName = "Unknown";
|
2019-08-27 21:41:56 +00:00
|
|
|
break;
|
2019-10-25 11:36:47 +00:00
|
|
|
case WGPUErrorType_DeviceLost:
|
2019-12-05 11:13:01 +00:00
|
|
|
errorTypeName = "Device lost";
|
2019-08-27 21:41:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return;
|
|
|
|
}
|
2019-12-06 18:21:39 +00:00
|
|
|
dawn::ErrorLog() << errorTypeName << " error: " << message;
|
2017-04-20 18:42:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 17:52:03 +00:00
|
|
|
void PrintGLFWError(int code, const char* message) {
|
2019-12-06 18:21:39 +00:00
|
|
|
dawn::ErrorLog() << "GLFW error: " << code << " - " << message;
|
2018-01-19 17:52:03 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2020-01-10 13:28:18 +00:00
|
|
|
static wgpu::BackendType backendType = wgpu::BackendType::D3D12;
|
2018-07-18 11:52:46 +00:00
|
|
|
#elif defined(DAWN_ENABLE_BACKEND_METAL)
|
2020-01-10 13:28:18 +00:00
|
|
|
static wgpu::BackendType backendType = wgpu::BackendType::Metal;
|
2018-07-18 11:52:46 +00:00
|
|
|
#elif defined(DAWN_ENABLE_BACKEND_VULKAN)
|
2020-01-10 13:28:18 +00:00
|
|
|
static wgpu::BackendType backendType = wgpu::BackendType::Vulkan;
|
|
|
|
#elif defined(DAWN_ENABLE_BACKEND_OPENGL)
|
|
|
|
static wgpu::BackendType backendType = wgpu::BackendType::OpenGL;
|
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;
|
2019-02-12 15:48:15 +00:00
|
|
|
static std::unique_ptr<dawn_native::Instance> instance;
|
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;
|
|
|
|
|
2019-02-11 21:50:16 +00:00
|
|
|
static dawn_wire::WireServer* wireServer = nullptr;
|
|
|
|
static dawn_wire::WireClient* wireClient = nullptr;
|
2018-07-26 13:07:57 +00:00
|
|
|
static utils::TerribleCommandBuffer* c2sBuf = nullptr;
|
|
|
|
static utils::TerribleCommandBuffer* s2cBuf = nullptr;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Device CreateCppDawnDevice() {
|
2018-01-19 17:52:03 +00:00
|
|
|
glfwSetErrorCallback(PrintGLFWError);
|
2017-04-20 18:38:20 +00:00
|
|
|
if (!glfwInit()) {
|
2019-10-25 11:36:47 +00:00
|
|
|
return wgpu::Device();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:48:15 +00:00
|
|
|
// Create the test window and discover adapters using it (esp. for OpenGL)
|
|
|
|
utils::SetupGLFWWindowHintsForBackend(backendType);
|
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) {
|
2019-10-25 11:36:47 +00:00
|
|
|
return wgpu::Device();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:48:15 +00:00
|
|
|
instance = std::make_unique<dawn_native::Instance>();
|
|
|
|
utils::DiscoverAdapter(instance.get(), window, backendType);
|
|
|
|
|
|
|
|
// Get an adapter for the backend to use, and create the device.
|
|
|
|
dawn_native::Adapter backendAdapter;
|
|
|
|
{
|
|
|
|
std::vector<dawn_native::Adapter> adapters = instance->GetAdapters();
|
|
|
|
auto adapterIt = std::find_if(adapters.begin(), adapters.end(),
|
|
|
|
[](const dawn_native::Adapter adapter) -> bool {
|
2020-01-10 13:28:18 +00:00
|
|
|
wgpu::AdapterProperties properties;
|
|
|
|
adapter.GetProperties(&properties);
|
|
|
|
return properties.backendType == backendType;
|
|
|
|
});
|
2019-02-12 15:48:15 +00:00
|
|
|
ASSERT(adapterIt != adapters.end());
|
|
|
|
backendAdapter = *adapterIt;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUDevice backendDevice = backendAdapter.CreateDevice();
|
2019-03-11 16:52:42 +00:00
|
|
|
DawnProcTable backendProcs = dawn_native::GetProcs();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-02-12 15:48:15 +00:00
|
|
|
binding = utils::CreateBinding(backendType, window, backendDevice);
|
|
|
|
if (binding == nullptr) {
|
2019-10-25 11:36:47 +00:00
|
|
|
return wgpu::Device();
|
2019-02-12 15:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Choose whether to use the backend procs and devices directly, or set up the wire.
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUDevice cDevice = nullptr;
|
2019-03-11 16:52:42 +00:00
|
|
|
DawnProcTable procs;
|
2019-02-12 15:48:15 +00:00
|
|
|
|
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
|
|
|
|
2019-07-19 16:01:48 +00:00
|
|
|
dawn_wire::WireServerDescriptor serverDesc = {};
|
|
|
|
serverDesc.device = backendDevice;
|
|
|
|
serverDesc.procs = &backendProcs;
|
|
|
|
serverDesc.serializer = s2cBuf;
|
|
|
|
|
|
|
|
wireServer = new dawn_wire::WireServer(serverDesc);
|
2017-05-09 13:34:13 +00:00
|
|
|
c2sBuf->SetHandler(wireServer);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-07-19 16:01:48 +00:00
|
|
|
dawn_wire::WireClientDescriptor clientDesc = {};
|
|
|
|
clientDesc.serializer = c2sBuf;
|
|
|
|
|
|
|
|
wireClient = new dawn_wire::WireClient(clientDesc);
|
2019-10-25 11:36:47 +00:00
|
|
|
WGPUDevice clientDevice = wireClient->GetDevice();
|
2020-01-10 00:02:38 +00:00
|
|
|
DawnProcTable clientProcs = dawn_wire::WireClient::GetProcs();
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-15 11:44:38 +00:00
|
|
|
dawnProcSetProcs(&procs);
|
2019-08-27 21:43:56 +00:00
|
|
|
procs.deviceSetUncapturedErrorCallback(cDevice, PrintDeviceError, nullptr);
|
2019-10-25 11:36:47 +00:00
|
|
|
return wgpu::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();
|
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::TextureFormat GetPreferredSwapChainTextureFormat() {
|
2018-01-19 17:52:25 +00:00
|
|
|
DoFlush();
|
2019-10-25 11:36:47 +00:00
|
|
|
return static_cast<wgpu::TextureFormat>(binding->GetPreferredSwapChainTextureFormat());
|
2017-09-21 16:54:53 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::SwapChain GetSwapChain(const wgpu::Device& device) {
|
|
|
|
wgpu::SwapChainDescriptor swapChainDesc;
|
2019-02-15 11:15:58 +00:00
|
|
|
swapChainDesc.implementation = GetSwapChainImplementation();
|
|
|
|
return device.CreateSwapChain(&swapChainDesc);
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::TextureView CreateDefaultDepthStencilView(const wgpu::Device& device) {
|
|
|
|
wgpu::TextureDescriptor descriptor;
|
|
|
|
descriptor.dimension = wgpu::TextureDimension::e2D;
|
2018-09-18 12:49:22 +00:00
|
|
|
descriptor.size.width = 640;
|
|
|
|
descriptor.size.height = 480;
|
|
|
|
descriptor.size.depth = 1;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.arrayLayerCount = 1;
|
2018-12-12 09:27:16 +00:00
|
|
|
descriptor.sampleCount = 1;
|
2019-10-25 11:36:47 +00:00
|
|
|
descriptor.format = wgpu::TextureFormat::Depth24PlusStencil8;
|
2019-02-21 00:45:19 +00:00
|
|
|
descriptor.mipLevelCount = 1;
|
2019-10-25 11:36:47 +00:00
|
|
|
descriptor.usage = wgpu::TextureUsage::OutputAttachment;
|
2018-08-27 00:44:48 +00:00
|
|
|
auto depthStencilTexture = device.CreateTexture(&descriptor);
|
2019-08-27 17:56:23 +00:00
|
|
|
return depthStencilTexture.CreateView();
|
2017-07-28 01:30:57 +00:00
|
|
|
}
|
|
|
|
|
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]) {
|
2020-01-10 13:28:18 +00:00
|
|
|
backendType = wgpu::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]) {
|
2020-01-10 13:28:18 +00:00
|
|
|
backendType = wgpu::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]) {
|
2020-01-10 13:28:18 +00:00
|
|
|
backendType = wgpu::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]) {
|
2020-01-10 13:28:18 +00:00
|
|
|
backendType = wgpu::BackendType::OpenGL;
|
2017-06-19 16:53:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i < argc && std::string("vulkan") == argv[i]) {
|
2020-01-10 13:28:18 +00:00
|
|
|
backendType = wgpu::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
|
|
|
}
|