Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - enable wgpu device features #547

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
enable wgpu device features
  • Loading branch information
verzuz committed Sep 21, 2020
commit ff1c511cd7783b390bab793f6ca7a0a1fc31eb70
64 changes: 62 additions & 2 deletions crates/bevy_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,54 @@ use bevy_ecs::{IntoQuerySystem, IntoThreadLocalSystem, Resources, World};
use bevy_render::renderer::{free_shared_buffers_system, RenderResourceContext, SharedBuffers};
use renderer::WgpuRenderResourceContext;

#[derive(Clone, Copy)]
pub enum DeviceFeatures {
DepthClamping,
TextureCompressionBC,
MappablePrimaryBuffers,
SampledTextureBindingArray,
SampledTextureArrayDynamicIndexing,
SampledTextureArrayNonUniformIndexing,
UnsizedBindingArray,
MultiDrawIndirect,
MultiDrawIndirectCount,
PushConstants,
AddressModeClampToBorder,
NonFillPolygonMode,
}

impl From<DeviceFeatures> for wgpu::Features {
fn from(value: DeviceFeatures) -> Self {
match value {
DeviceFeatures::DepthClamping => wgpu::Features::DEPTH_CLAMPING,
DeviceFeatures::TextureCompressionBC => wgpu::Features::TEXTURE_COMPRESSION_BC,
DeviceFeatures::MappablePrimaryBuffers => wgpu::Features::MAPPABLE_PRIMARY_BUFFERS,
DeviceFeatures::SampledTextureBindingArray => {
wgpu::Features::SAMPLED_TEXTURE_BINDING_ARRAY
}
DeviceFeatures::SampledTextureArrayDynamicIndexing => {
wgpu::Features::SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING
}
DeviceFeatures::SampledTextureArrayNonUniformIndexing => {
wgpu::Features::SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
}
DeviceFeatures::UnsizedBindingArray => wgpu::Features::UNSIZED_BINDING_ARRAY,
DeviceFeatures::MultiDrawIndirect => wgpu::Features::MULTI_DRAW_INDIRECT,
DeviceFeatures::MultiDrawIndirectCount => wgpu::Features::MULTI_DRAW_INDIRECT_COUNT,
DeviceFeatures::PushConstants => wgpu::Features::PUSH_CONSTANTS,
DeviceFeatures::AddressModeClampToBorder => {
wgpu::Features::ADDRESS_MODE_CLAMP_TO_BORDER
}
DeviceFeatures::NonFillPolygonMode => wgpu::Features::NON_FILL_POLYGON_MODE,
}
}
}

#[derive(Default)]
pub struct WgpuDeviceFeatures {
pub features: Vec<DeviceFeatures>,
}

#[derive(Default)]
pub struct WgpuPlugin;

Expand All @@ -31,9 +79,21 @@ impl Plugin for WgpuPlugin {
);
}
}

pub fn wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut World, &mut Resources) {
let mut wgpu_renderer = future::block_on(WgpuRenderer::new());

let mut wgpu_features = wgpu::Features::empty();
if let Some(device_features_res) = resources.get::<WgpuDeviceFeatures>() {
wgpu_features = device_features_res.features.iter().fold(
wgpu::Features::empty(),
|wgpu_features, feature| {
let feature: wgpu::Features = (*feature).into();
let wgpu_features = wgpu_features | feature;
wgpu_features
},
);
}

let mut wgpu_renderer = future::block_on(WgpuRenderer::new(wgpu_features));
let resource_context = WgpuRenderResourceContext::new(wgpu_renderer.device.clone());
resources.insert::<Box<dyn RenderResourceContext>>(Box::new(resource_context.clone()));
resources.insert(SharedBuffers::new(Box::new(resource_context)));
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_wgpu/src/wgpu_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct WgpuRenderer {
}

impl WgpuRenderer {
pub async fn new() -> Self {
pub async fn new(features : wgpu::Features) -> Self {
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand All @@ -35,7 +35,7 @@ impl WgpuRenderer {
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
features: wgpu::Features::empty(),
features: features,
limits: wgpu::Limits::default(),
shader_validation: true,
},
Expand Down