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"
|
2021-02-24 16:53:53 +00:00
|
|
|
#include "common/SystemUtils.h"
|
2017-06-19 16:53:38 +00:00
|
|
|
#include "utils/BackendBinding.h"
|
2020-01-15 13:14:12 +00:00
|
|
|
#include "utils/GLFWUtils.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,
|
2020-07-10 20:33:08 +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;
|
2021-05-17 18:04:48 +00:00
|
|
|
#elif defined(DAWN_ENABLE_BACKEND_OPENGLES)
|
|
|
|
static wgpu::BackendType backendType = wgpu::BackendType::OpenGLES;
|
|
|
|
#elif defined(DAWN_ENABLE_BACKEND_DESKTOP_GL)
|
2020-01-10 13:28:18 +00:00
|
|
|
static wgpu::BackendType backendType = wgpu::BackendType::OpenGL;
|
2017-06-05 21:08:55 +00:00
|
|
|
#else
|
2020-07-10 20:33:08 +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() {
|
2021-02-24 16:53:53 +00:00
|
|
|
if (GetEnvironmentVar("ANGLE_DEFAULT_PLATFORM").empty()) {
|
|
|
|
SetEnvironmentVar("ANGLE_DEFAULT_PLATFORM", "swiftshader");
|
|
|
|
}
|
|
|
|
|
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);
|
2020-10-17 23:31:37 +00:00
|
|
|
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE);
|
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;
|
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
case CmdBufType::Terrible: {
|
|
|
|
c2sBuf = new utils::TerribleCommandBuffer();
|
|
|
|
s2cBuf = new utils::TerribleCommandBuffer();
|
2017-05-09 13:34:13 +00:00
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
dawn_wire::WireServerDescriptor serverDesc = {};
|
|
|
|
serverDesc.procs = &backendProcs;
|
|
|
|
serverDesc.serializer = s2cBuf;
|
2019-07-19 16:01:48 +00:00
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
wireServer = new dawn_wire::WireServer(serverDesc);
|
|
|
|
c2sBuf->SetHandler(wireServer);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
dawn_wire::WireClientDescriptor clientDesc = {};
|
|
|
|
clientDesc.serializer = c2sBuf;
|
2019-07-19 16:01:48 +00:00
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
wireClient = new dawn_wire::WireClient(clientDesc);
|
2020-10-16 15:21:16 +00:00
|
|
|
procs = dawn_wire::client::GetProcs();
|
2020-07-10 20:33:08 +00:00
|
|
|
s2cBuf->SetHandler(wireClient);
|
2021-02-05 23:36:30 +00:00
|
|
|
|
|
|
|
auto deviceReservation = wireClient->ReserveDevice();
|
|
|
|
wireServer->InjectDevice(backendDevice, deviceReservation.id,
|
|
|
|
deviceReservation.generation);
|
|
|
|
|
|
|
|
cDevice = deviceReservation.device;
|
2020-07-10 20:33:08 +00:00
|
|
|
} break;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2020-01-23 17:20:38 +00:00
|
|
|
return device.CreateSwapChain(nullptr, &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;
|
2021-03-22 21:12:36 +00:00
|
|
|
descriptor.size.depthOrArrayLayers = 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;
|
2020-10-27 15:35:56 +00:00
|
|
|
descriptor.usage = wgpu::TextureUsage::RenderAttachment;
|
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;
|
|
|
|
}
|
2020-11-24 20:57:23 +00:00
|
|
|
if (i < argc && std::string("opengles") == argv[i]) {
|
|
|
|
backendType = wgpu::BackendType::OpenGLES;
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-19 16:53:38 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-07-10 20:33:08 +00:00
|
|
|
fprintf(stderr,
|
2020-11-24 20:57:23 +00:00
|
|
|
"--backend expects a backend name (opengl, opengles, 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]);
|
2021-03-15 08:02:53 +00:00
|
|
|
printf(" BACKEND is one of: d3d12, metal, null, opengl, opengles, 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
|
|
|
}
|