mirror of
https://github.com/bevyengine/bevy
synced 2024-11-21 20:23:28 +00:00
new shadow pass. pipeline assignments
This commit is contained in:
parent
aa16023f41
commit
42236cfe57
7 changed files with 372 additions and 67 deletions
|
@ -183,15 +183,15 @@ fn main() {
|
||||||
// scheduler.add_system(ApplicationStage::Update, build_spawner_system(&mut world));
|
// scheduler.add_system(ApplicationStage::Update, build_spawner_system(&mut world));
|
||||||
scheduler.add_system(ApplicationStage::Update, build_print_status_system());
|
scheduler.add_system(ApplicationStage::Update, build_print_status_system());
|
||||||
|
|
||||||
world.insert((), vec![
|
// world.insert((), vec![
|
||||||
// plane
|
// // plane
|
||||||
(
|
// (
|
||||||
Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
|
// Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
|
||||||
plane_handle.clone(),
|
// plane_handle.clone(),
|
||||||
LocalToWorld::identity(),
|
// LocalToWorld::identity(),
|
||||||
Translation::new(0.0, 0.0, 0.0)
|
// Translation::new(0.0, 0.0, 0.0)
|
||||||
),
|
// ),
|
||||||
]);
|
// ]);
|
||||||
|
|
||||||
let x = *world.insert((), vec![
|
let x = *world.insert((), vec![
|
||||||
// lights
|
// lights
|
||||||
|
@ -263,7 +263,7 @@ fn main() {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let mut rng = StdRng::from_entropy();
|
let mut rng = StdRng::from_entropy();
|
||||||
for _ in 0 .. 1000 {
|
for _ in 0 .. 70000 {
|
||||||
create_person(&mut world, _cube_handle.clone(),
|
create_person(&mut world, _cube_handle.clone(),
|
||||||
Translation::new(rng.gen_range(-50.0, 50.0), 0.0, rng.gen_range(-50.0, 50.0)));
|
Translation::new(rng.gen_range(-50.0, 50.0), 0.0, rng.gen_range(-50.0, 50.0)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,8 +37,8 @@ impl Application {
|
||||||
|
|
||||||
let depth_format = wgpu::TextureFormat::Depth32Float;
|
let depth_format = wgpu::TextureFormat::Depth32Float;
|
||||||
self.render_graph.set_pass("forward", Box::new(ForwardPass::new(depth_format)));
|
self.render_graph.set_pass("forward", Box::new(ForwardPass::new(depth_format)));
|
||||||
self.render_graph.set_pipeline("forward", Box::new(ForwardPipelineNew::new()));
|
self.render_graph.set_pipeline("forward", "forward", Box::new(ForwardPipelineNew::new()));
|
||||||
self.render_graph.set_pipeline("forward_instanced", Box::new(ForwardInstancedPipeline::new(depth_format)));
|
self.render_graph.set_pipeline("forward", "forward_instanced", Box::new(ForwardInstancedPipeline::new(depth_format)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self) {
|
fn update(&mut self) {
|
||||||
|
|
|
@ -52,9 +52,9 @@ impl Pass for ForwardPass {
|
||||||
let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
||||||
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
|
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
|
||||||
}
|
}
|
||||||
fn begin<'a>(&self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, frame: &'a wgpu::SwapChainOutput) -> wgpu::RenderPass<'a> {
|
fn begin<'a>(&mut self, render_graph: &mut RenderGraphData, world: &mut World, encoder: &'a mut wgpu::CommandEncoder, frame: &'a wgpu::SwapChainOutput) -> Option<wgpu::RenderPass<'a>> {
|
||||||
let depth_texture = render_graph.get_texture(DEPTH_TEXTURE_NAME);
|
let depth_texture = render_graph.get_texture(DEPTH_TEXTURE_NAME);
|
||||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
Some(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
|
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
|
||||||
attachment: &frame.view,
|
attachment: &frame.view,
|
||||||
resolve_target: None,
|
resolve_target: None,
|
||||||
|
@ -76,12 +76,17 @@ impl Pass for ForwardPass {
|
||||||
clear_depth: 1.0,
|
clear_depth: 1.0,
|
||||||
clear_stencil: 0,
|
clear_stencil: 0,
|
||||||
}),
|
}),
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize(&self, render_graph: &mut RenderGraphData) {
|
fn resize(&self, render_graph: &mut RenderGraphData) {
|
||||||
let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
||||||
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
|
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn should_repeat(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ForwardPipelineNew {
|
impl ForwardPipelineNew {
|
||||||
|
|
|
@ -71,7 +71,7 @@ impl Pipeline for ForwardShadowPass {
|
||||||
impl ForwardShadowPass {
|
impl ForwardShadowPass {
|
||||||
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
||||||
|
|
||||||
pub fn new(device: &Device, world: &World, render_resources: &RenderResources, shadow_pass: &shadow::ShadowPass, vertex_buffer_descriptor: VertexBufferDescriptor, swap_chain_descriptor: &SwapChainDescriptor) -> Self {
|
pub fn new(device: &Device, world: &World, render_resources: &RenderResources, shadow_pass: &shadow::ShadowPassOld, vertex_buffer_descriptor: VertexBufferDescriptor, swap_chain_descriptor: &SwapChainDescriptor) -> Self {
|
||||||
let vs_bytes = shader::load_glsl(
|
let vs_bytes = shader::load_glsl(
|
||||||
include_str!("forward_shadow.vert"),
|
include_str!("forward_shadow.vert"),
|
||||||
shader::ShaderStage::Vertex,
|
shader::ShaderStage::Vertex,
|
||||||
|
|
|
@ -14,7 +14,7 @@ mod material;
|
||||||
pub use forward::{ForwardUniforms, ForwardPipelineNew, ForwardPass};
|
pub use forward::{ForwardUniforms, ForwardPipelineNew, ForwardPass};
|
||||||
pub use forward_shadow::{ForwardShadowPass};
|
pub use forward_shadow::{ForwardShadowPass};
|
||||||
pub use forward_instanced::ForwardInstancedPipeline;
|
pub use forward_instanced::ForwardInstancedPipeline;
|
||||||
pub use shadow::ShadowPass;
|
pub use shadow::ShadowPassOld;
|
||||||
pub use light::*;
|
pub use light::*;
|
||||||
pub use shader::*;
|
pub use shader::*;
|
||||||
pub use pipeline::*;
|
pub use pipeline::*;
|
||||||
|
|
|
@ -4,7 +4,8 @@ use legion::world::World;
|
||||||
|
|
||||||
pub trait Pass {
|
pub trait Pass {
|
||||||
fn initialize(&self, render_graph: &mut RenderGraphData);
|
fn initialize(&self, render_graph: &mut RenderGraphData);
|
||||||
fn begin<'a>(&self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, frame: &'a wgpu::SwapChainOutput) -> wgpu::RenderPass<'a>;
|
fn begin<'a>(&mut self, render_graph: &mut RenderGraphData, world: &mut World, encoder: &'a mut wgpu::CommandEncoder, frame: &'a wgpu::SwapChainOutput) -> Option<wgpu::RenderPass<'a>>;
|
||||||
|
fn should_repeat(&self) -> bool;
|
||||||
fn resize(&self, render_graph: &mut RenderGraphData);
|
fn resize(&self, render_graph: &mut RenderGraphData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +19,7 @@ pub struct RenderGraph {
|
||||||
pub data: RenderGraphData,
|
pub data: RenderGraphData,
|
||||||
passes: HashMap<String, Box<dyn Pass>>,
|
passes: HashMap<String, Box<dyn Pass>>,
|
||||||
pipelines: HashMap<String, Box<dyn PipelineNew>>,
|
pipelines: HashMap<String, Box<dyn PipelineNew>>,
|
||||||
|
pass_pipelines: HashMap<String, Vec<String>>,
|
||||||
render_resource_managers: Vec<Box<dyn RenderResourceManager>>,
|
render_resource_managers: Vec<Box<dyn RenderResourceManager>>,
|
||||||
pub swap_chain: wgpu::SwapChain, // TODO: this is weird
|
pub swap_chain: wgpu::SwapChain, // TODO: this is weird
|
||||||
}
|
}
|
||||||
|
@ -75,6 +77,7 @@ impl RenderGraph {
|
||||||
passes: HashMap::new(),
|
passes: HashMap::new(),
|
||||||
pipelines: HashMap::new(),
|
pipelines: HashMap::new(),
|
||||||
swap_chain,
|
swap_chain,
|
||||||
|
pass_pipelines: HashMap::new(),
|
||||||
render_resource_managers: Vec::new(),
|
render_resource_managers: Vec::new(),
|
||||||
data: RenderGraphData::new(device, swap_chain_descriptor, queue, surface),
|
data: RenderGraphData::new(device, swap_chain_descriptor, queue, surface),
|
||||||
}
|
}
|
||||||
|
@ -106,15 +109,26 @@ impl RenderGraph {
|
||||||
render_resource_manager.update(&mut self.data, &mut encoder, world);
|
render_resource_manager.update(&mut self.data, &mut encoder, world);
|
||||||
}
|
}
|
||||||
|
|
||||||
for pass in self.passes.values_mut() {
|
for (pass_name, pass) in self.passes.iter_mut() {
|
||||||
let mut render_pass = pass.begin(&mut self.data, &mut encoder, &frame);
|
loop {
|
||||||
|
let render_pass = pass.begin(&mut self.data, world, &mut encoder, &frame);
|
||||||
|
if let Some(mut render_pass) = render_pass {
|
||||||
// TODO: assign pipelines to specific passes
|
// TODO: assign pipelines to specific passes
|
||||||
for pipeline in self.pipelines.values_mut() {
|
if let Some(pipeline_names) = self.pass_pipelines.get(pass_name) {
|
||||||
|
for pipeline_name in pipeline_names.iter() {
|
||||||
|
let pipeline = self.pipelines.get_mut(pipeline_name).unwrap();
|
||||||
render_pass.set_pipeline(pipeline.get_pipeline());
|
render_pass.set_pipeline(pipeline.get_pipeline());
|
||||||
pipeline.render(&mut self.data, &mut render_pass, &frame, world);
|
pipeline.render(&mut self.data, &mut render_pass, &frame, world);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !pass.should_repeat() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let command_buffer = encoder.finish();
|
let command_buffer = encoder.finish();
|
||||||
self.data.queue.submit(&[command_buffer]);
|
self.data.queue.submit(&[command_buffer]);
|
||||||
}
|
}
|
||||||
|
@ -148,8 +162,16 @@ impl RenderGraph {
|
||||||
self.render_resource_managers.push(render_resource_manager);
|
self.render_resource_managers.push(render_resource_manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_pipeline(&mut self, name: &str, pipeline: Box<dyn PipelineNew>) {
|
pub fn set_pipeline(&mut self, pass_name: &str, pipeline_name: &str, pipeline: Box<dyn PipelineNew>) {
|
||||||
self.pipelines.insert(name.to_string(), pipeline);
|
self.pipelines.insert(pipeline_name.to_string(), pipeline);
|
||||||
|
if let None = self.pass_pipelines.get_mut(pass_name) {
|
||||||
|
let mut pipelines = Vec::new();
|
||||||
|
self.pass_pipelines.insert(pass_name.to_string(), pipelines);
|
||||||
|
};
|
||||||
|
|
||||||
|
let current_pass_pipelines = self.pass_pipelines.get_mut(pass_name).unwrap();
|
||||||
|
|
||||||
|
current_pass_pipelines.push(pipeline_name.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_pass(&mut self, name: &str, pass: Box<dyn Pass>) {
|
pub fn set_pass(&mut self, name: &str, pass: Box<dyn Pass>) {
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
use crate::{render::*, asset::*, LocalToWorld, Translation};
|
use crate::{asset::*, render::*, LocalToWorld, Translation};
|
||||||
use wgpu::{Buffer, CommandEncoder, Device, VertexBufferDescriptor, SwapChainOutput, SwapChainDescriptor};
|
|
||||||
use legion::prelude::*;
|
use legion::prelude::*;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use wgpu::{
|
||||||
|
Buffer, CommandEncoder, Device, SwapChainDescriptor, SwapChainOutput, VertexBufferDescriptor,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct ShadowPass {
|
pub struct ShadowPassOld {
|
||||||
pub pipeline: wgpu::RenderPipeline,
|
pub pipeline: wgpu::RenderPipeline,
|
||||||
pub bind_group: wgpu::BindGroup,
|
pub bind_group: wgpu::BindGroup,
|
||||||
pub uniform_buf: wgpu::Buffer,
|
pub uniform_buf: wgpu::Buffer,
|
||||||
|
@ -13,33 +15,299 @@ pub struct ShadowPass {
|
||||||
pub lights_are_dirty: bool,
|
pub lights_are_dirty: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
pub struct ShadowPass {
|
||||||
pub struct ShadowUniforms {
|
pub shadow_size: wgpu::Extent3d,
|
||||||
pub proj: [[f32; 4]; 4],
|
light_index: isize,
|
||||||
|
shadow_texture: Option<wgpu::Texture>,
|
||||||
|
shadow_format: wgpu::TextureFormat,
|
||||||
|
pub max_lights: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pipeline for ShadowPass {
|
impl ShadowPass {
|
||||||
fn render(&mut self, device: &Device, _: &SwapChainOutput, encoder: &mut CommandEncoder, world: &mut World, render_resources: &RenderResources) {
|
pub fn new(shadow_size: wgpu::Extent3d, shadow_format: wgpu::TextureFormat, max_lights: usize) -> Self {
|
||||||
let mut light_query = <(Read<Light>, Read<LocalToWorld>, Read<Translation>)>::query();
|
ShadowPass {
|
||||||
let mut mesh_query =
|
light_index: -1,
|
||||||
<(Read<Material>, Read<Handle<Mesh>>)>::query()
|
shadow_texture: None,
|
||||||
.filter(!component::<Instanced>());
|
shadow_size,
|
||||||
|
shadow_format,
|
||||||
|
max_lights,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pass for ShadowPass {
|
||||||
|
fn initialize(&self, render_graph: &mut RenderGraphData) {
|
||||||
|
let shadow_texture = render_graph.device.create_texture(&wgpu::TextureDescriptor {
|
||||||
|
size: self.shadow_size,
|
||||||
|
array_layer_count: self.max_lights as u32,
|
||||||
|
mip_level_count: 1,
|
||||||
|
sample_count: 1,
|
||||||
|
dimension: wgpu::TextureDimension::D2,
|
||||||
|
format: self.shadow_format,
|
||||||
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::SAMPLED,
|
||||||
|
});
|
||||||
|
|
||||||
|
let shadow_view = shadow_texture.create_default_view();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn begin<'a>(
|
||||||
|
&mut self,
|
||||||
|
render_graph: &mut RenderGraphData,
|
||||||
|
world: &mut World,
|
||||||
|
encoder: &'a mut wgpu::CommandEncoder,
|
||||||
|
frame: &'a wgpu::SwapChainOutput,
|
||||||
|
) -> Option<wgpu::RenderPass<'a>> {
|
||||||
|
if self.light_index == -1 {
|
||||||
|
self.light_index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut light_query = <(Write<Light>, Read<LocalToWorld>, Read<Translation>)>::query();
|
||||||
|
let light_count = light_query.iter(world).count();
|
||||||
|
for (i, (mut light, _, _)) in light_query.iter(world).enumerate() {
|
||||||
|
if i != self.light_index as usize {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for (i, (mut light, _)) in <(Write<Light>, Read<LocalToWorld>)>::query().iter(world).enumerate() {
|
|
||||||
if let None = light.target_view {
|
if let None = light.target_view {
|
||||||
light.target_view = Some(self.shadow_texture.create_view(&wgpu::TextureViewDescriptor {
|
light.target_view = Some(self.shadow_texture.as_ref().unwrap().create_view(
|
||||||
format: ShadowPass::SHADOW_FORMAT,
|
&wgpu::TextureViewDescriptor {
|
||||||
|
format: ShadowPassOld::SHADOW_FORMAT,
|
||||||
dimension: wgpu::TextureViewDimension::D2,
|
dimension: wgpu::TextureViewDimension::D2,
|
||||||
aspect: wgpu::TextureAspect::All,
|
aspect: wgpu::TextureAspect::All,
|
||||||
base_mip_level: 0,
|
base_mip_level: 0,
|
||||||
level_count: 1,
|
level_count: 1,
|
||||||
base_array_layer: i as u32,
|
base_array_layer: i as u32,
|
||||||
array_layer_count: 1,
|
array_layer_count: 1,
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// The light uniform buffer already has the projection,
|
||||||
|
// let's just copy it over to the shadow uniform buffer.
|
||||||
|
let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap();
|
||||||
|
let shadow_pipeline_uniform_buffer = render_graph.get_uniform_buffer(SHADOW_PIPELINE_UNIFORMS).unwrap();
|
||||||
|
encoder.copy_buffer_to_buffer(
|
||||||
|
&light_uniform_buffer.buffer,
|
||||||
|
(i * mem::size_of::<LightRaw>()) as wgpu::BufferAddress,
|
||||||
|
&shadow_pipeline_uniform_buffer.buffer,
|
||||||
|
0,
|
||||||
|
64,
|
||||||
|
);
|
||||||
|
|
||||||
|
self.light_index += 1;
|
||||||
|
if self.light_index as usize == light_count {
|
||||||
|
self.light_index = -1;
|
||||||
|
}
|
||||||
|
return Some(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
|
color_attachments: &[],
|
||||||
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachmentDescriptor {
|
||||||
|
attachment: light.target_view.as_ref().unwrap(),
|
||||||
|
depth_load_op: wgpu::LoadOp::Clear,
|
||||||
|
depth_store_op: wgpu::StoreOp::Store,
|
||||||
|
stencil_load_op: wgpu::LoadOp::Clear,
|
||||||
|
stencil_store_op: wgpu::StoreOp::Store,
|
||||||
|
clear_depth: 1.0,
|
||||||
|
clear_stencil: 0,
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resize(&self, render_graph: &mut RenderGraphData) {}
|
||||||
|
|
||||||
|
fn should_repeat(&self) -> bool {
|
||||||
|
return self.light_index != -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct ShadowPipeline {
|
||||||
|
pub pipeline: Option<wgpu::RenderPipeline>,
|
||||||
|
pub bind_group: Option<wgpu::BindGroup>,
|
||||||
|
pub shadow_format: wgpu::TextureFormat,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const SHADOW_PIPELINE_UNIFORMS: &str = "shadow_pipeline";
|
||||||
|
|
||||||
|
impl ShadowPipeline {
|
||||||
|
pub fn new(shadow_format: wgpu::TextureFormat, shadow_size: wgpu::Extent3d, max_lights: usize) -> Self {
|
||||||
|
ShadowPipeline {
|
||||||
|
bind_group: None,
|
||||||
|
pipeline: None,
|
||||||
|
shadow_format,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PipelineNew for ShadowPipeline {
|
||||||
|
fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World) {
|
||||||
|
let bind_group_layout = render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||||
|
binding: 0, // global
|
||||||
|
visibility: wgpu::ShaderStage::VERTEX,
|
||||||
|
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: stop using "local"
|
||||||
|
let local_bind_group_layout = render_graph.get_bind_group_layout("local").unwrap();
|
||||||
|
|
||||||
|
let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
|
bind_group_layouts: &[
|
||||||
|
&bind_group_layout,
|
||||||
|
local_bind_group_layout,
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
let uniform_size = mem::size_of::<ShadowUniforms>() as wgpu::BufferAddress;
|
||||||
|
let uniform_buf = render_graph.device.create_buffer(&wgpu::BufferDescriptor {
|
||||||
|
size: uniform_size,
|
||||||
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create bind group
|
||||||
|
self.bind_group = Some(render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
|
layout: &bind_group_layout,
|
||||||
|
bindings: &[wgpu::Binding {
|
||||||
|
binding: 0,
|
||||||
|
resource: wgpu::BindingResource::Buffer {
|
||||||
|
buffer: &uniform_buf,
|
||||||
|
range: 0..uniform_size,
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
}));
|
||||||
|
|
||||||
|
render_graph.set_uniform_buffer(SHADOW_PIPELINE_UNIFORMS, UniformBuffer {
|
||||||
|
buffer: uniform_buf,
|
||||||
|
size: uniform_size,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create other resources
|
||||||
|
let shadow_sampler = render_graph.device.create_sampler(&wgpu::SamplerDescriptor {
|
||||||
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
||||||
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
||||||
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
||||||
|
mag_filter: wgpu::FilterMode::Linear,
|
||||||
|
min_filter: wgpu::FilterMode::Linear,
|
||||||
|
mipmap_filter: wgpu::FilterMode::Nearest,
|
||||||
|
lod_min_clamp: -100.0,
|
||||||
|
lod_max_clamp: 100.0,
|
||||||
|
compare_function: wgpu::CompareFunction::LessEqual,
|
||||||
|
});
|
||||||
|
|
||||||
|
let vertex_buffer_descriptor = get_vertex_buffer_descriptor();
|
||||||
|
|
||||||
|
// Create the render pipeline
|
||||||
|
let vs_bytes = shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex);
|
||||||
|
let fs_bytes =
|
||||||
|
shader::load_glsl(include_str!("shadow.frag"), shader::ShaderStage::Fragment);
|
||||||
|
let vs_module = render_graph.device.create_shader_module(&vs_bytes);
|
||||||
|
let fs_module = render_graph.device.create_shader_module(&fs_bytes);
|
||||||
|
|
||||||
|
self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||||
|
layout: &pipeline_layout,
|
||||||
|
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||||
|
module: &vs_module,
|
||||||
|
entry_point: "main",
|
||||||
|
},
|
||||||
|
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
||||||
|
module: &fs_module,
|
||||||
|
entry_point: "main",
|
||||||
|
}),
|
||||||
|
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
|
||||||
|
front_face: wgpu::FrontFace::Ccw,
|
||||||
|
cull_mode: wgpu::CullMode::Back,
|
||||||
|
depth_bias: 2, // corresponds to bilinear filtering
|
||||||
|
depth_bias_slope_scale: 2.0,
|
||||||
|
depth_bias_clamp: 0.0,
|
||||||
|
}),
|
||||||
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||||
|
color_states: &[],
|
||||||
|
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
||||||
|
format: self.shadow_format,
|
||||||
|
depth_write_enabled: true,
|
||||||
|
depth_compare: wgpu::CompareFunction::LessEqual,
|
||||||
|
stencil_front: wgpu::StencilStateFaceDescriptor::IGNORE,
|
||||||
|
stencil_back: wgpu::StencilStateFaceDescriptor::IGNORE,
|
||||||
|
stencil_read_mask: 0,
|
||||||
|
stencil_write_mask: 0,
|
||||||
|
}),
|
||||||
|
index_format: wgpu::IndexFormat::Uint16,
|
||||||
|
vertex_buffers: &[vertex_buffer_descriptor],
|
||||||
|
sample_count: 1,
|
||||||
|
sample_mask: !0,
|
||||||
|
alpha_to_coverage_enabled: false,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(
|
||||||
|
&mut self,
|
||||||
|
render_graph: &RenderGraphData,
|
||||||
|
pass: &mut wgpu::RenderPass,
|
||||||
|
frame: &SwapChainOutput,
|
||||||
|
world: &mut World,
|
||||||
|
) {
|
||||||
|
let mut mesh_query =
|
||||||
|
<(Read<Material>, Read<Handle<Mesh>>)>::query().filter(!component::<Instanced>());
|
||||||
|
pass.set_bind_group(0, self.bind_group.as_ref().unwrap(), &[]);
|
||||||
|
|
||||||
|
let mut mesh_storage = world
|
||||||
|
.resources
|
||||||
|
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||||
|
.unwrap();
|
||||||
|
for (entity, mesh) in mesh_query.iter_immutable(world) {
|
||||||
|
if let Some(mesh_asset) = mesh_storage.get(*mesh.id.read().unwrap()) {
|
||||||
|
mesh_asset.setup_buffers(&render_graph.device);
|
||||||
|
|
||||||
|
pass.set_bind_group(1, entity.bind_group.as_ref().unwrap(), &[]);
|
||||||
|
pass.set_index_buffer(&mesh_asset.index_buffer.as_ref().unwrap(), 0);
|
||||||
|
pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]);
|
||||||
|
pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resize(&mut self, render_graph: &RenderGraphData) {}
|
||||||
|
|
||||||
|
fn get_pipeline(&self) -> &wgpu::RenderPipeline {
|
||||||
|
self.pipeline.as_ref().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct ShadowUniforms {
|
||||||
|
pub proj: [[f32; 4]; 4],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pipeline for ShadowPassOld {
|
||||||
|
fn render(
|
||||||
|
&mut self,
|
||||||
|
device: &Device,
|
||||||
|
_: &SwapChainOutput,
|
||||||
|
encoder: &mut CommandEncoder,
|
||||||
|
world: &mut World,
|
||||||
|
render_resources: &RenderResources,
|
||||||
|
) {
|
||||||
|
let mut light_query = <(Read<Light>, Read<LocalToWorld>, Read<Translation>)>::query();
|
||||||
|
let mut mesh_query =
|
||||||
|
<(Read<Material>, Read<Handle<Mesh>>)>::query().filter(!component::<Instanced>());
|
||||||
|
|
||||||
for (i, (light, _, _)) in light_query.iter_immutable(world).enumerate() {
|
for (i, (light, _, _)) in light_query.iter_immutable(world).enumerate() {
|
||||||
|
// if let None = light.target_view {
|
||||||
|
// light.target_view = Some(self.shadow_texture.create_view(
|
||||||
|
// &wgpu::TextureViewDescriptor {
|
||||||
|
// format: ShadowPassOld::SHADOW_FORMAT,
|
||||||
|
// dimension: wgpu::TextureViewDimension::D2,
|
||||||
|
// aspect: wgpu::TextureAspect::All,
|
||||||
|
// base_mip_level: 0,
|
||||||
|
// level_count: 1,
|
||||||
|
// base_array_layer: i as u32,
|
||||||
|
// array_layer_count: 1,
|
||||||
|
// },
|
||||||
|
// ));
|
||||||
|
// }
|
||||||
|
|
||||||
// The light uniform buffer already has the projection,
|
// The light uniform buffer already has the projection,
|
||||||
// let's just copy it over to the shadow uniform buffer.
|
// let's just copy it over to the shadow uniform buffer.
|
||||||
encoder.copy_buffer_to_buffer(
|
encoder.copy_buffer_to_buffer(
|
||||||
|
@ -65,7 +333,10 @@ impl Pipeline for ShadowPass {
|
||||||
pass.set_pipeline(&self.pipeline);
|
pass.set_pipeline(&self.pipeline);
|
||||||
pass.set_bind_group(0, &self.bind_group, &[]);
|
pass.set_bind_group(0, &self.bind_group, &[]);
|
||||||
|
|
||||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
let mut mesh_storage = world
|
||||||
|
.resources
|
||||||
|
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||||
|
.unwrap();
|
||||||
for (entity, mesh) in mesh_query.iter_immutable(world) {
|
for (entity, mesh) in mesh_query.iter_immutable(world) {
|
||||||
if let Some(mesh_asset) = mesh_storage.get(*mesh.id.read().unwrap()) {
|
if let Some(mesh_asset) = mesh_storage.get(*mesh.id.read().unwrap()) {
|
||||||
mesh_asset.setup_buffers(device);
|
mesh_asset.setup_buffers(device);
|
||||||
|
@ -74,17 +345,18 @@ impl Pipeline for ShadowPass {
|
||||||
pass.set_index_buffer(&mesh_asset.index_buffer.as_ref().unwrap(), 0);
|
pass.set_index_buffer(&mesh_asset.index_buffer.as_ref().unwrap(), 0);
|
||||||
pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]);
|
pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]);
|
||||||
pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1);
|
pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1);
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resize(&mut self, _: &Device, _: &SwapChainDescriptor) {}
|
fn resize(&mut self, _: &Device, _: &SwapChainDescriptor) {}
|
||||||
fn get_camera_uniform_buffer(&self) -> Option<&Buffer> { None }
|
fn get_camera_uniform_buffer(&self) -> Option<&Buffer> {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ShadowPass {
|
impl ShadowPassOld {
|
||||||
pub const SHADOW_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
pub const SHADOW_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
||||||
pub const SHADOW_SIZE: wgpu::Extent3d = wgpu::Extent3d {
|
pub const SHADOW_SIZE: wgpu::Extent3d = wgpu::Extent3d {
|
||||||
width: 512,
|
width: 512,
|
||||||
|
@ -92,10 +364,14 @@ impl ShadowPass {
|
||||||
depth: 1,
|
depth: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn new(device: &Device, _: &World, render_resources: &RenderResources, vertex_buffer_descriptor: VertexBufferDescriptor) -> ShadowPass {
|
pub fn new(
|
||||||
|
device: &Device,
|
||||||
|
_: &World,
|
||||||
|
render_resources: &RenderResources,
|
||||||
|
vertex_buffer_descriptor: VertexBufferDescriptor,
|
||||||
|
) -> ShadowPassOld {
|
||||||
// Create pipeline layout
|
// Create pipeline layout
|
||||||
let bind_group_layout =
|
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
||||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||||
binding: 0, // global
|
binding: 0, // global
|
||||||
visibility: wgpu::ShaderStage::VERTEX,
|
visibility: wgpu::ShaderStage::VERTEX,
|
||||||
|
@ -103,7 +379,10 @@ impl ShadowPass {
|
||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
bind_group_layouts: &[&bind_group_layout, &render_resources.local_bind_group_layout],
|
bind_group_layouts: &[
|
||||||
|
&bind_group_layout,
|
||||||
|
&render_resources.local_bind_group_layout,
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
let uniform_size = mem::size_of::<ShadowUniforms>() as wgpu::BufferAddress;
|
let uniform_size = mem::size_of::<ShadowUniforms>() as wgpu::BufferAddress;
|
||||||
|
@ -150,8 +429,7 @@ impl ShadowPass {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create the render pipeline
|
// Create the render pipeline
|
||||||
let vs_bytes =
|
let vs_bytes = shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex);
|
||||||
shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex);
|
|
||||||
let fs_bytes =
|
let fs_bytes =
|
||||||
shader::load_glsl(include_str!("shadow.frag"), shader::ShaderStage::Fragment);
|
shader::load_glsl(include_str!("shadow.frag"), shader::ShaderStage::Fragment);
|
||||||
let vs_module = device.create_shader_module(&vs_bytes);
|
let vs_module = device.create_shader_module(&vs_bytes);
|
||||||
|
@ -192,7 +470,7 @@ impl ShadowPass {
|
||||||
alpha_to_coverage_enabled: false,
|
alpha_to_coverage_enabled: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
ShadowPass {
|
ShadowPassOld {
|
||||||
pipeline,
|
pipeline,
|
||||||
bind_group,
|
bind_group,
|
||||||
uniform_buf,
|
uniform_buf,
|
||||||
|
|
Loading…
Reference in a new issue