Skip to content

Commit

Permalink
upgrade to latest wgpu
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Aug 22, 2020
1 parent 7a79dcc commit 7c3b49c
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 57 deletions.
13 changes: 8 additions & 5 deletions crates/bevy_pbr/src/render_graph/forward_pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
13 changes: 8 additions & 5 deletions crates/bevy_render/src/pipeline/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 11 additions & 4 deletions crates/bevy_render/src/pipeline/state_descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_render/src/renderer/render_resource/bind_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>>,
}

Expand All @@ -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,
}
Expand All @@ -47,7 +47,7 @@ impl BindGroupBuilder {

binding.hash(&mut self.hasher);
self.indexed_bindings
.push(IndexedBinding { index, binding });
.push(IndexedBindGroupEntry { index, entry: binding });
self
}

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/texture/sampler_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Texture;
use crate::pipeline::CompareFunction;
use std::num::NonZeroU8;

/// Describes a sampler
#[derive(Copy, Clone)]
Expand All @@ -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 {
Expand Down
24 changes: 15 additions & 9 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
46 changes: 28 additions & 18 deletions crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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| {
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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);

Expand Down
Loading

0 comments on commit 7c3b49c

Please sign in to comment.