mirror of
https://github.com/bevyengine/bevy
synced 2024-11-21 12:13:25 +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_print_status_system());
|
||||
|
||||
world.insert((), vec![
|
||||
// plane
|
||||
(
|
||||
Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
|
||||
plane_handle.clone(),
|
||||
LocalToWorld::identity(),
|
||||
Translation::new(0.0, 0.0, 0.0)
|
||||
),
|
||||
]);
|
||||
// world.insert((), vec![
|
||||
// // plane
|
||||
// (
|
||||
// Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
|
||||
// plane_handle.clone(),
|
||||
// LocalToWorld::identity(),
|
||||
// Translation::new(0.0, 0.0, 0.0)
|
||||
// ),
|
||||
// ]);
|
||||
|
||||
let x = *world.insert((), vec![
|
||||
// lights
|
||||
|
@ -263,7 +263,7 @@ fn main() {
|
|||
]);
|
||||
|
||||
let mut rng = StdRng::from_entropy();
|
||||
for _ in 0 .. 1000 {
|
||||
for _ in 0 .. 70000 {
|
||||
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)));
|
||||
}
|
||||
|
|
|
@ -37,8 +37,8 @@ impl Application {
|
|||
|
||||
let depth_format = wgpu::TextureFormat::Depth32Float;
|
||||
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_instanced", Box::new(ForwardInstancedPipeline::new(depth_format)));
|
||||
self.render_graph.set_pipeline("forward", "forward", Box::new(ForwardPipelineNew::new()));
|
||||
self.render_graph.set_pipeline("forward", "forward_instanced", Box::new(ForwardInstancedPipeline::new(depth_format)));
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
Some(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
|
||||
attachment: &frame.view,
|
||||
resolve_target: None,
|
||||
|
@ -76,12 +76,17 @@ impl Pass for ForwardPass {
|
|||
clear_depth: 1.0,
|
||||
clear_stencil: 0,
|
||||
}),
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn resize(&self, render_graph: &mut RenderGraphData) {
|
||||
let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
||||
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
|
||||
}
|
||||
|
||||
fn should_repeat(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl ForwardPipelineNew {
|
||||
|
|
|
@ -71,7 +71,7 @@ impl Pipeline for ForwardShadowPass {
|
|||
impl ForwardShadowPass {
|
||||
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(
|
||||
include_str!("forward_shadow.vert"),
|
||||
shader::ShaderStage::Vertex,
|
||||
|
|
|
@ -14,7 +14,7 @@ mod material;
|
|||
pub use forward::{ForwardUniforms, ForwardPipelineNew, ForwardPass};
|
||||
pub use forward_shadow::{ForwardShadowPass};
|
||||
pub use forward_instanced::ForwardInstancedPipeline;
|
||||
pub use shadow::ShadowPass;
|
||||
pub use shadow::ShadowPassOld;
|
||||
pub use light::*;
|
||||
pub use shader::*;
|
||||
pub use pipeline::*;
|
||||
|
|
|
@ -4,7 +4,8 @@ use legion::world::World;
|
|||
|
||||
pub trait Pass {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -18,6 +19,7 @@ pub struct RenderGraph {
|
|||
pub data: RenderGraphData,
|
||||
passes: HashMap<String, Box<dyn Pass>>,
|
||||
pipelines: HashMap<String, Box<dyn PipelineNew>>,
|
||||
pass_pipelines: HashMap<String, Vec<String>>,
|
||||
render_resource_managers: Vec<Box<dyn RenderResourceManager>>,
|
||||
pub swap_chain: wgpu::SwapChain, // TODO: this is weird
|
||||
}
|
||||
|
@ -75,6 +77,7 @@ impl RenderGraph {
|
|||
passes: HashMap::new(),
|
||||
pipelines: HashMap::new(),
|
||||
swap_chain,
|
||||
pass_pipelines: HashMap::new(),
|
||||
render_resource_managers: Vec::new(),
|
||||
data: RenderGraphData::new(device, swap_chain_descriptor, queue, surface),
|
||||
}
|
||||
|
@ -106,12 +109,23 @@ impl RenderGraph {
|
|||
render_resource_manager.update(&mut self.data, &mut encoder, world);
|
||||
}
|
||||
|
||||
for pass in self.passes.values_mut() {
|
||||
let mut render_pass = pass.begin(&mut self.data, &mut encoder, &frame);
|
||||
// TODO: assign pipelines to specific passes
|
||||
for pipeline in self.pipelines.values_mut() {
|
||||
render_pass.set_pipeline(pipeline.get_pipeline());
|
||||
pipeline.render(&mut self.data, &mut render_pass, &frame, world);
|
||||
for (pass_name, pass) in self.passes.iter_mut() {
|
||||
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
|
||||
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());
|
||||
pipeline.render(&mut self.data, &mut render_pass, &frame, world);
|
||||
}
|
||||
}
|
||||
|
||||
if !pass.should_repeat() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,8 +162,16 @@ impl RenderGraph {
|
|||
self.render_resource_managers.push(render_resource_manager);
|
||||
}
|
||||
|
||||
pub fn set_pipeline(&mut self, name: &str, pipeline: Box<dyn PipelineNew>) {
|
||||
self.pipelines.insert(name.to_string(), pipeline);
|
||||
pub fn set_pipeline(&mut self, pass_name: &str, pipeline_name: &str, pipeline: Box<dyn PipelineNew>) {
|
||||
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>) {
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
use crate::{render::*, asset::*, LocalToWorld, Translation};
|
||||
use wgpu::{Buffer, CommandEncoder, Device, VertexBufferDescriptor, SwapChainOutput, SwapChainDescriptor};
|
||||
use crate::{asset::*, render::*, LocalToWorld, Translation};
|
||||
use legion::prelude::*;
|
||||
use std::mem;
|
||||
use wgpu::{
|
||||
Buffer, CommandEncoder, Device, SwapChainDescriptor, SwapChainOutput, VertexBufferDescriptor,
|
||||
};
|
||||
|
||||
pub struct ShadowPass {
|
||||
pub struct ShadowPassOld {
|
||||
pub pipeline: wgpu::RenderPipeline,
|
||||
pub bind_group: wgpu::BindGroup,
|
||||
pub uniform_buf: wgpu::Buffer,
|
||||
|
@ -13,33 +15,299 @@ pub struct ShadowPass {
|
|||
pub lights_are_dirty: bool,
|
||||
}
|
||||
|
||||
pub struct ShadowPass {
|
||||
pub shadow_size: wgpu::Extent3d,
|
||||
light_index: isize,
|
||||
shadow_texture: Option<wgpu::Texture>,
|
||||
shadow_format: wgpu::TextureFormat,
|
||||
pub max_lights: usize,
|
||||
}
|
||||
|
||||
impl ShadowPass {
|
||||
pub fn new(shadow_size: wgpu::Extent3d, shadow_format: wgpu::TextureFormat, max_lights: usize) -> Self {
|
||||
ShadowPass {
|
||||
light_index: -1,
|
||||
shadow_texture: None,
|
||||
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;
|
||||
}
|
||||
|
||||
if let None = light.target_view {
|
||||
light.target_view = Some(self.shadow_texture.as_ref().unwrap().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,
|
||||
// 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 ShadowPass {
|
||||
fn render(&mut self, device: &Device, _: &SwapChainOutput, encoder: &mut CommandEncoder, world: &mut World, render_resources: &RenderResources) {
|
||||
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, (mut light, _)) in <(Write<Light>, Read<LocalToWorld>)>::query().iter(world).enumerate() {
|
||||
if let None = light.target_view {
|
||||
light.target_view = Some(self.shadow_texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
format: ShadowPass::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,
|
||||
}));
|
||||
}
|
||||
}
|
||||
<(Read<Material>, Read<Handle<Mesh>>)>::query().filter(!component::<Instanced>());
|
||||
|
||||
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,
|
||||
// let's just copy it over to the shadow uniform buffer.
|
||||
encoder.copy_buffer_to_buffer(
|
||||
|
@ -65,7 +333,10 @@ impl Pipeline for ShadowPass {
|
|||
pass.set_pipeline(&self.pipeline);
|
||||
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) {
|
||||
if let Some(mesh_asset) = mesh_storage.get(*mesh.id.read().unwrap()) {
|
||||
mesh_asset.setup_buffers(device);
|
||||
|
@ -73,18 +344,19 @@ impl Pipeline for ShadowPass {
|
|||
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);
|
||||
|
||||
pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resize(&mut self, _: &Device, _: &SwapChainDescriptor) { }
|
||||
fn get_camera_uniform_buffer(&self) -> Option<&Buffer> { None }
|
||||
fn resize(&mut self, _: &Device, _: &SwapChainDescriptor) {}
|
||||
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_SIZE: wgpu::Extent3d = wgpu::Extent3d {
|
||||
width: 512,
|
||||
|
@ -92,18 +364,25 @@ impl ShadowPass {
|
|||
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
|
||||
let bind_group_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||
binding: 0, // global
|
||||
visibility: wgpu::ShaderStage::VERTEX,
|
||||
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
||||
}],
|
||||
});
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||
binding: 0, // global
|
||||
visibility: wgpu::ShaderStage::VERTEX,
|
||||
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
||||
}],
|
||||
});
|
||||
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;
|
||||
|
@ -119,7 +398,7 @@ impl ShadowPass {
|
|||
binding: 0,
|
||||
resource: wgpu::BindingResource::Buffer {
|
||||
buffer: &uniform_buf,
|
||||
range: 0 .. uniform_size,
|
||||
range: 0..uniform_size,
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
@ -150,8 +429,7 @@ impl ShadowPass {
|
|||
});
|
||||
|
||||
// Create the render pipeline
|
||||
let vs_bytes =
|
||||
shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex);
|
||||
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 = device.create_shader_module(&vs_bytes);
|
||||
|
@ -192,7 +470,7 @@ impl ShadowPass {
|
|||
alpha_to_coverage_enabled: false,
|
||||
});
|
||||
|
||||
ShadowPass {
|
||||
ShadowPassOld {
|
||||
pipeline,
|
||||
bind_group,
|
||||
uniform_buf,
|
||||
|
|
Loading…
Reference in a new issue