mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-21 22:33:49 +00:00
native: basic surface and swapchain creation
This commit is contained in:
parent
2d6bc1762b
commit
68cacd4829
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -104,8 +104,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "cbindgen"
|
||||
version = "0.6.7"
|
||||
source = "git+https://github.com/grovesNL/cbindgen?branch=associated-constants#c87c6774c9f1b9540abac4fc505ed63f0e0a0798"
|
||||
version = "0.6.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -1061,7 +1061,7 @@ dependencies = [
|
||||
name = "wgpu-bindings"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cbindgen 0.6.7 (git+https://github.com/grovesNL/cbindgen?branch=associated-constants)",
|
||||
"cbindgen 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1077,6 +1077,7 @@ dependencies = [
|
||||
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winit 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1180,7 +1181,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
|
||||
"checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
|
||||
"checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d"
|
||||
"checksum cbindgen 0.6.7 (git+https://github.com/grovesNL/cbindgen?branch=associated-constants)" = "<none>"
|
||||
"checksum cbindgen 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "89ae8c2f780373f1842acb548fa53c7fd3acd6ca27d47966c69351719b239eae"
|
||||
"checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749"
|
||||
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
|
||||
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
|
||||
|
@ -11,5 +11,4 @@ edition = "2018"
|
||||
default = []
|
||||
|
||||
[dependencies]
|
||||
#cbindgen = "0.6.7"
|
||||
cbindgen = { git = "https://github.com/grovesNL/cbindgen", branch = "associated-constants"}
|
||||
cbindgen = "0.6.8"
|
||||
|
@ -25,3 +25,4 @@ gfx-backend-vulkan = { version = "0.1.0", optional = true }
|
||||
gfx-backend-dx12 = { version = "0.1.0", optional = true }
|
||||
gfx-backend-metal = { version = "0.1.0", optional = true }
|
||||
#rendy-memory = { git = "https://github.com/rustgd/rendy", rev = "ce7dd7f", features = ["gfx-hal"] }
|
||||
winit = "0.18"
|
||||
|
@ -1,15 +1,18 @@
|
||||
use crate::registry::{Items, HUB};
|
||||
use crate::{back, binding_model, command, conv, pipeline, resource, swap_chain};
|
||||
use crate::registry::{HUB, Items};
|
||||
use crate::track::{BufferTracker, TextureTracker};
|
||||
use crate::{back, binding_model, command, conv, pipeline, resource};
|
||||
use crate::{
|
||||
BindGroupLayoutId, BlendStateId, BufferId, CommandBuffer, CommandBufferId, DepthStencilStateId,
|
||||
DeviceId, LifeGuard, PipelineLayoutId, QueueId, RefCount, RenderPipelineId, ShaderModuleId,
|
||||
Stored, SubmissionIndex, TextureId, TextureUsageFlags, TextureViewId, WeaklyStored,
|
||||
CommandBuffer, LifeGuard, RefCount, Stored, SubmissionIndex, WeaklyStored,
|
||||
TextureUsageFlags,
|
||||
BindGroupLayoutId, BlendStateId, BufferId, CommandBufferId, DepthStencilStateId,
|
||||
AdapterId, DeviceId, PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId,
|
||||
TextureId, TextureViewId,
|
||||
SurfaceId, SwapChainId,
|
||||
};
|
||||
|
||||
use hal::command::RawCommandBuffer;
|
||||
use hal::queue::RawCommandQueue;
|
||||
use hal::{self, Device as _Device};
|
||||
use hal::{self, Device as _Device, Surface as _Surface};
|
||||
//use rendy_memory::{allocator, Config, Heaps};
|
||||
|
||||
use std::collections::hash_map::{Entry, HashMap};
|
||||
@ -124,8 +127,10 @@ impl<B: hal::Backend> DestroyedResources<B> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct Device<B: hal::Backend> {
|
||||
pub(crate) raw: B::Device,
|
||||
adapter_id: WeaklyStored<AdapterId>,
|
||||
queue_group: hal::QueueGroup<B, hal::General>,
|
||||
//mem_allocator: Heaps<B::Memory>,
|
||||
pub(crate) com_allocator: command::CommandAllocator<B>,
|
||||
@ -142,6 +147,7 @@ pub struct Device<B: hal::Backend> {
|
||||
impl<B: hal::Backend> Device<B> {
|
||||
pub(crate) fn new(
|
||||
raw: B::Device,
|
||||
adapter_id: WeaklyStored<AdapterId>,
|
||||
queue_group: hal::QueueGroup<B, hal::General>,
|
||||
mem_props: hal::MemoryProperties,
|
||||
) -> Self {
|
||||
@ -168,6 +174,7 @@ impl<B: hal::Backend> Device<B> {
|
||||
|
||||
Device {
|
||||
raw,
|
||||
adapter_id,
|
||||
//mem_allocator,
|
||||
com_allocator: command::CommandAllocator::new(queue_group.family()),
|
||||
queue_group,
|
||||
@ -823,3 +830,54 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
|
||||
.write()
|
||||
.register(pipeline::RenderPipeline { raw: pipeline })
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wgpu_device_create_swap_chain(
|
||||
device_id: DeviceId,
|
||||
surface_id: SurfaceId,
|
||||
desc: &swap_chain::SwapChainDescriptor,
|
||||
) -> SwapChainId {
|
||||
let device_guard = HUB.devices.read();
|
||||
let device = device_guard.get(device_id);
|
||||
let adapter_guard = HUB.adapters.read();
|
||||
let physical_device = &adapter_guard.get(device.adapter_id.0).physical_device;
|
||||
let mut surface_guard = HUB.surfaces.write();
|
||||
let surface = surface_guard.get_mut(surface_id);
|
||||
|
||||
let (caps, formats, _present_modes, _composite_alphas) = surface.raw.compatibility(physical_device);
|
||||
let config = hal::SwapchainConfig::from_caps(
|
||||
&caps,
|
||||
conv::map_texture_format(desc.format),
|
||||
hal::window::Extent2D {
|
||||
width: desc.width,
|
||||
height: desc.height,
|
||||
},
|
||||
);
|
||||
|
||||
let usage = conv::map_texture_usage(desc.usage, hal::format::Aspects::COLOR);
|
||||
if let Some(formats) = formats {
|
||||
assert!(formats.contains(&config.format));
|
||||
}
|
||||
|
||||
let (raw, backbuffer) = unsafe {
|
||||
device.raw
|
||||
.create_swapchain(
|
||||
&mut surface.raw,
|
||||
config.with_image_usage(usage),
|
||||
None,
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let images = match backbuffer {
|
||||
hal::Backbuffer::Images(images) => images,
|
||||
hal::Backbuffer::Framebuffer(_) => panic!("Deprecated API detected!"),
|
||||
};
|
||||
|
||||
HUB.swap_chains
|
||||
.write()
|
||||
.register(swap_chain::SwapChain {
|
||||
raw,
|
||||
images,
|
||||
})
|
||||
}
|
||||
|
@ -1,7 +1,13 @@
|
||||
use hal::{Instance as _Instance, PhysicalDevice as _PhysicalDevice};
|
||||
#[cfg(not(feature = "winit"))]
|
||||
extern crate winit;
|
||||
|
||||
use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice};
|
||||
|
||||
use crate::registry::{HUB, Items};
|
||||
use crate::{WeaklyStored, Device, Surface,
|
||||
AdapterId, DeviceId, InstanceId, SurfaceId,
|
||||
};
|
||||
|
||||
use crate::registry::{Items, HUB};
|
||||
use crate::{AdapterId, Device, DeviceId, InstanceId};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
|
||||
@ -47,6 +53,25 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "remote"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wgpu_instance_create_surface_from_winit(
|
||||
instance_id: InstanceId,
|
||||
window: &winit::Window,
|
||||
) -> SurfaceId {
|
||||
let raw = HUB.instances
|
||||
.read()
|
||||
.get(instance_id)
|
||||
.create_surface(window);
|
||||
let surface = Surface {
|
||||
raw,
|
||||
};
|
||||
|
||||
HUB.surfaces
|
||||
.write()
|
||||
.register(surface)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wgpu_instance_get_adapter(
|
||||
instance_id: InstanceId,
|
||||
@ -77,9 +102,11 @@ pub extern "C" fn wgpu_adapter_create_device(
|
||||
) -> DeviceId {
|
||||
let mut adapter_guard = HUB.adapters.write();
|
||||
let adapter = adapter_guard.get_mut(adapter_id);
|
||||
let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap();
|
||||
let (raw, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap();
|
||||
let mem_props = adapter.physical_device.memory_properties();
|
||||
let device = Device::new(raw, WeaklyStored(adapter_id), queue_group, mem_props);
|
||||
|
||||
HUB.devices
|
||||
.write()
|
||||
.register(Device::new(device, queue_group, mem_props))
|
||||
.register(device)
|
||||
}
|
||||
|
@ -1,3 +1,6 @@
|
||||
#[cfg(not(feature = "remote"))]
|
||||
extern crate winit;
|
||||
|
||||
#[cfg(feature = "gfx-backend-dx12")]
|
||||
extern crate gfx_backend_dx12 as back;
|
||||
#[cfg(not(any(
|
||||
@ -22,6 +25,7 @@ mod instance;
|
||||
mod pipeline;
|
||||
mod registry;
|
||||
mod resource;
|
||||
mod swap_chain;
|
||||
mod track;
|
||||
|
||||
pub use self::binding_model::*;
|
||||
@ -30,6 +34,7 @@ pub use self::device::*;
|
||||
pub use self::instance::*;
|
||||
pub use self::pipeline::*;
|
||||
pub use self::resource::*;
|
||||
pub use self::swap_chain::*;
|
||||
|
||||
use back::Backend as B;
|
||||
pub use crate::registry::Id;
|
||||
@ -214,3 +219,9 @@ pub type RenderPassId = Id;
|
||||
type RenderPassHandle = RenderPass<B>;
|
||||
pub type ComputePassId = Id;
|
||||
type ComputePassHandle = ComputePass<B>;
|
||||
|
||||
|
||||
pub type SurfaceId = Id;
|
||||
type SurfaceHandle = Surface<B>;
|
||||
pub type SwapChainId = Id;
|
||||
type SwapChainHandle = SwapChain<B>;
|
||||
|
@ -1,16 +1,18 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use parking_lot::RwLock;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::{
|
||||
AdapterHandle, BindGroupHandle, BindGroupLayoutHandle, BlendStateHandle, BufferHandle,
|
||||
CommandBufferHandle, ComputePassHandle, ComputePipelineHandle, DepthStencilStateHandle,
|
||||
DeviceHandle, InstanceHandle, PipelineLayoutHandle, RenderPassHandle, RenderPipelineHandle,
|
||||
ShaderModuleHandle, TextureHandle, TextureViewHandle,
|
||||
AdapterHandle, BindGroupLayoutHandle, BindGroupHandle,
|
||||
BlendStateHandle, CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle,
|
||||
RenderPassHandle, ComputePassHandle,
|
||||
PipelineLayoutHandle, RenderPipelineHandle, ComputePipelineHandle, ShaderModuleHandle,
|
||||
BufferHandle, TextureHandle, TextureViewHandle,
|
||||
SurfaceHandle, SwapChainHandle,
|
||||
};
|
||||
|
||||
|
||||
#[cfg(not(feature = "remote"))]
|
||||
mod local;
|
||||
#[cfg(feature = "remote")]
|
||||
@ -49,6 +51,8 @@ pub struct Hub {
|
||||
pub(crate) buffers: ConcreteRegistry<BufferHandle>,
|
||||
pub(crate) textures: ConcreteRegistry<TextureHandle>,
|
||||
pub(crate) texture_views: ConcreteRegistry<TextureViewHandle>,
|
||||
pub(crate) surfaces: ConcreteRegistry<SurfaceHandle>,
|
||||
pub(crate) swap_chains: ConcreteRegistry<SwapChainHandle>,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
|
21
wgpu-native/src/swap_chain.rs
Normal file
21
wgpu-native/src/swap_chain.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use hal;
|
||||
|
||||
use crate::resource;
|
||||
|
||||
|
||||
pub(crate) struct Surface<B: hal::Backend> {
|
||||
pub raw: B::Surface,
|
||||
}
|
||||
|
||||
pub(crate) struct SwapChain<B: hal::Backend> {
|
||||
pub raw: B::Swapchain,
|
||||
pub images: Vec<B::Image>,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct SwapChainDescriptor {
|
||||
pub usage: resource::TextureUsageFlags,
|
||||
pub format: resource::TextureFormat,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
Loading…
Reference in New Issue
Block a user