Skip to content

Commit

Permalink
Update to wgpu-rs 0.7 (#542)
Browse files Browse the repository at this point in the history
Update to wgpu-rs 0.7
  • Loading branch information
Neo-Zhixing committed Feb 1, 2021
1 parent e6e23fd commit 81809c7
Show file tree
Hide file tree
Showing 29 changed files with 574 additions and 370 deletions.
33 changes: 15 additions & 18 deletions crates/bevy_pbr/src/render_graph/forward_pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use bevy_asset::{Assets, HandleUntyped};
use bevy_reflect::TypeUuid;
use bevy_render::{
pipeline::{
BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite,
CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, PipelineDescriptor,
RasterizationStateDescriptor, StencilStateDescriptor, StencilStateFaceDescriptor,
BlendFactor, BlendOperation, BlendState, ColorTargetState, ColorWrite, CompareFunction,
DepthBiasState, DepthStencilState, PipelineDescriptor, StencilFaceState, StencilState,
},
shader::{Shader, ShaderStage, ShaderStages},
texture::TextureFormat,
Expand All @@ -15,33 +14,31 @@ pub const FORWARD_PIPELINE_HANDLE: HandleUntyped =

pub(crate) fn build_forward_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
PipelineDescriptor {
rasterization_state: Some(RasterizationStateDescriptor {
front_face: FrontFace::Ccw,
cull_mode: CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
depth_stencil: Some(DepthStencilState {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
stencil: StencilState {
front: StencilFaceState::IGNORE,
back: StencilFaceState::IGNORE,
read_mask: 0,
write_mask: 0,
},
bias: DepthBiasState {
constant: 0,
slope_scale: 0.0,
clamp: 0.0,
},
clamp_depth: false,
}),
color_states: vec![ColorStateDescriptor {
color_target_states: vec![ColorTargetState {
format: TextureFormat::default(),
color_blend: BlendDescriptor {
color_blend: BlendState {
src_factor: BlendFactor::SrcAlpha,
dst_factor: BlendFactor::OneMinusSrcAlpha,
operation: BlendOperation::Add,
},
alpha_blend: BlendDescriptor {
alpha_blend: BlendState {
src_factor: BlendFactor::One,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
Expand Down
17 changes: 12 additions & 5 deletions crates/bevy_render/src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
pipeline::{PipelineCompiler, PipelineDescriptor, PipelineLayout, PipelineSpecialization},
pipeline::{
IndexFormat, PipelineCompiler, PipelineDescriptor, PipelineLayout, PipelineSpecialization,
},
renderer::{
AssetRenderResourceBindings, BindGroup, BindGroupId, BufferId, RenderResource,
RenderResourceBinding, RenderResourceBindings, RenderResourceContext, SharedBuffers,
Expand All @@ -26,6 +28,7 @@ pub enum RenderCommand {
SetIndexBuffer {
buffer: BufferId,
offset: u64,
index_format: IndexFormat,
},
SetBindGroup {
index: u32,
Expand Down Expand Up @@ -95,8 +98,12 @@ impl Draw {
});
}

pub fn set_index_buffer(&mut self, buffer: BufferId, offset: u64) {
self.render_command(RenderCommand::SetIndexBuffer { buffer, offset });
pub fn set_index_buffer(&mut self, buffer: BufferId, offset: u64, index_format: IndexFormat) {
self.render_command(RenderCommand::SetIndexBuffer {
buffer,
offset,
index_format,
});
}

pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup) {
Expand Down Expand Up @@ -325,8 +332,8 @@ impl<'a> DrawContext<'a> {
render_resource_bindings: &[&RenderResourceBindings],
) -> Result<(), DrawError> {
for bindings in render_resource_bindings.iter() {
if let Some(index_buffer) = bindings.index_buffer {
draw.set_index_buffer(index_buffer, 0);
if let Some((index_buffer, index_format)) = bindings.index_buffer {
draw.set_index_buffer(index_buffer, 0, index_format);
}
if let Some(main_vertex_buffer) = bindings.vertex_attribute_buffer {
draw.set_vertex_buffer(0, main_vertex_buffer, 0);
Expand Down
25 changes: 13 additions & 12 deletions crates/bevy_render/src/mesh/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_math::*;
use bevy_reflect::TypeUuid;
use std::borrow::Cow;

use crate::pipeline::{InputStepMode, VertexAttributeDescriptor, VertexBufferDescriptor};
use crate::pipeline::{InputStepMode, VertexAttribute, VertexBufferLayout};
use bevy_utils::{HashMap, HashSet};

pub const INDEX_BUFFER_ASSET_INDEX: u64 = 0;
Expand Down Expand Up @@ -256,12 +256,12 @@ impl Mesh {
})
}

pub fn get_vertex_buffer_descriptor(&self) -> VertexBufferDescriptor {
pub fn get_vertex_buffer_layout(&self) -> VertexBufferLayout {
let mut attributes = Vec::new();
let mut accumulated_offset = 0;
for (attribute_name, attribute_values) in self.attributes.iter() {
let vertex_format = VertexFormat::from(attribute_values);
attributes.push(VertexAttributeDescriptor {
attributes.push(VertexAttribute {
name: attribute_name.clone(),
offset: accumulated_offset,
format: vertex_format,
Expand All @@ -270,7 +270,7 @@ impl Mesh {
accumulated_offset += vertex_format.get_size();
}

VertexBufferDescriptor {
VertexBufferLayout {
name: Default::default(),
stride: accumulated_offset,
step_mode: InputStepMode::Vertex,
Expand Down Expand Up @@ -453,21 +453,22 @@ fn update_entity_mesh(
for render_pipeline in render_pipelines.pipelines.iter_mut() {
render_pipeline.specialization.primitive_topology = mesh.primitive_topology;
// TODO: don't allocate a new vertex buffer descriptor for every entity
render_pipeline.specialization.vertex_buffer_descriptor =
mesh.get_vertex_buffer_descriptor();
render_pipeline.specialization.index_format = mesh
.indices()
.map(|i| i.into())
.unwrap_or(IndexFormat::Uint32);
render_pipeline.specialization.vertex_buffer_layout = mesh.get_vertex_buffer_layout();
if let PrimitiveTopology::LineStrip | PrimitiveTopology::TriangleStrip =
mesh.primitive_topology
{
render_pipeline.specialization.strip_index_format =
mesh.indices().map(|indices| indices.into());
}
}

if let Some(RenderResourceId::Buffer(index_buffer_resource)) =
render_resource_context.get_asset_resource(handle, INDEX_BUFFER_ASSET_INDEX)
{
let index_format: IndexFormat = mesh.indices().unwrap().into();
// set index buffer into binding
render_pipelines
.bindings
.set_index_buffer(index_buffer_resource);
.set_index_buffer(index_buffer_resource, index_format);
}

if let Some(RenderResourceId::Buffer(vertex_attribute_buffer_resource)) =
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/pass/render_pass.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
pipeline::{BindGroupDescriptorId, PipelineDescriptor},
pipeline::{BindGroupDescriptorId, IndexFormat, PipelineDescriptor},
renderer::{BindGroupId, BufferId, RenderContext},
};
use bevy_asset::Handle;
use std::ops::Range;

pub trait RenderPass {
fn get_render_context(&self) -> &dyn RenderContext;
fn set_index_buffer(&mut self, buffer: BufferId, offset: u64);
fn set_index_buffer(&mut self, buffer: BufferId, offset: u64, index_format: IndexFormat);
fn set_vertex_buffer(&mut self, start_slot: u32, buffer: BufferId, offset: u64);
fn set_pipeline(&mut self, pipeline_handle: &Handle<PipelineDescriptor>);
fn set_viewport(&mut self, x: f32, y: f32, w: f32, h: f32, min_depth: f32, max_depth: f32);
Expand Down
28 changes: 20 additions & 8 deletions crates/bevy_render/src/pipeline/binding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::UniformProperty;
use crate::texture::{TextureComponentType, TextureFormat, TextureViewDimension};
use crate::texture::{
StorageTextureAccess, TextureFormat, TextureSampleType, TextureViewDimension,
};

bitflags::bitflags! {
pub struct BindingShaderStage: u32 {
Expand All @@ -20,25 +22,35 @@ pub struct BindingDescriptor {
#[derive(Hash, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum BindType {
Uniform {
dynamic: bool,
has_dynamic_offset: bool,
property: UniformProperty,
},
StorageBuffer {
dynamic: bool,
has_dynamic_offset: bool,
readonly: bool,
},
Sampler {
/// The sampling result is produced based on more than a single color sample from a texture,
/// e.g. when bilinear interpolation is enabled.
///
/// A filtering sampler can only be used with a filterable texture.
filtering: bool,
/// Use as a comparison sampler instead of a normal sampler.
/// For more info take a look at the analogous functionality in OpenGL: https://www.khronos.org/opengl/wiki/Sampler_Object#Comparison_mode.
comparison: bool,
},
SampledTexture {
Texture {
multisampled: bool,
dimension: TextureViewDimension,
component_type: TextureComponentType,
view_dimension: TextureViewDimension,
sample_type: TextureSampleType,
},
StorageTexture {
dimension: TextureViewDimension,
/// Allowed access to this texture.
access: StorageTextureAccess,
/// Format of the texture.
format: TextureFormat,
readonly: bool,
/// Dimension of the texture view that is going to be sampled.
view_dimension: TextureViewDimension,
},
}

Expand Down
109 changes: 53 additions & 56 deletions crates/bevy_render/src/pipeline/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use super::{
state_descriptors::{
BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite,
CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, IndexFormat,
PrimitiveTopology, RasterizationStateDescriptor, StencilStateFaceDescriptor,
BlendFactor, BlendOperation, ColorWrite, CompareFunction, CullMode, FrontFace,
PrimitiveTopology,
},
PipelineLayout, StencilStateDescriptor,
PipelineLayout,
};
use crate::{
pipeline::{
BlendState, ColorTargetState, DepthBiasState, DepthStencilState, MultisampleState,
PolygonMode, PrimitiveState, StencilFaceState, StencilState,
},
shader::ShaderStages,
texture::TextureFormat,
};
use crate::{shader::ShaderStages, texture::TextureFormat};
use bevy_reflect::TypeUuid;

#[derive(Clone, Debug, TypeUuid)]
Expand All @@ -15,93 +21,84 @@ pub struct PipelineDescriptor {
pub name: Option<String>,
pub layout: Option<PipelineLayout>,
pub shader_stages: ShaderStages,
pub rasterization_state: Option<RasterizationStateDescriptor>,

/// The primitive topology used to interpret vertices.
pub primitive_topology: PrimitiveTopology,
pub primitive: PrimitiveState,
pub depth_stencil: Option<DepthStencilState>,
pub multisample: MultisampleState,

/// The effect of draw calls on the color aspect of the output target.
pub color_states: Vec<ColorStateDescriptor>,

/// The effect of draw calls on the depth and stencil aspects of the output target, if any.
pub depth_stencil_state: Option<DepthStencilStateDescriptor>,

/// The format of any index buffers used with this pipeline.
pub index_format: IndexFormat,

/// The number of samples calculated per pixel (for MSAA).
pub sample_count: u32,

/// Bitmask that restricts the samples of a pixel modified by this pipeline.
pub sample_mask: u32,

/// When enabled, produces another sample mask per pixel based on the alpha output value, that
/// is AND-ed with the sample_mask and the primitive coverage to restrict the set of samples
/// affected by a primitive.
/// The implicit mask produced for alpha of zero is guaranteed to be zero, and for alpha of one
/// is guaranteed to be all 1-s.
pub alpha_to_coverage_enabled: bool,
pub color_target_states: Vec<ColorTargetState>,
}

impl PipelineDescriptor {
pub fn new(shader_stages: ShaderStages) -> Self {
PipelineDescriptor {
name: None,
layout: None,
color_states: Vec::new(),
depth_stencil_state: None,
color_target_states: Vec::new(),
depth_stencil: None,
shader_stages,
rasterization_state: None,
primitive_topology: PrimitiveTopology::TriangleList,
index_format: IndexFormat::Uint32,
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
primitive: PrimitiveState {
topology: PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: FrontFace::Ccw,
cull_mode: CullMode::Back,
polygon_mode: PolygonMode::Fill,
},
multisample: MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
}
}

pub fn default_config(shader_stages: ShaderStages) -> Self {
PipelineDescriptor {
name: None,
primitive_topology: PrimitiveTopology::TriangleList,
layout: None,
index_format: IndexFormat::Uint32,
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
rasterization_state: Some(RasterizationStateDescriptor {
primitive: PrimitiveState {
topology: PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: FrontFace::Ccw,
cull_mode: CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
polygon_mode: PolygonMode::Fill,
},
layout: None,
depth_stencil: Some(DepthStencilState {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
stencil: StencilState {
front: StencilFaceState::IGNORE,
back: StencilFaceState::IGNORE,
read_mask: 0,
write_mask: 0,
},
bias: DepthBiasState {
constant: 0,
slope_scale: 0.0,
clamp: 0.0,
},
clamp_depth: false,
}),
color_states: vec![ColorStateDescriptor {
color_target_states: vec![ColorTargetState {
format: TextureFormat::default(),
color_blend: BlendDescriptor {
color_blend: BlendState {
src_factor: BlendFactor::SrcAlpha,
dst_factor: BlendFactor::OneMinusSrcAlpha,
operation: BlendOperation::Add,
},
alpha_blend: BlendDescriptor {
alpha_blend: BlendState {
src_factor: BlendFactor::One,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
},
write_mask: ColorWrite::ALL,
}],
multisample: MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
shader_stages,
}
}
Expand Down
Loading

0 comments on commit 81809c7

Please sign in to comment.