2022-07-27 15:25:25 +00:00
|
|
|
#include "gpu.hpp"
|
|
|
|
|
|
|
|
#include <aurora/aurora.h>
|
|
|
|
|
|
|
|
#include "../window.hpp"
|
|
|
|
#include "../internal.hpp"
|
|
|
|
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <magic_enum.hpp>
|
|
|
|
#include <memory>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
#ifdef WEBGPU_DAWN
|
|
|
|
#include <dawn/native/DawnNative.h>
|
2022-07-27 15:25:25 +00:00
|
|
|
#include "../dawn/BackendBinding.hpp"
|
2022-08-02 20:37:56 +00:00
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
|
|
|
|
namespace aurora::webgpu {
|
|
|
|
static Module Log("aurora::gpu");
|
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::Device g_device;
|
|
|
|
wgpu::Queue g_queue;
|
|
|
|
wgpu::SwapChain g_swapChain;
|
|
|
|
wgpu::BackendType g_backendType;
|
2022-07-27 15:25:25 +00:00
|
|
|
GraphicsConfig g_graphicsConfig;
|
|
|
|
TextureWithSampler g_frameBuffer;
|
|
|
|
TextureWithSampler g_frameBufferResolved;
|
|
|
|
TextureWithSampler g_depthBuffer;
|
|
|
|
|
|
|
|
// EFB -> XFB copy pipeline
|
2022-08-02 20:37:56 +00:00
|
|
|
static wgpu::BindGroupLayout g_CopyBindGroupLayout;
|
|
|
|
wgpu::RenderPipeline g_CopyPipeline;
|
|
|
|
wgpu::BindGroup g_CopyBindGroup;
|
|
|
|
|
|
|
|
#ifdef WEBGPU_DAWN
|
|
|
|
static std::unique_ptr<dawn::native::Instance> g_dawnInstance;
|
|
|
|
static dawn::native::Adapter g_adapter;
|
|
|
|
static std::unique_ptr<utils::BackendBinding> g_backendBinding;
|
|
|
|
#else
|
|
|
|
wgpu::Instance g_instance;
|
|
|
|
static wgpu::Adapter g_adapter;
|
|
|
|
#endif
|
|
|
|
static wgpu::Surface g_surface;
|
|
|
|
static wgpu::AdapterProperties g_adapterProperties;
|
2022-07-27 15:25:25 +00:00
|
|
|
|
|
|
|
TextureWithSampler create_render_texture(bool multisampled) {
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::Extent3D size{
|
|
|
|
.width = g_graphicsConfig.swapChainDescriptor.width,
|
|
|
|
.height = g_graphicsConfig.swapChainDescriptor.height,
|
2022-07-27 15:25:25 +00:00
|
|
|
.depthOrArrayLayers = 1,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
const auto format = g_graphicsConfig.swapChainDescriptor.format;
|
2022-07-27 15:25:25 +00:00
|
|
|
uint32_t sampleCount = 1;
|
|
|
|
if (multisampled) {
|
|
|
|
sampleCount = g_graphicsConfig.msaaSamples;
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureDescriptor textureDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = "Render texture",
|
2022-08-02 20:37:56 +00:00
|
|
|
.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopySrc |
|
|
|
|
wgpu::TextureUsage::CopyDst,
|
|
|
|
.dimension = wgpu::TextureDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
.size = size,
|
|
|
|
.format = format,
|
|
|
|
.mipLevelCount = 1,
|
|
|
|
.sampleCount = sampleCount,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto texture = g_device.CreateTexture(&textureDescriptor);
|
2022-07-27 15:25:25 +00:00
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureViewDescriptor viewDescriptor{
|
|
|
|
.label = "Render texture view",
|
|
|
|
.dimension = wgpu::TextureViewDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
.mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED,
|
|
|
|
.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto view = texture.CreateView(&viewDescriptor);
|
2022-07-27 15:25:25 +00:00
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::SamplerDescriptor samplerDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = "Render sampler",
|
2022-08-02 20:37:56 +00:00
|
|
|
.addressModeU = wgpu::AddressMode::ClampToEdge,
|
|
|
|
.addressModeV = wgpu::AddressMode::ClampToEdge,
|
|
|
|
.addressModeW = wgpu::AddressMode::ClampToEdge,
|
|
|
|
.magFilter = wgpu::FilterMode::Linear,
|
|
|
|
.minFilter = wgpu::FilterMode::Linear,
|
|
|
|
.mipmapFilter = wgpu::FilterMode::Linear,
|
2022-07-27 15:25:25 +00:00
|
|
|
.lodMinClamp = 0.f,
|
|
|
|
.lodMaxClamp = 1000.f,
|
|
|
|
.maxAnisotropy = 1,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto sampler = g_device.CreateSampler(&samplerDescriptor);
|
2022-07-27 15:25:25 +00:00
|
|
|
|
|
|
|
return {
|
2022-08-02 20:37:56 +00:00
|
|
|
.texture = std::move(texture),
|
|
|
|
.view = std::move(view),
|
2022-07-27 15:25:25 +00:00
|
|
|
.size = size,
|
|
|
|
.format = format,
|
2022-08-02 20:37:56 +00:00
|
|
|
.sampler = std::move(sampler),
|
2022-07-27 15:25:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static TextureWithSampler create_depth_texture() {
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::Extent3D size{
|
|
|
|
.width = g_graphicsConfig.swapChainDescriptor.width,
|
|
|
|
.height = g_graphicsConfig.swapChainDescriptor.height,
|
2022-07-27 15:25:25 +00:00
|
|
|
.depthOrArrayLayers = 1,
|
|
|
|
};
|
|
|
|
const auto format = g_graphicsConfig.depthFormat;
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureDescriptor textureDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = "Depth texture",
|
2022-08-02 20:37:56 +00:00
|
|
|
.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::TextureBinding,
|
|
|
|
.dimension = wgpu::TextureDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
.size = size,
|
|
|
|
.format = format,
|
|
|
|
.mipLevelCount = 1,
|
|
|
|
.sampleCount = g_graphicsConfig.msaaSamples,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto texture = g_device.CreateTexture(&textureDescriptor);
|
2022-07-27 15:25:25 +00:00
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureViewDescriptor viewDescriptor{
|
|
|
|
.label = "Depth texture view",
|
|
|
|
.dimension = wgpu::TextureViewDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
.mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED,
|
|
|
|
.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto view = texture.CreateView(&viewDescriptor);
|
2022-07-27 15:25:25 +00:00
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::SamplerDescriptor samplerDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = "Depth sampler",
|
2022-08-02 20:37:56 +00:00
|
|
|
.addressModeU = wgpu::AddressMode::ClampToEdge,
|
|
|
|
.addressModeV = wgpu::AddressMode::ClampToEdge,
|
|
|
|
.addressModeW = wgpu::AddressMode::ClampToEdge,
|
|
|
|
.magFilter = wgpu::FilterMode::Linear,
|
|
|
|
.minFilter = wgpu::FilterMode::Linear,
|
|
|
|
.mipmapFilter = wgpu::FilterMode::Linear,
|
2022-07-27 15:25:25 +00:00
|
|
|
.lodMinClamp = 0.f,
|
|
|
|
.lodMaxClamp = 1000.f,
|
|
|
|
.maxAnisotropy = 1,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto sampler = g_device.CreateSampler(&samplerDescriptor);
|
2022-07-27 15:25:25 +00:00
|
|
|
|
|
|
|
return {
|
2022-08-02 20:37:56 +00:00
|
|
|
.texture = std::move(texture),
|
|
|
|
.view = std::move(view),
|
2022-07-27 15:25:25 +00:00
|
|
|
.size = size,
|
|
|
|
.format = format,
|
2022-08-02 20:37:56 +00:00
|
|
|
.sampler = std::move(sampler),
|
2022-07-27 15:25:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_copy_pipeline() {
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::ShaderModuleWGSLDescriptor sourceDescriptor{};
|
|
|
|
sourceDescriptor.source = R"""(
|
2022-07-27 15:25:25 +00:00
|
|
|
@group(0) @binding(0)
|
|
|
|
var efb_sampler: sampler;
|
|
|
|
@group(0) @binding(1)
|
|
|
|
var efb_texture: texture_2d<f32>;
|
|
|
|
|
|
|
|
struct VertexOutput {
|
|
|
|
@builtin(position) pos: vec4<f32>,
|
|
|
|
@location(0) uv: vec2<f32>,
|
|
|
|
};
|
|
|
|
|
|
|
|
var<private> pos: array<vec2<f32>, 3> = array<vec2<f32>, 3>(
|
|
|
|
vec2(-1.0, 1.0),
|
|
|
|
vec2(-1.0, -3.0),
|
|
|
|
vec2(3.0, 1.0),
|
|
|
|
);
|
|
|
|
var<private> uvs: array<vec2<f32>, 3> = array<vec2<f32>, 3>(
|
|
|
|
vec2(0.0, 0.0),
|
|
|
|
vec2(0.0, 2.0),
|
|
|
|
vec2(2.0, 0.0),
|
|
|
|
);
|
|
|
|
|
2022-08-29 21:07:21 +00:00
|
|
|
@vertex
|
2022-07-27 15:25:25 +00:00
|
|
|
fn vs_main(@builtin(vertex_index) vtxIdx: u32) -> VertexOutput {
|
|
|
|
var out: VertexOutput;
|
|
|
|
out.pos = vec4<f32>(pos[vtxIdx], 0.0, 1.0);
|
|
|
|
out.uv = uvs[vtxIdx];
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-08-29 21:07:21 +00:00
|
|
|
@fragment
|
2022-07-27 15:25:25 +00:00
|
|
|
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
|
|
return textureSample(efb_texture, efb_sampler, in.uv);
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
)""";
|
|
|
|
const wgpu::ShaderModuleDescriptor moduleDescriptor{
|
|
|
|
.nextInChain = &sourceDescriptor,
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = "XFB Copy Module",
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto module = g_device.CreateShaderModule(&moduleDescriptor);
|
|
|
|
const std::array colorTargets{wgpu::ColorTargetState{
|
|
|
|
.format = g_graphicsConfig.swapChainDescriptor.format,
|
|
|
|
.writeMask = wgpu::ColorWriteMask::All,
|
2022-07-27 15:25:25 +00:00
|
|
|
}};
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::FragmentState fragmentState{
|
2022-07-27 15:25:25 +00:00
|
|
|
.module = module,
|
|
|
|
.entryPoint = "fs_main",
|
|
|
|
.targetCount = colorTargets.size(),
|
|
|
|
.targets = colorTargets.data(),
|
|
|
|
};
|
|
|
|
const std::array bindGroupLayoutEntries{
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::BindGroupLayoutEntry{
|
2022-07-27 15:25:25 +00:00
|
|
|
.binding = 0,
|
2022-08-02 20:37:56 +00:00
|
|
|
.visibility = wgpu::ShaderStage::Fragment,
|
2022-07-27 15:25:25 +00:00
|
|
|
.sampler =
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::SamplerBindingLayout{
|
|
|
|
.type = wgpu::SamplerBindingType::Filtering,
|
2022-07-27 15:25:25 +00:00
|
|
|
},
|
|
|
|
},
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::BindGroupLayoutEntry{
|
2022-07-27 15:25:25 +00:00
|
|
|
.binding = 1,
|
2022-08-02 20:37:56 +00:00
|
|
|
.visibility = wgpu::ShaderStage::Fragment,
|
2022-07-27 15:25:25 +00:00
|
|
|
.texture =
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::TextureBindingLayout{
|
|
|
|
.sampleType = wgpu::TextureSampleType::Float,
|
|
|
|
.viewDimension = wgpu::TextureViewDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::BindGroupLayoutDescriptor bindGroupLayoutDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.entryCount = bindGroupLayoutEntries.size(),
|
|
|
|
.entries = bindGroupLayoutEntries.data(),
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
g_CopyBindGroupLayout = g_device.CreateBindGroupLayout(&bindGroupLayoutDescriptor);
|
|
|
|
const wgpu::PipelineLayoutDescriptor layoutDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.bindGroupLayoutCount = 1,
|
|
|
|
.bindGroupLayouts = &g_CopyBindGroupLayout,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto pipelineLayout = g_device.CreatePipelineLayout(&layoutDescriptor);
|
|
|
|
const wgpu::RenderPipelineDescriptor pipelineDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.layout = pipelineLayout,
|
|
|
|
.vertex =
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::VertexState{
|
2022-07-27 15:25:25 +00:00
|
|
|
.module = module,
|
|
|
|
.entryPoint = "vs_main",
|
|
|
|
},
|
|
|
|
.primitive =
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::PrimitiveState{
|
|
|
|
.topology = wgpu::PrimitiveTopology::TriangleList,
|
2022-07-27 15:25:25 +00:00
|
|
|
},
|
|
|
|
.multisample =
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::MultisampleState{
|
2022-07-27 15:25:25 +00:00
|
|
|
.count = 1,
|
|
|
|
.mask = UINT32_MAX,
|
|
|
|
},
|
|
|
|
.fragment = &fragmentState,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
g_CopyPipeline = g_device.CreateRenderPipeline(&pipelineDescriptor);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void create_copy_bind_group() {
|
|
|
|
const std::array bindGroupEntries{
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::BindGroupEntry{
|
2022-07-27 15:25:25 +00:00
|
|
|
.binding = 0,
|
|
|
|
.sampler = g_graphicsConfig.msaaSamples > 1 ? g_frameBufferResolved.sampler : g_frameBuffer.sampler,
|
|
|
|
},
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::BindGroupEntry{
|
2022-07-27 15:25:25 +00:00
|
|
|
.binding = 1,
|
|
|
|
.textureView = g_graphicsConfig.msaaSamples > 1 ? g_frameBufferResolved.view : g_frameBuffer.view,
|
|
|
|
},
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::BindGroupDescriptor bindGroupDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.layout = g_CopyBindGroupLayout,
|
|
|
|
.entryCount = bindGroupEntries.size(),
|
|
|
|
.entries = bindGroupEntries.data(),
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
g_CopyBindGroup = g_device.CreateBindGroup(&bindGroupDescriptor);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 21:06:12 +00:00
|
|
|
static void log_callback(WGPULoggingType type, char const * message, void * userdata) {
|
|
|
|
AuroraLogLevel level = LOG_FATAL;
|
|
|
|
switch (type) {
|
|
|
|
case WGPULoggingType_Verbose:
|
|
|
|
level = LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
case WGPULoggingType_Info:
|
|
|
|
level = LOG_INFO;
|
|
|
|
break;
|
|
|
|
case WGPULoggingType_Warning:
|
|
|
|
level = LOG_WARNING;
|
|
|
|
break;
|
|
|
|
case WGPULoggingType_Error:
|
|
|
|
level = LOG_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Log.report(level, FMT_STRING("WebGPU message: {}"), message);
|
|
|
|
}
|
|
|
|
|
2022-07-27 15:25:25 +00:00
|
|
|
static void error_callback(WGPUErrorType type, char const* message, void* userdata) {
|
2022-08-09 06:05:33 +00:00
|
|
|
FATAL("WebGPU error {}: {}", static_cast<int>(type), message);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
#ifndef WEBGPU_DAWN
|
|
|
|
static void adapter_callback(WGPURequestAdapterStatus status, WGPUAdapter adapter, char const* message,
|
|
|
|
void* userdata) {
|
|
|
|
if (status == WGPURequestAdapterStatus_Success) {
|
|
|
|
g_adapter = wgpu::Adapter::Acquire(adapter);
|
|
|
|
} else {
|
|
|
|
Log.report(LOG_WARNING, FMT_STRING("Adapter request failed with message: {}"), message);
|
|
|
|
}
|
|
|
|
*static_cast<bool*>(userdata) = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-27 15:25:25 +00:00
|
|
|
static void device_callback(WGPURequestDeviceStatus status, WGPUDevice device, char const* message, void* userdata) {
|
|
|
|
if (status == WGPURequestDeviceStatus_Success) {
|
2022-08-02 20:37:56 +00:00
|
|
|
g_device = wgpu::Device::Acquire(device);
|
2022-07-27 15:25:25 +00:00
|
|
|
} else {
|
|
|
|
Log.report(LOG_WARNING, FMT_STRING("Device request failed with message: {}"), message);
|
|
|
|
}
|
|
|
|
*static_cast<bool*>(userdata) = true;
|
|
|
|
}
|
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
static wgpu::BackendType to_wgpu_backend(AuroraBackend backend) {
|
2022-07-27 15:25:25 +00:00
|
|
|
switch (backend) {
|
|
|
|
case BACKEND_WEBGPU:
|
2022-08-02 20:37:56 +00:00
|
|
|
return wgpu::BackendType::WebGPU;
|
2022-07-27 15:25:25 +00:00
|
|
|
case BACKEND_D3D12:
|
2022-08-02 20:37:56 +00:00
|
|
|
return wgpu::BackendType::D3D12;
|
2022-07-27 15:25:25 +00:00
|
|
|
case BACKEND_METAL:
|
2022-08-02 20:37:56 +00:00
|
|
|
return wgpu::BackendType::Metal;
|
2022-07-27 15:25:25 +00:00
|
|
|
case BACKEND_VULKAN:
|
2022-08-02 20:37:56 +00:00
|
|
|
return wgpu::BackendType::Vulkan;
|
2022-07-27 15:25:25 +00:00
|
|
|
case BACKEND_OPENGL:
|
2022-08-02 20:37:56 +00:00
|
|
|
return wgpu::BackendType::OpenGL;
|
2022-07-27 15:25:25 +00:00
|
|
|
case BACKEND_OPENGLES:
|
2022-08-02 20:37:56 +00:00
|
|
|
return wgpu::BackendType::OpenGLES;
|
2022-07-27 15:25:25 +00:00
|
|
|
default:
|
2022-08-02 20:37:56 +00:00
|
|
|
return wgpu::BackendType::Null;
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool initialize(AuroraBackend auroraBackend) {
|
2022-08-02 20:37:56 +00:00
|
|
|
#ifdef WEBGPU_DAWN
|
|
|
|
if (!g_dawnInstance) {
|
2022-07-27 15:25:25 +00:00
|
|
|
Log.report(LOG_INFO, FMT_STRING("Creating Dawn instance"));
|
2022-08-02 20:37:56 +00:00
|
|
|
g_dawnInstance = std::make_unique<dawn::native::Instance>();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (!g_instance) {
|
|
|
|
const wgpu::InstanceDescriptor instanceDescriptor{};
|
|
|
|
g_instance = {}; // TODO use wgpuCreateInstance when supported
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
#endif
|
|
|
|
wgpu::BackendType backend = to_wgpu_backend(auroraBackend);
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
if (backend != wgpu::BackendType::WebGPU) {
|
|
|
|
Log.report(LOG_WARNING, FMT_STRING("Backend type {} unsupported"), magic_enum::enum_name(backend));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
Log.report(LOG_INFO, FMT_STRING("Attempting to initialize {}"), magic_enum::enum_name(backend));
|
|
|
|
#if 0
|
|
|
|
// D3D12's debug layer is very slow
|
2022-08-02 20:37:56 +00:00
|
|
|
g_dawnInstance->EnableBackendValidation(backend != WGPUBackendType::D3D12);
|
2022-07-27 15:25:25 +00:00
|
|
|
#endif
|
2022-08-02 20:37:56 +00:00
|
|
|
|
|
|
|
#ifdef WEBGPU_DAWN
|
2022-07-27 15:25:25 +00:00
|
|
|
SDL_Window* window = window::get_sdl_window();
|
2022-08-02 20:37:56 +00:00
|
|
|
if (!utils::DiscoverAdapter(g_dawnInstance.get(), window, backend)) {
|
2022-07-27 15:25:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
{
|
2022-08-02 20:37:56 +00:00
|
|
|
std::vector<dawn::native::Adapter> adapters = g_dawnInstance->GetAdapters();
|
2022-07-27 15:25:25 +00:00
|
|
|
std::sort(adapters.begin(), adapters.end(), [&](const auto& a, const auto& b) {
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::AdapterProperties propertiesA;
|
|
|
|
wgpu::AdapterProperties propertiesB;
|
2022-07-27 15:25:25 +00:00
|
|
|
a.GetProperties(&propertiesA);
|
|
|
|
b.GetProperties(&propertiesB);
|
|
|
|
constexpr std::array PreferredTypeOrder{
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::AdapterType::DiscreteGPU,
|
|
|
|
wgpu::AdapterType::IntegratedGPU,
|
|
|
|
wgpu::AdapterType::CPU,
|
2022-07-27 15:25:25 +00:00
|
|
|
};
|
|
|
|
const auto typeItA = std::find(PreferredTypeOrder.begin(), PreferredTypeOrder.end(), propertiesA.adapterType);
|
|
|
|
const auto typeItB = std::find(PreferredTypeOrder.begin(), PreferredTypeOrder.end(), propertiesB.adapterType);
|
|
|
|
return typeItA < typeItB;
|
|
|
|
});
|
|
|
|
const auto adapterIt = std::find_if(adapters.begin(), adapters.end(), [=](const auto& adapter) -> bool {
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::AdapterProperties properties;
|
2022-07-27 15:25:25 +00:00
|
|
|
adapter.GetProperties(&properties);
|
|
|
|
return properties.backendType == backend;
|
|
|
|
});
|
|
|
|
if (adapterIt == adapters.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
g_adapter = *adapterIt;
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
#else
|
|
|
|
const WGPUSurfaceDescriptorFromCanvasHTMLSelector canvasDescriptor{
|
|
|
|
.chain = {.sType = WGPUSType_SurfaceDescriptorFromCanvasHTMLSelector},
|
|
|
|
.selector = "#canvas",
|
|
|
|
};
|
|
|
|
const WGPUSurfaceDescriptor surfaceDescriptor{
|
|
|
|
.nextInChain = &canvasDescriptor.chain,
|
|
|
|
.label = "Surface",
|
|
|
|
};
|
|
|
|
g_surface = wgpu::Surface::Acquire(wgpuInstanceCreateSurface(g_instance.Get(), &surfaceDescriptor));
|
2022-08-09 06:05:33 +00:00
|
|
|
ASSERT(g_surface, "Failed to initialize surface");
|
2022-08-02 20:37:56 +00:00
|
|
|
const WGPURequestAdapterOptions options{
|
|
|
|
.compatibleSurface = g_surface.Get(),
|
|
|
|
.powerPreference = WGPUPowerPreference_HighPerformance,
|
|
|
|
.forceFallbackAdapter = false,
|
|
|
|
};
|
|
|
|
bool adapterCallbackRecieved = false;
|
|
|
|
wgpuInstanceRequestAdapter(g_instance.Get(), &options, adapter_callback, &adapterCallbackRecieved);
|
|
|
|
while (!adapterCallbackRecieved) {
|
|
|
|
emscripten_log(EM_LOG_CONSOLE, "Waiting for adapter...\n");
|
|
|
|
emscripten_sleep(100);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
g_adapter.GetProperties(&g_adapterProperties);
|
|
|
|
g_backendType = g_adapterProperties.backendType;
|
2022-07-27 15:25:25 +00:00
|
|
|
const auto backendName = magic_enum::enum_name(g_backendType);
|
2022-08-02 20:37:56 +00:00
|
|
|
const char* adapterName = g_adapterProperties.name;
|
|
|
|
if (adapterName == nullptr) {
|
|
|
|
adapterName = "Unknown";
|
|
|
|
}
|
|
|
|
const char* driverDescription = g_adapterProperties.driverDescription;
|
|
|
|
if (driverDescription == nullptr) {
|
|
|
|
driverDescription = "Unknown";
|
|
|
|
}
|
2022-07-27 15:25:25 +00:00
|
|
|
Log.report(LOG_INFO, FMT_STRING("Graphics adapter information\n API: {}\n Device: {} ({})\n Driver: {}"),
|
2022-08-02 20:37:56 +00:00
|
|
|
backendName, adapterName, magic_enum::enum_name(g_adapterProperties.adapterType), driverDescription);
|
2022-07-27 15:25:25 +00:00
|
|
|
|
|
|
|
{
|
2022-08-02 20:37:56 +00:00
|
|
|
// TODO: emscripten doesn't implement wgpuAdapterGetLimits
|
|
|
|
#ifdef WEBGPU_DAWN
|
2022-07-27 15:25:25 +00:00
|
|
|
WGPUSupportedLimits supportedLimits{};
|
2022-08-02 20:37:56 +00:00
|
|
|
g_adapter.GetLimits(&supportedLimits);
|
|
|
|
const wgpu::RequiredLimits requiredLimits{
|
2022-07-27 15:25:25 +00:00
|
|
|
.limits =
|
|
|
|
{
|
|
|
|
// Use "best" supported alignments
|
|
|
|
.minUniformBufferOffsetAlignment = supportedLimits.limits.minUniformBufferOffsetAlignment == 0
|
|
|
|
? static_cast<uint32_t>(WGPU_LIMIT_U32_UNDEFINED)
|
|
|
|
: supportedLimits.limits.minUniformBufferOffsetAlignment,
|
|
|
|
.minStorageBufferOffsetAlignment = supportedLimits.limits.minStorageBufferOffsetAlignment == 0
|
|
|
|
? static_cast<uint32_t>(WGPU_LIMIT_U32_UNDEFINED)
|
|
|
|
: supportedLimits.limits.minStorageBufferOffsetAlignment,
|
|
|
|
},
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
#endif
|
|
|
|
std::vector<wgpu::FeatureName> features;
|
|
|
|
#ifdef WEBGPU_DAWN
|
|
|
|
const auto supportedFeatures = g_adapter.GetSupportedFeatures();
|
2022-07-27 15:25:25 +00:00
|
|
|
for (const auto* const feature : supportedFeatures) {
|
|
|
|
if (strcmp(feature, "texture-compression-bc") == 0) {
|
2022-08-02 20:37:56 +00:00
|
|
|
features.push_back(wgpu::FeatureName::TextureCompressionBC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
std::vector<wgpu::FeatureName> supportedFeatures;
|
|
|
|
size_t featureCount = g_adapter.EnumerateFeatures(nullptr);
|
|
|
|
supportedFeatures.resize(featureCount);
|
|
|
|
g_adapter.EnumerateFeatures(supportedFeatures.data());
|
|
|
|
for (const auto& feature : supportedFeatures) {
|
|
|
|
if (feature == wgpu::FeatureName::TextureCompressionBC) {
|
|
|
|
features.push_back(wgpu::FeatureName::TextureCompressionBC);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
#endif
|
|
|
|
#ifdef WEBGPU_DAWN
|
2022-07-27 15:25:25 +00:00
|
|
|
const std::array enableToggles {
|
|
|
|
/* clang-format off */
|
|
|
|
#if _WIN32
|
|
|
|
"use_dxc",
|
|
|
|
#endif
|
|
|
|
#ifdef NDEBUG
|
|
|
|
"skip_validation",
|
|
|
|
"disable_robustness",
|
|
|
|
#endif
|
|
|
|
"use_user_defined_labels_in_backend",
|
|
|
|
"disable_symbol_renaming",
|
|
|
|
/* clang-format on */
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::DawnTogglesDeviceDescriptor togglesDescriptor{};
|
|
|
|
togglesDescriptor.forceEnabledTogglesCount = enableToggles.size();
|
|
|
|
togglesDescriptor.forceEnabledToggles = enableToggles.data();
|
|
|
|
#endif
|
|
|
|
const wgpu::DeviceDescriptor deviceDescriptor{
|
|
|
|
#ifdef WEBGPU_DAWN
|
|
|
|
.nextInChain = &togglesDescriptor,
|
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
.requiredFeaturesCount = static_cast<uint32_t>(features.size()),
|
|
|
|
.requiredFeatures = features.data(),
|
2022-08-02 20:37:56 +00:00
|
|
|
#ifdef WEBGPU_DAWN
|
2022-07-27 15:25:25 +00:00
|
|
|
.requiredLimits = &requiredLimits,
|
2022-08-02 20:37:56 +00:00
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
};
|
|
|
|
bool deviceCallbackReceived = false;
|
2022-08-02 20:37:56 +00:00
|
|
|
g_adapter.RequestDevice(&deviceDescriptor, device_callback, &deviceCallbackReceived);
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
while (!deviceCallbackReceived) {
|
|
|
|
emscripten_log(EM_LOG_CONSOLE, "Waiting for device...\n");
|
|
|
|
emscripten_sleep(100);
|
|
|
|
}
|
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
if (!g_device) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-29 21:06:12 +00:00
|
|
|
g_device.SetLoggingCallback(&log_callback, nullptr);
|
2022-08-02 20:37:56 +00:00
|
|
|
g_device.SetUncapturedErrorCallback(&error_callback, nullptr);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
g_device.SetDeviceLostCallback(nullptr, nullptr);
|
|
|
|
g_queue = g_device.GetQueue();
|
2022-07-27 15:25:25 +00:00
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
#if WEBGPU_DAWN
|
|
|
|
g_backendBinding =
|
|
|
|
std::unique_ptr<utils::BackendBinding>(utils::CreateBinding(g_backendType, window, g_device.Get()));
|
|
|
|
if (!g_backendBinding) {
|
2022-07-27 15:25:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
auto swapChainFormat = static_cast<wgpu::TextureFormat>(g_backendBinding->GetPreferredSwapChainTextureFormat());
|
|
|
|
#else
|
|
|
|
auto swapChainFormat = g_surface.GetPreferredFormat(g_adapter);
|
|
|
|
#endif
|
|
|
|
if (swapChainFormat == wgpu::TextureFormat::RGBA8UnormSrgb) {
|
|
|
|
swapChainFormat = wgpu::TextureFormat::RGBA8Unorm;
|
|
|
|
} else if (swapChainFormat == wgpu::TextureFormat::BGRA8UnormSrgb) {
|
|
|
|
swapChainFormat = wgpu::TextureFormat::BGRA8Unorm;
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
Log.report(LOG_INFO, FMT_STRING("Using swapchain format {}"), magic_enum::enum_name(swapChainFormat));
|
2022-08-02 20:37:56 +00:00
|
|
|
const auto size = window::get_window_size();
|
|
|
|
g_graphicsConfig = GraphicsConfig{
|
|
|
|
.swapChainDescriptor =
|
|
|
|
wgpu::SwapChainDescriptor{
|
|
|
|
.usage = wgpu::TextureUsage::RenderAttachment,
|
|
|
|
.format = swapChainFormat,
|
|
|
|
.width = size.fb_width,
|
|
|
|
.height = size.fb_height,
|
|
|
|
.presentMode = wgpu::PresentMode::Fifo,
|
|
|
|
#ifdef WEBGPU_DAWN
|
|
|
|
.implementation = g_backendBinding->GetSwapChainImplementation(),
|
|
|
|
#endif
|
|
|
|
},
|
|
|
|
.depthFormat = wgpu::TextureFormat::Depth32Float,
|
|
|
|
.msaaSamples = g_config.msaa,
|
|
|
|
.textureAnisotropy = g_config.maxTextureAnisotropy,
|
|
|
|
};
|
|
|
|
create_copy_pipeline();
|
|
|
|
resize_swapchain(size.fb_width, size.fb_height, true);
|
2022-07-27 15:25:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown() {
|
2022-08-02 20:37:56 +00:00
|
|
|
g_CopyBindGroupLayout = {};
|
|
|
|
g_CopyPipeline = {};
|
|
|
|
g_CopyBindGroup = {};
|
2022-07-27 15:25:25 +00:00
|
|
|
g_frameBuffer = {};
|
|
|
|
g_frameBufferResolved = {};
|
|
|
|
g_depthBuffer = {};
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpuSwapChainRelease(g_swapChain.Release());
|
|
|
|
wgpuQueueRelease(g_queue.Release());
|
|
|
|
wgpuDeviceDestroy(g_device.Release());
|
|
|
|
g_adapter = {};
|
|
|
|
#ifdef WEBGPU_DAWN
|
|
|
|
g_backendBinding.reset();
|
|
|
|
g_dawnInstance.reset();
|
|
|
|
#else
|
|
|
|
g_surface = {};
|
|
|
|
g_instance = {};
|
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resize_swapchain(uint32_t width, uint32_t height, bool force) {
|
2022-08-02 20:37:56 +00:00
|
|
|
if (!force && g_graphicsConfig.swapChainDescriptor.width == width &&
|
|
|
|
g_graphicsConfig.swapChainDescriptor.height == height) {
|
2022-07-27 15:25:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
g_graphicsConfig.swapChainDescriptor.width = width;
|
|
|
|
g_graphicsConfig.swapChainDescriptor.height = height;
|
|
|
|
#ifdef WEBGPU_DAWN
|
|
|
|
if (!g_swapChain) {
|
|
|
|
g_swapChain = g_device.CreateSwapChain(g_surface, &g_graphicsConfig.swapChainDescriptor);
|
|
|
|
}
|
|
|
|
g_swapChain.Configure(g_graphicsConfig.swapChainDescriptor.format, g_graphicsConfig.swapChainDescriptor.usage, width,
|
|
|
|
height);
|
|
|
|
#else
|
|
|
|
g_swapChain = g_device.CreateSwapChain(g_surface, &g_graphicsConfig.swapChainDescriptor);
|
|
|
|
#endif
|
2022-07-27 15:25:25 +00:00
|
|
|
g_frameBuffer = create_render_texture(true);
|
|
|
|
g_frameBufferResolved = create_render_texture(false);
|
|
|
|
g_depthBuffer = create_depth_texture();
|
|
|
|
create_copy_bind_group();
|
|
|
|
}
|
|
|
|
} // namespace aurora::webgpu
|