Simplify registry access

This commit is contained in:
Joshua Groves 2018-09-27 23:56:52 -06:00
parent aab17db5a1
commit e3d7fda9c7
6 changed files with 187 additions and 132 deletions

26
Cargo.lock generated
View File

@ -76,10 +76,10 @@ dependencies = [
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -376,7 +376,7 @@ name = "lazy_static"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -395,7 +395,7 @@ dependencies = [
[[package]]
name = "lock_api"
version = "0.1.3"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -504,7 +504,7 @@ name = "parking_lot"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lock_api 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -659,7 +659,7 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.28"
version = "1.0.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -726,7 +726,7 @@ name = "storage-map"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lock_api 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -815,7 +815,7 @@ dependencies = [
[[package]]
name = "toml"
version = "0.4.6"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)",
@ -851,7 +851,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.4"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -1067,7 +1067,7 @@ dependencies = [
"checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7"
"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
"checksum libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3ad660d7cb8c5822cd83d10897b0f1f1526792737a179e73896152f85b88c2"
"checksum lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "949826a5ccf18c1b3a7c3d57692778d21768b79e46eb9dd07bfc4c2160036c54"
"checksum lock_api 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "775751a3e69bde4df9b38dd00a1b5d6ac13791e4223d4a0506577f0dd27cfb7a"
"checksum log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fcce5fa49cc693c312001daf1d13411c4a5283796bac1084299ea3e567113f"
"checksum malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
"checksum memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2ffa2c986de11a9df78620c01eeaaf27d94d3ff02bf81bfcca953102dd0c6ff"
@ -1100,7 +1100,7 @@ dependencies = [
"checksum serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "84257ccd054dc351472528c8587b4de2dbf0dc0fe2e634030c1a90bfdacebaa9"
"checksum serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "652bc323d694dc925829725ec6c890156d8e70ae5202919869cb00fe2eff3788"
"checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab"
"checksum serde_json 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "d30ec34ac923489285d24688c7a9c0898d16edff27fc1f1bd854edeff6ca3b7f"
"checksum serde_json 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)" = "bb47a3d5c84320222f66d7db21157c4a7407755de41798f9b4c1c40593397b1a"
"checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11"
"checksum smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "153ffa32fd170e9944f7e0838edf824a754ec4c1fc64746fcc9fe1f8fa602e5d"
"checksum smithay-client-toolkit 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f1609083d6bca3991a3c648d80ae16e1764d70881c3321bee1c915149073d605"
@ -1117,13 +1117,13 @@ dependencies = [
"checksum tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "55c1195ef8513f3273d55ff59fe5da6940287a0d7a98331254397f464833675b"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
"checksum toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a0263c6c02c4db6c8f7681f9fd35e90de799ebd4cfdeab77a38f4ff6b3d8c0d9"
"checksum toml 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b7e7d59d55f36979a9dd86d71ae54657a5e9c7fdb4fa2212f4064e2d32f9dcda"
"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7716c242968ee87e5542f8021178248f267f295a5c4803beae8b8b7fd9bc6051"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum wayland-client 0.20.12 (registry+https://github.com/rust-lang/crates.io-index)" = "e7516a23419a55bd2e6d466c75a6a52c85718e5013660603289c2b8bee794b12"
"checksum wayland-commons 0.20.12 (registry+https://github.com/rust-lang/crates.io-index)" = "d8609d59b95bf198bae4f3b064d55a712f2d529eec6aac98cc1f6e9cc911d47a"

View File

@ -31,13 +31,11 @@ int main()
};
WGPUDeviceId device = wgpu_adapter_create_device(adapter, device_desc);
/*
WGPUBindGroupLayoutDescriptor bind_group_layout_desc = {
.bindings = NULL,
.bindings_length = 0,
};
WGPUBindGroupLayoutId _bind_group_layout = wgpu_device_create_bind_group_layout(device, bind_group_layout_desc);
*/
WGPUPipelineLayoutDescriptor pipeline_layout_desc = {
.bind_group_layouts = NULL,
@ -66,9 +64,7 @@ int main()
};
const unsigned int STAGES_LENGTH = 2;
WGPUPipelineStageDescriptor *stages = malloc(STAGES_LENGTH);
stages[0] = vertex_stage;
stages[1] = fragment_stage;
WGPUPipelineStageDescriptor stages[STAGES_LENGTH] = { vertex_stage, fragment_stage };
WGPUBlendDescriptor blend_alpha = {
.src_factor = WGPUBlendFactor_Zero,
@ -88,8 +84,7 @@ int main()
};
WGPUBlendStateId blend_state_0 = wgpu_device_create_blend_state(device, blend_state_0_desc);
const unsigned int BLEND_STATE_LENGTH = 1;
WGPUBlendStateId *blend_state = malloc(BLEND_STATE_LENGTH);
blend_state[0] = blend_state_0;
WGPUBlendStateId blend_state[BLEND_STATE_LENGTH] = { blend_state_0 };
WGPUStencilStateFaceDescriptor stencil_state_front = {
.compare = WGPUCompareFunction_Never,
@ -114,8 +109,7 @@ int main()
WGPUDepthStencilStateId depth_stencil_state = wgpu_device_create_depth_stencil_state(device, depth_stencil_state_desc);
const unsigned int FORMATS_LENGTH = 1;
WGPUTextureFormat *formats = malloc(FORMATS_LENGTH);
formats[0] = WGPUTextureFormat_R8g8b8a8Unorm;
WGPUTextureFormat formats[FORMATS_LENGTH] = { WGPUTextureFormat_R8g8b8a8Unorm };
WGPUAttachmentStateDescriptor attachment_state_desc = {
.formats = formats,
.formats_length = FORMATS_LENGTH,

View File

@ -76,8 +76,8 @@ pub(crate) fn map_shader_stage_flags(
pub(crate) fn map_primitive_topology(
primitive_topology: pipeline::PrimitiveTopology,
) -> hal::Primitive {
use pipeline::PrimitiveTopology::*;
use hal::Primitive as H;
use pipeline::PrimitiveTopology::*;
match primitive_topology {
PointList => H::PointList,
LineList => H::LineList,
@ -102,8 +102,10 @@ pub(crate) fn map_blend_state_descriptor(
}
fn map_color_write_flags(flags: u32) -> hal::pso::ColorMask {
use pipeline::{ColorWriteFlags_RED, ColorWriteFlags_GREEN, ColorWriteFlags_BLUE, ColorWriteFlags_ALPHA};
use hal::pso::ColorMask as H;
use pipeline::{
ColorWriteFlags_ALPHA, ColorWriteFlags_BLUE, ColorWriteFlags_GREEN, ColorWriteFlags_RED,
};
let mut value = H::empty();
if 0 != flags & ColorWriteFlags_RED {
value |= H::RED;
@ -120,25 +122,30 @@ fn map_color_write_flags(flags: u32) -> hal::pso::ColorMask {
value
}
fn map_blend_descriptor(
blend_desc: pipeline::BlendDescriptor,
) -> hal::pso::BlendOp {
use pipeline::BlendOperation::*;
fn map_blend_descriptor(blend_desc: pipeline::BlendDescriptor) -> hal::pso::BlendOp {
use hal::pso::BlendOp as H;
use pipeline::BlendOperation::*;
match blend_desc.operation {
Add => H::Add { src: map_blend_factor(blend_desc.src_factor), dst: map_blend_factor(blend_desc.dst_factor) },
Subtract => H::Sub { src: map_blend_factor(blend_desc.src_factor), dst: map_blend_factor(blend_desc.dst_factor) },
ReverseSubtract => H::RevSub { src: map_blend_factor(blend_desc.src_factor), dst: map_blend_factor(blend_desc.dst_factor) },
Add => H::Add {
src: map_blend_factor(blend_desc.src_factor),
dst: map_blend_factor(blend_desc.dst_factor),
},
Subtract => H::Sub {
src: map_blend_factor(blend_desc.src_factor),
dst: map_blend_factor(blend_desc.dst_factor),
},
ReverseSubtract => H::RevSub {
src: map_blend_factor(blend_desc.src_factor),
dst: map_blend_factor(blend_desc.dst_factor),
},
Min => H::Min,
Max => H::Max,
}
}
fn map_blend_factor(
blend_factor: pipeline::BlendFactor,
) -> hal::pso::Factor {
use pipeline::BlendFactor::*;
fn map_blend_factor(blend_factor: pipeline::BlendFactor) -> hal::pso::Factor {
use hal::pso::Factor as H;
use pipeline::BlendFactor::*;
match blend_factor {
Zero => H::Zero,
One => H::One,
@ -161,7 +168,7 @@ pub(crate) fn map_depth_stencil_state(
) -> hal::pso::DepthStencilDesc {
hal::pso::DepthStencilDesc {
// TODO DepthTest::Off?
depth: hal::pso::DepthTest:: On {
depth: hal::pso::DepthTest::On {
fun: map_compare_function(desc.depth_compare),
write: desc.depth_write_enabled,
},
@ -190,11 +197,9 @@ fn map_stencil_face(
}
}
fn map_compare_function(
compare_function: resource::CompareFunction
) -> hal::pso::Comparison {
use resource::CompareFunction::*;
fn map_compare_function(compare_function: resource::CompareFunction) -> hal::pso::Comparison {
use hal::pso::Comparison as H;
use resource::CompareFunction::*;
match compare_function {
Never => H::Never,
Less => H::Less,
@ -203,15 +208,13 @@ fn map_compare_function(
Greater => H::Greater,
NotEqual => H::NotEqual,
GreaterEqual => H::GreaterEqual,
Always => H::Always
Always => H::Always,
}
}
fn map_stencil_operation(
stencil_operation: pipeline::StencilOperation,
) -> hal::pso::StencilOp {
use pipeline::StencilOperation::*;
fn map_stencil_operation(stencil_operation: pipeline::StencilOperation) -> hal::pso::StencilOp {
use hal::pso::StencilOp as H;
use pipeline::StencilOperation::*;
match stencil_operation {
Keep => H::Keep,
Zero => H::Zero,
@ -224,11 +227,9 @@ fn map_stencil_operation(
}
}
pub(crate) fn map_texture_format(
texture_format: resource::TextureFormat,
) -> hal::format::Format {
use resource::TextureFormat::*;
pub(crate) fn map_texture_format(texture_format: resource::TextureFormat) -> hal::format::Format {
use hal::format::Format as H;
use resource::TextureFormat::*;
match texture_format {
R8g8b8a8Unorm => H::Rgba8Unorm,
R8g8b8a8Uint => H::Rgba8Uint,

View File

@ -2,10 +2,12 @@ use hal::{self, Device as _Device};
use hal::queue::RawCommandQueue;
use {binding_model, command, conv, memory, pipeline, resource};
use registry::{self, Registry};
use std::{iter, slice};
use {binding_model, conv, memory, pipeline};
use {AttachmentStateId, BindGroupLayoutId, BlendStateId, BufferId, CommandBufferId, DepthStencilStateId, DeviceId, PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId};
use registry::{self, Items, Registry};
use {
AttachmentStateId, BindGroupLayoutId, BlendStateId, BufferId, CommandBufferId, DepthStencilStateId, DeviceId,
PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId,
};
pub struct Device<B: hal::Backend> {
@ -40,8 +42,9 @@ pub extern "C" fn wgpu_device_create_bind_group_layout(
desc: binding_model::BindGroupLayoutDescriptor,
) -> BindGroupLayoutId {
let bindings = unsafe { slice::from_raw_parts(desc.bindings, desc.bindings_length) };
let device = &mut registry::DEVICE_REGISTRY.get_mut(device_id).device;
let descriptor_set_layout = device.create_descriptor_set_layout(
let device_guard = registry::DEVICE_REGISTRY.lock();
let device = device_guard.get(device_id);
let descriptor_set_layout = device.device.create_descriptor_set_layout(
bindings.iter().map(|binding| {
hal::pso::DescriptorSetLayoutBinding {
binding: binding.binding,
@ -63,11 +66,16 @@ pub extern "C" fn wgpu_device_create_pipeline_layout(
device_id: DeviceId,
desc: binding_model::PipelineLayoutDescriptor,
) -> PipelineLayoutId {
let bind_group_layout_ids = unsafe { slice::from_raw_parts(desc.bind_group_layouts, desc.bind_group_layouts_length) };
// TODO: only lock mutex once?
let descriptor_set_layouts = bind_group_layout_ids.iter().map(|id| registry::BIND_GROUP_LAYOUT_REGISTRY.get_mut(*id)).collect::<Vec<_>>();
let device = &mut registry::DEVICE_REGISTRY.get_mut(device_id).device;
let pipeline_layout = device.create_pipeline_layout(descriptor_set_layouts.iter().map(|d| &d.raw), &[]); // TODO: push constants
let bind_group_layout_guard = registry::BIND_GROUP_LAYOUT_REGISTRY.lock();
let descriptor_set_layouts =
unsafe { slice::from_raw_parts(desc.bind_group_layouts, desc.bind_group_layouts_length) }
.iter()
.map(|id| bind_group_layout_guard.get(id.clone()))
.collect::<Vec<_>>();
let device_guard = registry::DEVICE_REGISTRY.lock();
let device = &device_guard.get(device_id).device;
let pipeline_layout =
device.create_pipeline_layout(descriptor_set_layouts.iter().map(|d| &d.raw), &[]); // TODO: push constants
registry::PIPELINE_LAYOUT_REGISTRY.register(binding_model::PipelineLayout {
raw: pipeline_layout,
})
@ -76,7 +84,7 @@ pub extern "C" fn wgpu_device_create_pipeline_layout(
#[no_mangle]
pub extern "C" fn wgpu_device_create_blend_state(
_device_id: DeviceId,
desc: pipeline::BlendStateDescriptor
desc: pipeline::BlendStateDescriptor,
) -> BlendStateId {
registry::BLEND_STATE_REGISTRY.register(pipeline::BlendState {
raw: conv::map_blend_state_descriptor(desc),
@ -89,7 +97,7 @@ pub extern "C" fn wgpu_device_create_depth_stencil_state(
desc: pipeline::DepthStencilStateDescriptor,
) -> DepthStencilStateId {
registry::DEPTH_STENCIL_STATE_REGISTRY.register(pipeline::DepthStencilState {
raw: conv::map_depth_stencil_state(desc)
raw: conv::map_depth_stencil_state(desc),
})
}
@ -98,7 +106,8 @@ pub extern "C" fn wgpu_device_create_shader_module(
device_id: DeviceId,
desc: pipeline::ShaderModuleDescriptor,
) -> ShaderModuleId {
let device = &mut registry::DEVICE_REGISTRY.get_mut(device_id).device;
let device_guard = registry::DEVICE_REGISTRY.lock();
let device = &device_guard.get(device_id).device;
let shader = device
.create_shader_module(unsafe { slice::from_raw_parts(desc.code.bytes, desc.code.length) })
.unwrap();
@ -153,26 +162,26 @@ pub extern "C" fn wgpu_device_create_attachment_state(
device_id: DeviceId,
desc: pipeline::AttachmentStateDescriptor,
) -> AttachmentStateId {
// TODO: Either formats should be changed to a single format,
// or its assumed that `AttachmentStateDescriptor` contains multiple attachments.
// Assume the latter for now.
let attachments = unsafe { slice::from_raw_parts(desc.formats, desc.formats_length) }.iter().map(|format| {
hal::pass::Attachment {
// TODO: either formats should be changed to format,
// or return multiple attachments here
format: Some(conv::map_texture_format(*format)),
samples: 1, // TODO map
ops: hal::pass::AttachmentOps { // TODO map
load: hal::pass::AttachmentLoadOp::Clear,
store: hal::pass::AttachmentStoreOp::Store,
},
stencil_ops: hal::pass::AttachmentOps { // TODO map
load: hal::pass::AttachmentLoadOp::DontCare,
store: hal::pass::AttachmentStoreOp::DontCare,
},
layouts: hal::image::Layout::Undefined..hal::image::Layout::Present, // TODO map
}
}).collect();
// TODO: Assume that `AttachmentStateDescriptor` contains multiple attachments.
let attachments = unsafe { slice::from_raw_parts(desc.formats, desc.formats_length) }
.iter()
.map(|format| {
hal::pass::Attachment {
format: Some(conv::map_texture_format(*format)),
samples: 1, // TODO map
ops: hal::pass::AttachmentOps {
// TODO map
load: hal::pass::AttachmentLoadOp::Clear,
store: hal::pass::AttachmentStoreOp::Store,
},
stencil_ops: hal::pass::AttachmentOps {
// TODO map
load: hal::pass::AttachmentLoadOp::DontCare,
store: hal::pass::AttachmentStoreOp::DontCare,
},
layouts: hal::image::Layout::Undefined..hal::image::Layout::Present, // TODO map
}
}).collect();
registry::ATTACHMENT_STATE_REGISTRY.register(pipeline::AttachmentState { raw: attachments })
}
@ -182,29 +191,31 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
desc: pipeline::RenderPipelineDescriptor,
) -> RenderPipelineId {
// TODO
let extent = hal::window::Extent2D { width: 100, height: 100 };
let device = &registry::DEVICE_REGISTRY.get_mut(device_id).device;
let layout = &registry::PIPELINE_LAYOUT_REGISTRY.get_mut(desc.layout).raw;
let extent = hal::window::Extent2D {
width: 100,
height: 100,
};
let device_guard = registry::DEVICE_REGISTRY.lock();
let device = &device_guard.get(device_id).device;
let pipeline_layout_guard = registry::PIPELINE_LAYOUT_REGISTRY.lock();
let layout = &pipeline_layout_guard.get(desc.layout).raw;
let pipeline_stages = unsafe { slice::from_raw_parts(desc.stages, desc.stages_length) };
// TODO: avoid allocation
let shaders_owned = pipeline_stages
.iter()
.map(|ps| registry::SHADER_MODULE_REGISTRY.get_mut(ps.module))
.collect::<Vec<_>>();
let shader_module_guard = registry::SHADER_MODULE_REGISTRY.lock();
let shaders = {
let mut vertex = None;
let mut fragment = None;
for (i, pipeline_stage) in pipeline_stages.iter().enumerate() {
for pipeline_stage in pipeline_stages.iter() {
let entry_name = unsafe { ffi::CStr::from_ptr(pipeline_stage.entry_point) }
.to_str()
.to_owned()
.unwrap();
let entry = hal::pso::EntryPoint::<back::Backend> {
entry: unsafe { ffi::CStr::from_ptr(pipeline_stage.entry_point) }
.to_str()
.to_owned()
.unwrap(), // TODO
module: &shaders_owned[i].raw,
module: &shader_module_guard.get(pipeline_stage.module).raw,
specialization: hal::pso::Specialization {
// TODO
constants: &[],
@ -252,15 +263,19 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
primitive_restart: hal::pso::PrimitiveRestart::Disabled, // TODO
};
let blend_state_guard = registry::BLEND_STATE_REGISTRY.lock();
let blend_state = unsafe { slice::from_raw_parts(desc.blend_state, desc.blend_state_length) }
.iter()
.map(|id| blend_state_guard.get(id.clone()).raw)
.collect();
let blender = hal::pso::BlendDesc {
logic_op: None, // TODO
targets: unsafe { slice::from_raw_parts(desc.blend_state, desc.blend_state_length) }
.iter()
.map(|id| registry::BLEND_STATE_REGISTRY.get_mut(*id).raw)
.collect()
targets: blend_state,
};
let depth_stencil = registry::DEPTH_STENCIL_STATE_REGISTRY.get_mut(desc.depth_stencil_state).raw;
let depth_stencil_state_guard = registry::DEPTH_STENCIL_STATE_REGISTRY.lock();
let depth_stencil = depth_stencil_state_guard.get(desc.depth_stencil_state).raw;
// TODO
let multisampling: Option<hal::pso::Multisampling> = None;
@ -286,7 +301,8 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
depth_bounds: None,
};
let attachments = &registry::ATTACHMENT_STATE_REGISTRY.get_mut(desc.attachment_state).raw;
let attachment_state_guard = registry::ATTACHMENT_STATE_REGISTRY.lock();
let attachments = &attachment_state_guard.get(desc.attachment_state).raw;
// TODO
let subpass = hal::pass::SubpassDesc {
@ -300,8 +316,11 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
// TODO
let subpass_dependency = hal::pass::SubpassDependency {
passes: hal::pass::SubpassRef::External..hal::pass::SubpassRef::Pass(0),
stages: hal::pso::PipelineStage::COLOR_ATTACHMENT_OUTPUT..hal::pso::PipelineStage::COLOR_ATTACHMENT_OUTPUT,
accesses: hal::image::Access::empty()..(hal::image::Access::COLOR_ATTACHMENT_READ | hal::image::Access::COLOR_ATTACHMENT_WRITE),
stages: hal::pso::PipelineStage::COLOR_ATTACHMENT_OUTPUT
..hal::pso::PipelineStage::COLOR_ATTACHMENT_OUTPUT,
accesses: hal::image::Access::empty()
..(hal::image::Access::COLOR_ATTACHMENT_READ
| hal::image::Access::COLOR_ATTACHMENT_WRITE),
};
let main_pass = &device.create_render_pass(&attachments[..], &[subpass], &[subpass_dependency]);
@ -335,7 +354,9 @@ pub extern "C" fn wgpu_device_create_render_pipeline(
};
// TODO: cache
let pipeline = device.create_graphics_pipeline(&pipeline_desc, None).unwrap();
let pipeline = device
.create_graphics_pipeline(&pipeline_desc, None)
.unwrap();
registry::RENDER_PIPELINE_REGISTRY.register(pipeline::RenderPipeline { raw: pipeline })
}

View File

@ -1,6 +1,6 @@
use hal::{self, Instance as _Instance, PhysicalDevice as _PhysicalDevice};
use registry::{self, Registry};
use registry::{self, Items, Registry};
use {AdapterId, Device, DeviceId, InstanceId};
#[repr(C)]
@ -52,7 +52,8 @@ pub extern "C" fn wgpu_instance_get_adapter(
instance_id: InstanceId,
desc: AdapterDescriptor,
) -> AdapterId {
let instance = registry::INSTANCE_REGISTRY.get_mut(instance_id);
let instance_guard = registry::INSTANCE_REGISTRY.lock();
let instance = instance_guard.get(instance_id);
let (mut low, mut high, mut other) = (None, None, None);
for adapter in instance.enumerate_adapters() {
match adapter.info.device_type {
@ -74,7 +75,8 @@ pub extern "C" fn wgpu_adapter_create_device(
adapter_id: AdapterId,
_desc: DeviceDescriptor,
) -> DeviceId {
let mut adapter = registry::ADAPTER_REGISTRY.get_mut(adapter_id);
let mut adapter_guard = registry::ADAPTER_REGISTRY.lock();
let adapter = adapter_guard.get_mut(adapter_id);
let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap();
let mem_props = adapter.physical_device.memory_properties();
registry::DEVICE_REGISTRY.register(Device::new(device, queue_group, mem_props))

View File

@ -1,35 +1,61 @@
#[cfg(feature = "remote")]
use hal::backend::FastHashMap;
#[cfg(feature = "remote")]
use parking_lot::{MappedMutexGuard, Mutex, MutexGuard};
use parking_lot::{Mutex, MutexGuard};
#[cfg(not(feature = "remote"))]
use std::marker::PhantomData;
#[cfg(not(feature = "remote"))]
use std::os::raw::c_void;
#[cfg(feature = "remote")]
use std::sync::Arc;
#[cfg(feature = "remote")]
use hal::backend::FastHashMap;
use {AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BlendStateHandle, CommandBufferHandle, DepthStencilStateHandle, DeviceHandle, InstanceHandle, PipelineLayoutHandle, RenderPipelineHandle, ShaderModuleHandle};
use {
AdapterHandle, AttachmentStateHandle, BindGroupLayoutHandle, BlendStateHandle, CommandBufferHandle,
DepthStencilStateHandle, DeviceHandle, InstanceHandle, PipelineLayoutHandle,
RenderPipelineHandle, ShaderModuleHandle,
};
#[cfg(not(feature = "remote"))]
pub(crate) type Id = *mut c_void;
#[cfg(feature = "remote")]
pub(crate) type Id = u32;
type Item<'a, T> = &'a T;
type ItemMut<'a, T> = &'a mut T;
#[cfg(not(feature = "remote"))]
type RegistryItem<'a, T> = &'a mut T;
type ItemsGuard<'a, T> = LocalItems<T>;
#[cfg(feature = "remote")]
type RegistryItem<'a, T> = MappedMutexGuard<'a, T>;
type ItemsGuard<'a, T> = MutexGuard<'a, RemoteItems<T>>;
pub(crate) trait Registry<T> {
fn new() -> Self;
fn register(&self, handle: T) -> Id;
fn get_mut(&self, id: Id) -> RegistryItem<T>;
fn lock(&self) -> ItemsGuard<T>;
}
pub(crate) trait Items<T> {
fn get(&self, id: Id) -> Item<T>;
fn get_mut(&mut self, id: Id) -> ItemMut<T>;
fn take(&self, id: Id) -> T;
}
#[cfg(not(feature = "remote"))]
pub(crate) struct LocalItems<T> {
marker: PhantomData<T>,
}
#[cfg(not(feature = "remote"))]
impl<T> Items<T> for LocalItems<T> {
fn get(&self, id: Id) -> Item<T> {
unsafe { (id as *mut T).as_ref() }.unwrap()
}
fn get_mut(&mut self, id: Id) -> ItemMut<T> {
unsafe { (id as *mut T).as_mut() }.unwrap()
}
}
#[cfg(not(feature = "remote"))]
pub(crate) struct LocalRegistry<T> {
marker: PhantomData<T>,
@ -47,8 +73,10 @@ impl<T> Registry<T> for LocalRegistry<T> {
Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void
}
fn get_mut(&self, id: Id) -> RegistryItem<T> {
unsafe { (id as *mut T).as_mut() }.unwrap()
fn lock(&self) -> ItemsGuard<T> {
LocalItems {
marker: PhantomData,
}
}
fn take(&self, id: Id) -> T {
@ -59,16 +87,16 @@ impl<T> Registry<T> for LocalRegistry<T> {
}
#[cfg(feature = "remote")]
struct Registrations<T> {
pub(crate) struct RemoteItems<T> {
next_id: Id,
tracked: FastHashMap<Id, T>,
free: Vec<Id>,
}
#[cfg(feature = "remote")]
impl<T> Registrations<T> {
impl<T> RemoteItems<T> {
fn new() -> Self {
Registrations {
RemoteItems {
next_id: 0,
tracked: FastHashMap::default(),
free: Vec::new(),
@ -76,36 +104,45 @@ impl<T> Registrations<T> {
}
}
#[cfg(feature = "remote")]
impl<T> Items<T> for RemoteItems<T> {
fn get(&self, id: Id) -> Item<T> {
self.tracked.get(&id).unwrap()
}
fn get_mut(&mut self, id: Id) -> ItemMut<T> {
self.tracked.get_mut(&id).unwrap()
}
}
#[cfg(feature = "remote")]
pub(crate) struct RemoteRegistry<T> {
registrations: Arc<Mutex<Registrations<T>>>,
items: Arc<Mutex<RemoteItems<T>>>,
}
#[cfg(feature = "remote")]
impl<T> Registry<T> for RemoteRegistry<T> {
fn new() -> Self {
RemoteRegistry {
registrations: Arc::new(Mutex::new(Registrations::new())),
items: Arc::new(Mutex::new(RemoteItems::new())),
}
}
fn register(&self, handle: T) -> Id {
let mut registrations = self.registrations.lock();
let id = match registrations.free.pop() {
let mut items = self.items.lock();
let id = match items.free.pop() {
Some(id) => id,
None => {
registrations.next_id += 1;
registrations.next_id - 1
items.next_id += 1;
items.next_id - 1
}
};
registrations.tracked.insert(id, handle);
items.tracked.insert(id, handle);
id
}
fn get_mut(&self, id: Id) -> RegistryItem<T> {
MutexGuard::map(self.registrations.lock(), |r| {
r.tracked.get_mut(&id).unwrap()
})
fn lock(&self) -> ItemsGuard<T> {
self.items.lock()
}
fn take(&self, id: Id) -> T {