mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-21 06:50:59 +01:00
304 lines
7.5 KiB
Rust
304 lines
7.5 KiB
Rust
#![allow(unused_variables)]
|
|
|
|
mod adapter;
|
|
mod command;
|
|
mod conv;
|
|
mod device;
|
|
mod instance;
|
|
|
|
use std::{borrow::Borrow, ffi::CStr, sync::Arc};
|
|
|
|
use arrayvec::ArrayVec;
|
|
use ash::{
|
|
extensions::{ext, khr},
|
|
vk,
|
|
};
|
|
use parking_lot::Mutex;
|
|
|
|
const MILLIS_TO_NANOS: u64 = 1_000_000;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Api;
|
|
#[derive(Debug)]
|
|
pub struct Resource;
|
|
|
|
type DeviceResult<T> = Result<T, crate::DeviceError>;
|
|
|
|
impl crate::Api for Api {
|
|
type Instance = Instance;
|
|
type Surface = Surface;
|
|
type Adapter = Adapter;
|
|
type Device = Device;
|
|
|
|
type Queue = Queue;
|
|
type CommandEncoder = CommandEncoder;
|
|
type CommandBuffer = CommandBuffer;
|
|
|
|
type Buffer = Buffer;
|
|
type Texture = Texture;
|
|
type SurfaceTexture = SurfaceTexture;
|
|
type TextureView = TextureView;
|
|
type Sampler = Sampler;
|
|
type QuerySet = Resource;
|
|
type Fence = Resource;
|
|
|
|
type BindGroupLayout = BindGroupLayout;
|
|
type BindGroup = BindGroup;
|
|
type PipelineLayout = PipelineLayout;
|
|
type ShaderModule = ShaderModule;
|
|
type RenderPipeline = RenderPipeline;
|
|
type ComputePipeline = ComputePipeline;
|
|
}
|
|
|
|
struct DebugUtils {
|
|
extension: ext::DebugUtils,
|
|
messenger: vk::DebugUtilsMessengerEXT,
|
|
}
|
|
|
|
struct InstanceShared {
|
|
raw: ash::Instance,
|
|
flags: crate::InstanceFlag,
|
|
debug_utils: Option<DebugUtils>,
|
|
get_physical_device_properties: Option<vk::KhrGetPhysicalDeviceProperties2Fn>,
|
|
//debug_messenger: Option<DebugMessenger>,
|
|
}
|
|
|
|
pub struct Instance {
|
|
shared: Arc<InstanceShared>,
|
|
extensions: Vec<&'static CStr>,
|
|
entry: ash::Entry,
|
|
}
|
|
|
|
struct Swapchain {
|
|
raw: vk::SwapchainKHR,
|
|
functor: khr::Swapchain,
|
|
device: Arc<DeviceShared>,
|
|
fence: vk::Fence,
|
|
//semaphore: vk::Semaphore,
|
|
images: Vec<vk::Image>,
|
|
}
|
|
|
|
pub struct Surface {
|
|
raw: vk::SurfaceKHR,
|
|
functor: khr::Surface,
|
|
instance: Arc<InstanceShared>,
|
|
swapchain: Option<Swapchain>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct SurfaceTexture {
|
|
index: u32,
|
|
texture: Texture,
|
|
}
|
|
|
|
impl Borrow<Texture> for SurfaceTexture {
|
|
fn borrow(&self) -> &Texture {
|
|
&self.texture
|
|
}
|
|
}
|
|
|
|
pub struct Adapter {
|
|
raw: vk::PhysicalDevice,
|
|
instance: Arc<InstanceShared>,
|
|
queue_families: Vec<vk::QueueFamilyProperties>,
|
|
known_memory_flags: vk::MemoryPropertyFlags,
|
|
phd_capabilities: adapter::PhysicalDeviceCapabilities,
|
|
phd_features: adapter::PhysicalDeviceFeatures,
|
|
downlevel_flags: wgt::DownlevelFlags,
|
|
private_caps: PrivateCapabilities,
|
|
}
|
|
|
|
// TODO there's no reason why this can't be unified--the function pointers should all be the same--it's not clear how to do this with `ash`.
|
|
enum ExtensionFn<T> {
|
|
/// The loaded function pointer struct for an extension.
|
|
Extension(T),
|
|
/// The extension was promoted to a core version of Vulkan and the functions on `ash`'s `DeviceV1_x` traits should be used.
|
|
Promoted,
|
|
}
|
|
|
|
struct DeviceExtensionFunctions {
|
|
draw_indirect_count: Option<ExtensionFn<khr::DrawIndirectCount>>,
|
|
}
|
|
|
|
/// Set of internal capabilities, which don't show up in the exposed
|
|
/// device geometry, but affect the code paths taken internally.
|
|
#[derive(Clone, Debug)]
|
|
struct PrivateCapabilities {
|
|
/// Y-flipping is implemented with either `VK_AMD_negative_viewport_height` or `VK_KHR_maintenance1`/1.1+. The AMD extension for negative viewport height does not require a Y shift.
|
|
///
|
|
/// This flag is `true` if the device has `VK_KHR_maintenance1`/1.1+ and `false` otherwise (i.e. in the case of `VK_AMD_negative_viewport_height`).
|
|
flip_y_requires_shift: bool,
|
|
imageless_framebuffers: bool,
|
|
image_view_usage: bool,
|
|
texture_d24: bool,
|
|
texture_d24_s8: bool,
|
|
}
|
|
|
|
#[derive(Clone, Eq, Hash, PartialEq)]
|
|
struct AttachmentKey {
|
|
format: vk::Format,
|
|
layout_pre: vk::ImageLayout,
|
|
layout_in: vk::ImageLayout,
|
|
layout_post: vk::ImageLayout,
|
|
ops: crate::AttachmentOp,
|
|
}
|
|
|
|
impl AttachmentKey {
|
|
/// Returns an attachment key for a compatible attachment.
|
|
fn compatible(format: vk::Format) -> Self {
|
|
Self {
|
|
format,
|
|
layout_pre: vk::ImageLayout::UNDEFINED,
|
|
layout_in: vk::ImageLayout::UNDEFINED,
|
|
layout_post: vk::ImageLayout::UNDEFINED,
|
|
ops: crate::AttachmentOp::empty(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Eq, Hash, PartialEq)]
|
|
struct ColorAttachmentKey {
|
|
base: AttachmentKey,
|
|
resolve: Option<AttachmentKey>,
|
|
}
|
|
|
|
#[derive(Clone, Eq, Hash, PartialEq)]
|
|
struct DepthStencilAttachmentKey {
|
|
base: AttachmentKey,
|
|
stencil_ops: crate::AttachmentOp,
|
|
}
|
|
|
|
#[derive(Clone, Eq, Default, Hash, PartialEq)]
|
|
struct RenderPassKey {
|
|
colors: ArrayVec<[ColorAttachmentKey; crate::MAX_COLOR_TARGETS]>,
|
|
depth_stencil: Option<DepthStencilAttachmentKey>,
|
|
sample_count: u32,
|
|
}
|
|
|
|
struct DeviceShared {
|
|
raw: ash::Device,
|
|
instance: Arc<InstanceShared>,
|
|
extension_fns: DeviceExtensionFunctions,
|
|
features: wgt::Features,
|
|
vendor_id: u32,
|
|
timestamp_period: f32,
|
|
downlevel_flags: wgt::DownlevelFlags,
|
|
private_caps: PrivateCapabilities,
|
|
render_passes: Mutex<fxhash::FxHashMap<RenderPassKey, vk::RenderPass>>,
|
|
}
|
|
|
|
pub struct Device {
|
|
shared: Arc<DeviceShared>,
|
|
mem_allocator: Mutex<gpu_alloc::GpuAllocator<vk::DeviceMemory>>,
|
|
desc_allocator:
|
|
Mutex<gpu_descriptor::DescriptorAllocator<vk::DescriptorPool, vk::DescriptorSet>>,
|
|
valid_ash_memory_types: u32,
|
|
naga_options: naga::back::spv::Options,
|
|
}
|
|
|
|
pub struct Queue {
|
|
raw: vk::Queue,
|
|
swapchain_fn: khr::Swapchain,
|
|
family_index: u32,
|
|
//device: Arc<DeviceShared>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Buffer {
|
|
raw: vk::Buffer,
|
|
block: Mutex<gpu_alloc::MemoryBlock<vk::DeviceMemory>>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Texture {
|
|
raw: vk::Image,
|
|
block: Option<gpu_alloc::MemoryBlock<vk::DeviceMemory>>,
|
|
aspects: crate::FormatAspect,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct TextureView {
|
|
raw: vk::ImageView,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Sampler {
|
|
raw: vk::Sampler,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct BindGroupLayout {
|
|
raw: vk::DescriptorSetLayout,
|
|
desc_count: gpu_descriptor::DescriptorTotalCount,
|
|
types: Vec<vk::DescriptorType>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct PipelineLayout {
|
|
raw: vk::PipelineLayout,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct BindGroup {
|
|
raw: gpu_descriptor::DescriptorSet<vk::DescriptorSet>,
|
|
}
|
|
|
|
pub struct CommandEncoder {
|
|
raw: vk::CommandPool,
|
|
device: Arc<DeviceShared>,
|
|
active: vk::CommandBuffer,
|
|
free: Vec<vk::CommandBuffer>,
|
|
discarded: Vec<vk::CommandBuffer>,
|
|
}
|
|
|
|
pub struct CommandBuffer {
|
|
raw: vk::CommandBuffer,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ShaderModule {
|
|
raw: vk::ShaderModule,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct RenderPipeline {
|
|
raw: vk::Pipeline,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ComputePipeline {
|
|
raw: vk::Pipeline,
|
|
}
|
|
|
|
impl crate::Queue<Api> for Queue {
|
|
unsafe fn submit(
|
|
&mut self,
|
|
command_buffers: &[&CommandBuffer],
|
|
signal_fence: Option<(&mut Resource, crate::FenceValue)>,
|
|
) -> DeviceResult<()> {
|
|
Ok(())
|
|
}
|
|
unsafe fn present(
|
|
&mut self,
|
|
surface: &mut Surface,
|
|
texture: SurfaceTexture,
|
|
) -> Result<(), crate::SurfaceError> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl From<vk::Result> for crate::DeviceError {
|
|
fn from(result: vk::Result) -> Self {
|
|
match result {
|
|
vk::Result::ERROR_OUT_OF_HOST_MEMORY | vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => {
|
|
Self::OutOfMemory
|
|
}
|
|
vk::Result::ERROR_DEVICE_LOST => Self::Lost,
|
|
_ => {
|
|
log::warn!("Unrecognized device error {:?}", result);
|
|
Self::Lost
|
|
}
|
|
}
|
|
}
|
|
}
|