upgrade to latest wgpu

This commit is contained in:
Carter Anderson 2020-08-13 13:58:36 -07:00
parent 7a79dcc46c
commit 7c3b49cb6f
10 changed files with 99 additions and 57 deletions

View file

@ -3,7 +3,7 @@ use bevy_render::{
pipeline::{
BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite,
CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, PipelineDescriptor,
RasterizationStateDescriptor, StencilStateFaceDescriptor,
RasterizationStateDescriptor, StencilStateFaceDescriptor, StencilStateDescriptor,
},
shader::{Shader, ShaderStage, ShaderStages},
texture::TextureFormat,
@ -20,15 +20,18 @@ pub(crate) fn build_forward_pipeline(shaders: &mut Assets<Shader>) -> PipelineDe
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil_front: StencilStateFaceDescriptor::IGNORE,
stencil_back: StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
read_mask: 0,
write_mask: 0,
},
}),
color_states: vec![ColorStateDescriptor {
format: TextureFormat::Bgra8UnormSrgb,

View file

@ -4,7 +4,7 @@ use super::{
CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, IndexFormat,
PrimitiveTopology, RasterizationStateDescriptor, StencilStateFaceDescriptor,
},
BindType, DynamicBinding, PipelineLayout, VertexBufferDescriptors,
BindType, DynamicBinding, PipelineLayout, VertexBufferDescriptors, StencilStateDescriptor,
};
use crate::{
shader::{Shader, ShaderStages},
@ -77,15 +77,18 @@ impl PipelineDescriptor {
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil_front: StencilStateFaceDescriptor::IGNORE,
stencil_back: StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
read_mask: 0,
write_mask: 0,
},
}),
color_states: vec![ColorStateDescriptor {
format: TextureFormat::Bgra8UnormSrgb,

View file

@ -7,11 +7,17 @@ pub struct DepthStencilStateDescriptor {
pub format: TextureFormat,
pub depth_write_enabled: bool,
pub depth_compare: CompareFunction,
pub stencil_front: StencilStateFaceDescriptor,
pub stencil_back: StencilStateFaceDescriptor,
pub stencil_read_mask: u32,
pub stencil_write_mask: u32,
pub stencil: StencilStateDescriptor,
}
#[derive(Clone, Debug)]
pub struct StencilStateDescriptor {
pub front: StencilStateFaceDescriptor,
pub back: StencilStateFaceDescriptor,
pub read_mask: u32,
pub write_mask: u32,
}
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum StencilOperation {
Keep = 0,
@ -100,6 +106,7 @@ pub struct RasterizationStateDescriptor {
pub depth_bias: i32,
pub depth_bias_slope_scale: f32,
pub depth_bias_clamp: f32,
pub clamp_depth: bool,
}
#[derive(Clone, Debug)]

View file

@ -10,15 +10,15 @@ use std::{
pub struct BindGroupId(pub u64);
#[derive(Eq, PartialEq, Debug)]
pub struct IndexedBinding {
pub struct IndexedBindGroupEntry {
pub index: u32,
pub binding: RenderResourceBinding,
pub entry: RenderResourceBinding,
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct BindGroup {
pub id: BindGroupId,
pub indexed_bindings: Arc<Vec<IndexedBinding>>,
pub indexed_bindings: Arc<Vec<IndexedBindGroupEntry>>,
pub dynamic_uniform_indices: Option<Arc<Vec<u32>>>,
}
@ -30,7 +30,7 @@ impl BindGroup {
#[derive(Default)]
pub struct BindGroupBuilder {
pub indexed_bindings: Vec<IndexedBinding>,
pub indexed_bindings: Vec<IndexedBindGroupEntry>,
pub dynamic_uniform_indices: Vec<u32>,
pub hasher: DefaultHasher,
}
@ -47,7 +47,7 @@ impl BindGroupBuilder {
binding.hash(&mut self.hasher);
self.indexed_bindings
.push(IndexedBinding { index, binding });
.push(IndexedBindGroupEntry { index, entry: binding });
self
}

View file

@ -1,5 +1,6 @@
use super::Texture;
use crate::pipeline::CompareFunction;
use std::num::NonZeroU8;
/// Describes a sampler
#[derive(Copy, Clone)]
@ -13,7 +14,7 @@ pub struct SamplerDescriptor {
pub lod_min_clamp: f32,
pub lod_max_clamp: f32,
pub compare_function: Option<CompareFunction>,
pub anisotropy_clamp: Option<u8>,
pub anisotropy_clamp: Option<NonZeroU8>,
}
impl Default for SamplerDescriptor {

View file

@ -5,7 +5,7 @@ use bevy_render::{
pipeline::{
BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite,
CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, PipelineDescriptor,
RasterizationStateDescriptor, StencilStateFaceDescriptor,
RasterizationStateDescriptor, StencilStateDescriptor, StencilStateFaceDescriptor,
},
render_graph::{base, AssetRenderResourcesNode, RenderGraph, RenderResourcesNode},
shader::{Shader, ShaderStage, ShaderStages},
@ -26,15 +26,18 @@ pub fn build_sprite_sheet_pipeline(shaders: &mut Assets<Shader>) -> PipelineDesc
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil_front: StencilStateFaceDescriptor::IGNORE,
stencil_back: StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
read_mask: 0,
write_mask: 0,
},
}),
color_states: vec![ColorStateDescriptor {
format: TextureFormat::Bgra8UnormSrgb,
@ -71,15 +74,18 @@ pub fn build_sprite_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil_front: StencilStateFaceDescriptor::IGNORE,
stencil_back: StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
read_mask: 0,
write_mask: 0,
},
}),
color_states: vec![ColorStateDescriptor {
format: TextureFormat::Bgra8UnormSrgb,

View file

@ -28,15 +28,18 @@ pub fn build_ui_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil_front: StencilStateFaceDescriptor::IGNORE,
stencil_back: StencilStateFaceDescriptor::IGNORE,
stencil_read_mask: 0,
stencil_write_mask: 0,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
read_mask: 0,
write_mask: 0,
},
}),
color_states: vec![ColorStateDescriptor {
format: TextureFormat::Bgra8UnormSrgb,

View file

@ -25,7 +25,7 @@ bevy_window = { path = "../bevy_window", version = "0.1" }
bevy_winit = { path = "../bevy_winit", optional = true, version = "0.1" }
# other
wgpu = { version = "0.1.0", package = "cart-tmp-wgpu" }
wgpu = { git = "https://github.com/gfx-rs/wgpu-rs", rev = "333aeb73c103037abdae62abc078c4fcf745aef7" }
pollster = "0.2.0"
log = { version = "0.4", features = ["release_max_level_info"] }
crossbeam-channel = "0.4.2"

View file

@ -16,7 +16,8 @@ use bevy_render::{
texture::{Extent3d, SamplerDescriptor, TextureDescriptor},
};
use bevy_window::{Window, WindowId};
use std::{ops::Range, sync::Arc};
use std::{ops::Range, sync::Arc, borrow::Cow};
use wgpu::util::DeviceExt;
#[derive(Clone)]
pub struct WgpuRenderResourceContext {
@ -111,7 +112,7 @@ impl WgpuRenderResourceContext {
let mut bind_group_layouts = self.resources.bind_group_layouts.write();
// TODO: consider re-checking existence here
let bind_group_layout_binding = descriptor
let bind_group_layout_entries = descriptor
.bindings
.iter()
.map(|binding| {
@ -126,15 +127,16 @@ impl WgpuRenderResourceContext {
} else {
panic!("Invalid binding shader stage.")
};
wgpu::BindGroupLayoutEntry::new(
binding.index,
shader_stage,
(&binding.bind_type).wgpu_into(),
)
wgpu::BindGroupLayoutEntry {
binding: binding.index,
visibility: shader_stage,
ty: (&binding.bind_type).wgpu_into(),
count: None,
}
})
.collect::<Vec<wgpu::BindGroupLayoutEntry>>();
let wgpu_descriptor = wgpu::BindGroupLayoutDescriptor {
bindings: bind_group_layout_binding.as_slice(),
entries: bind_group_layout_entries.as_slice(),
label: None,
};
let bind_group_layout = self.device.create_bind_group_layout(&wgpu_descriptor);
@ -146,7 +148,7 @@ impl WgpuRenderResourceContext {
let mut swap_chain_outputs = self.resources.swap_chain_frames.write();
let window_swap_chain = window_swap_chains.get_mut(&window_id).unwrap();
let next_texture = window_swap_chain.get_next_frame().ok()?;
let next_texture = window_swap_chain.get_current_frame().ok()?;
let id = TextureId::new();
swap_chain_outputs.insert(id, next_texture);
Some(id)
@ -172,7 +174,7 @@ impl RenderResourceContext for WgpuRenderResourceContext {
let descriptor: wgpu::TextureDescriptor = (&texture_descriptor).wgpu_into();
let texture = self.device.create_texture(&descriptor);
let texture_view = texture.create_default_view();
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let id = TextureId::new();
texture_descriptors.insert(id, texture_descriptor);
@ -207,7 +209,11 @@ impl RenderResourceContext for WgpuRenderResourceContext {
buffer_info.size = data.len();
let buffer = self
.device
.create_buffer_with_data(data, buffer_info.buffer_usage.wgpu_into());
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
contents: data,
label: None,
usage: buffer_info.buffer_usage.wgpu_into(),
});
let id = BufferId::new();
buffer_infos.insert(id, buffer_info);
@ -240,9 +246,10 @@ impl RenderResourceContext for WgpuRenderResourceContext {
fn create_shader_module_from_source(&self, shader_handle: Handle<Shader>, shader: &Shader) {
let mut shader_modules = self.resources.shader_modules.write();
let spirv: Cow<[u32]> = shader.get_spirv(None).into();
let shader_module = self
.device
.create_shader_module(wgpu::ShaderModuleSource::SpirV(&shader.get_spirv(None)));
.create_shader_module(wgpu::ShaderModuleSource::SpirV(spirv));
shader_modules.insert(shader_handle, shader_module);
}
@ -352,7 +359,9 @@ impl RenderResourceContext for WgpuRenderResourceContext {
let pipeline_layout = self
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: bind_group_layouts.as_slice(),
push_constant_ranges: &[],
});
let owned_vertex_buffer_descriptors = layout
@ -384,7 +393,8 @@ impl RenderResourceContext for WgpuRenderResourceContext {
};
let render_pipeline_descriptor = wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
label: None,
layout: Some(&pipeline_layout),
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vertex_shader_module,
entry_point: "main",
@ -452,11 +462,11 @@ impl RenderResourceContext for WgpuRenderResourceContext {
let bind_group_layouts = self.resources.bind_group_layouts.read();
let mut bind_groups = self.resources.bind_groups.write();
let bindings = bind_group
let entries = bind_group
.indexed_bindings
.iter()
.map(|indexed_binding| {
let wgpu_resource = match &indexed_binding.binding {
let wgpu_resource = match &indexed_binding.entry {
RenderResourceBinding::Texture(resource) => {
let texture_view = texture_views
.get(&resource)
@ -472,18 +482,18 @@ impl RenderResourceContext for WgpuRenderResourceContext {
wgpu::BindingResource::Buffer(wgpu_buffer.slice(range.clone()))
}
};
wgpu::Binding {
wgpu::BindGroupEntry {
binding: indexed_binding.index,
resource: wgpu_resource,
}
})
.collect::<Vec<wgpu::Binding>>();
.collect::<Vec<wgpu::BindGroupEntry>>();
let bind_group_layout = bind_group_layouts.get(&bind_group_descriptor_id).unwrap();
let wgpu_bind_group_descriptor = wgpu::BindGroupDescriptor {
label: None,
layout: bind_group_layout,
bindings: bindings.as_slice(),
entries: entries.as_slice(),
};
let wgpu_bind_group = self.device.create_bind_group(&wgpu_bind_group_descriptor);

View file

@ -6,7 +6,7 @@ use bevy_render::{
CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, IndexFormat,
InputStepMode, PrimitiveTopology, RasterizationStateDescriptor, StencilOperation,
StencilStateFaceDescriptor, VertexAttributeDescriptor, VertexBufferDescriptor,
VertexFormat,
VertexFormat, StencilStateDescriptor,
},
renderer::BufferUsage,
texture::{
@ -322,16 +322,24 @@ impl WgpuFrom<TextureUsage> for wgpu::TextureUsage {
}
}
impl WgpuFrom<&StencilStateDescriptor> for wgpu::StencilStateDescriptor {
fn from(val: &StencilStateDescriptor) -> Self {
wgpu::StencilStateDescriptor {
back: (&val.back).wgpu_into(),
front: (&val.front).wgpu_into(),
read_mask: val.read_mask,
write_mask: val.write_mask,
}
}
}
impl WgpuFrom<&DepthStencilStateDescriptor> for wgpu::DepthStencilStateDescriptor {
fn from(val: &DepthStencilStateDescriptor) -> Self {
wgpu::DepthStencilStateDescriptor {
depth_compare: val.depth_compare.wgpu_into(),
depth_write_enabled: val.depth_write_enabled,
format: val.format.wgpu_into(),
stencil_back: (&val.stencil_back).wgpu_into(),
stencil_front: (&val.stencil_front).wgpu_into(),
stencil_read_mask: val.stencil_read_mask,
stencil_write_mask: val.stencil_write_mask,
stencil: (&val.stencil).wgpu_into(),
}
}
}
@ -440,6 +448,7 @@ impl WgpuFrom<&RasterizationStateDescriptor> for wgpu::RasterizationStateDescrip
depth_bias: val.depth_bias,
depth_bias_slope_scale: val.depth_bias_slope_scale,
depth_bias_clamp: val.depth_bias_clamp,
clamp_depth: val.clamp_depth,
}
}
}