2020-03-10 07:53:07 +00:00
|
|
|
use super::{wgpu_type_converter::OwnedWgpuVertexBufferDescriptor, WgpuRenderPass, WgpuResources};
|
2020-01-20 08:57:54 +00:00
|
|
|
use crate::{
|
2020-03-01 18:22:58 +00:00
|
|
|
asset::{AssetStorage, Handle},
|
2020-01-20 08:57:54 +00:00
|
|
|
legion::prelude::*,
|
2020-02-16 00:28:17 +00:00
|
|
|
render::{
|
2020-03-10 06:08:09 +00:00
|
|
|
pass::{
|
|
|
|
PassDescriptor, RenderPassColorAttachmentDescriptor,
|
|
|
|
RenderPassDepthStencilAttachmentDescriptor,
|
2020-02-16 00:28:17 +00:00
|
|
|
},
|
2020-03-18 01:25:27 +00:00
|
|
|
pipeline::{BindType, PipelineDescriptor, PipelineLayout, PipelineLayoutType, VertexBufferDescriptor},
|
2020-03-10 06:08:09 +00:00
|
|
|
render_graph::RenderGraph,
|
2020-03-10 07:53:07 +00:00
|
|
|
render_resource::{
|
|
|
|
resource_name, BufferUsage, RenderResource, RenderResources, ResourceInfo,
|
|
|
|
},
|
2020-03-10 06:08:09 +00:00
|
|
|
renderer::Renderer,
|
|
|
|
shader::{DynamicUniformBufferInfo, Shader},
|
|
|
|
texture::{SamplerDescriptor, TextureDescriptor},
|
|
|
|
update_shader_assignments,
|
2020-02-16 00:28:17 +00:00
|
|
|
},
|
2020-01-20 08:57:54 +00:00
|
|
|
};
|
2020-03-09 02:26:50 +00:00
|
|
|
use std::{collections::HashMap, ops::Deref};
|
2020-03-03 01:53:39 +00:00
|
|
|
|
|
|
|
pub struct WgpuRenderer {
|
|
|
|
pub device: wgpu::Device,
|
|
|
|
pub queue: wgpu::Queue,
|
|
|
|
pub surface: Option<wgpu::Surface>,
|
|
|
|
pub encoder: Option<wgpu::CommandEncoder>,
|
|
|
|
pub swap_chain_descriptor: wgpu::SwapChainDescriptor,
|
2020-03-05 08:44:53 +00:00
|
|
|
pub render_pipelines: HashMap<Handle<PipelineDescriptor>, wgpu::RenderPipeline>,
|
2020-03-18 01:25:27 +00:00
|
|
|
pub vertex_buffer_descriptors: HashMap<String, VertexBufferDescriptor>,
|
2020-03-03 01:53:39 +00:00
|
|
|
pub wgpu_resources: WgpuResources,
|
|
|
|
}
|
|
|
|
|
2020-01-20 08:57:54 +00:00
|
|
|
impl WgpuRenderer {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let adapter = wgpu::Adapter::request(
|
|
|
|
&wgpu::RequestAdapterOptions {
|
|
|
|
power_preference: wgpu::PowerPreference::Default,
|
|
|
|
},
|
|
|
|
wgpu::BackendBit::PRIMARY,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let (device, queue) = adapter.request_device(&wgpu::DeviceDescriptor {
|
|
|
|
extensions: wgpu::Extensions {
|
|
|
|
anisotropic_filtering: false,
|
|
|
|
},
|
|
|
|
limits: wgpu::Limits::default(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let swap_chain_descriptor = wgpu::SwapChainDescriptor {
|
|
|
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
|
|
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
present_mode: wgpu::PresentMode::Vsync,
|
|
|
|
};
|
|
|
|
|
|
|
|
WgpuRenderer {
|
|
|
|
device,
|
|
|
|
queue,
|
|
|
|
surface: None,
|
2020-02-04 08:06:17 +00:00
|
|
|
encoder: None,
|
2020-01-20 08:57:54 +00:00
|
|
|
swap_chain_descriptor,
|
2020-03-03 01:53:39 +00:00
|
|
|
wgpu_resources: WgpuResources::new(),
|
2020-03-05 08:44:53 +00:00
|
|
|
render_pipelines: HashMap::new(),
|
2020-03-18 01:25:27 +00:00
|
|
|
vertex_buffer_descriptors: HashMap::new(),
|
2020-01-20 08:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_render_pipeline(
|
2020-02-24 05:13:03 +00:00
|
|
|
render_resources: &RenderResources,
|
|
|
|
dynamic_uniform_buffer_info: &HashMap<RenderResource, DynamicUniformBufferInfo>,
|
2020-01-28 09:53:28 +00:00
|
|
|
pipeline_descriptor: &mut PipelineDescriptor,
|
2020-01-26 00:33:26 +00:00
|
|
|
bind_group_layouts: &mut HashMap<u64, wgpu::BindGroupLayout>,
|
2020-01-20 08:57:54 +00:00
|
|
|
device: &wgpu::Device,
|
2020-02-16 00:28:17 +00:00
|
|
|
vertex_shader: &Shader,
|
|
|
|
fragment_shader: Option<&Shader>,
|
2020-01-20 08:57:54 +00:00
|
|
|
) -> wgpu::RenderPipeline {
|
2020-02-17 22:12:10 +00:00
|
|
|
let vertex_spirv = vertex_shader.get_spirv_shader(None);
|
|
|
|
let fragment_spirv = fragment_shader.map(|f| f.get_spirv_shader(None));
|
|
|
|
|
|
|
|
let vertex_shader_module = Self::create_shader_module(device, &vertex_spirv, None);
|
2020-02-16 00:28:17 +00:00
|
|
|
let fragment_shader_module = match fragment_shader {
|
2020-02-17 22:12:10 +00:00
|
|
|
Some(fragment_spirv) => Some(Self::create_shader_module(device, fragment_spirv, None)),
|
2020-01-20 08:57:54 +00:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2020-02-17 22:12:10 +00:00
|
|
|
if let PipelineLayoutType::Reflected(None) = pipeline_descriptor.layout {
|
|
|
|
let mut layouts = vec![vertex_spirv.reflect_layout().unwrap()];
|
|
|
|
|
|
|
|
if let Some(ref fragment_spirv) = fragment_spirv {
|
|
|
|
layouts.push(fragment_spirv.reflect_layout().unwrap());
|
|
|
|
}
|
|
|
|
|
2020-02-18 00:33:46 +00:00
|
|
|
let mut layout = PipelineLayout::from_shader_layouts(&mut layouts);
|
|
|
|
|
|
|
|
// set each uniform binding to dynamic if there is a matching dynamic uniform buffer info
|
|
|
|
for mut bind_group in layout.bind_groups.iter_mut() {
|
|
|
|
bind_group.bindings = bind_group
|
|
|
|
.bindings
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.map(|mut binding| {
|
|
|
|
if let BindType::Uniform {
|
|
|
|
ref mut dynamic, ..
|
|
|
|
} = binding.bind_type
|
|
|
|
{
|
2020-02-24 05:13:03 +00:00
|
|
|
if let Some(resource) =
|
|
|
|
render_resources.get_named_resource(&binding.name)
|
|
|
|
{
|
|
|
|
if dynamic_uniform_buffer_info.contains_key(&resource) {
|
|
|
|
*dynamic = true;
|
|
|
|
}
|
2020-02-18 00:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
binding
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline_descriptor.layout = PipelineLayoutType::Reflected(Some(layout));
|
2020-02-17 22:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let layout = pipeline_descriptor.get_layout_mut().unwrap();
|
|
|
|
|
2020-01-26 00:33:26 +00:00
|
|
|
// setup new bind group layouts
|
2020-02-17 22:12:10 +00:00
|
|
|
for bind_group in layout.bind_groups.iter_mut() {
|
|
|
|
let bind_group_id = bind_group.get_or_update_hash();
|
2020-01-26 00:33:26 +00:00
|
|
|
if let None = bind_group_layouts.get(&bind_group_id) {
|
2020-01-23 08:31:56 +00:00
|
|
|
let bind_group_layout_binding = bind_group
|
|
|
|
.bindings
|
|
|
|
.iter()
|
2020-02-17 22:12:10 +00:00
|
|
|
.map(|binding| wgpu::BindGroupLayoutBinding {
|
|
|
|
binding: binding.index,
|
2020-01-23 08:31:56 +00:00
|
|
|
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
2020-01-24 07:39:56 +00:00
|
|
|
ty: (&binding.bind_type).into(),
|
2020-01-23 08:31:56 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<wgpu::BindGroupLayoutBinding>>();
|
2020-01-26 00:33:26 +00:00
|
|
|
let bind_group_layout =
|
|
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
bindings: bind_group_layout_binding.as_slice(),
|
|
|
|
});
|
|
|
|
|
|
|
|
bind_group_layouts.insert(bind_group_id, bind_group_layout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect bind group layout references
|
2020-02-17 22:12:10 +00:00
|
|
|
let bind_group_layouts = layout
|
2020-01-26 00:33:26 +00:00
|
|
|
.bind_groups
|
|
|
|
.iter()
|
|
|
|
.map(|bind_group| {
|
2020-02-17 22:12:10 +00:00
|
|
|
let bind_group_id = bind_group.get_hash().unwrap();
|
2020-01-26 00:33:26 +00:00
|
|
|
bind_group_layouts.get(&bind_group_id).unwrap()
|
2020-01-23 08:31:56 +00:00
|
|
|
})
|
2020-01-26 00:33:26 +00:00
|
|
|
.collect::<Vec<&wgpu::BindGroupLayout>>();
|
|
|
|
|
2020-01-20 08:57:54 +00:00
|
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
2020-01-27 05:44:01 +00:00
|
|
|
bind_group_layouts: bind_group_layouts.as_slice(),
|
2020-01-20 08:57:54 +00:00
|
|
|
});
|
2020-01-23 08:31:56 +00:00
|
|
|
|
2020-03-17 20:09:51 +00:00
|
|
|
let reflected_vertex_layout = if pipeline_descriptor.reflect_vertex_buffer_descriptors {
|
|
|
|
Some(vertex_spirv.reflect_layout().unwrap())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let vertex_buffer_descriptors = if let Some(ref layout) = reflected_vertex_layout {
|
|
|
|
&layout.vertex_buffer_descriptors
|
|
|
|
} else {
|
|
|
|
&pipeline_descriptor.vertex_buffer_descriptors
|
|
|
|
};
|
|
|
|
|
|
|
|
let owned_vertex_buffer_descriptors = vertex_buffer_descriptors
|
2020-03-10 07:53:07 +00:00
|
|
|
.iter()
|
|
|
|
.map(|v| v.into())
|
|
|
|
.collect::<Vec<OwnedWgpuVertexBufferDescriptor>>();
|
|
|
|
|
2020-03-11 04:57:57 +00:00
|
|
|
let color_states = pipeline_descriptor
|
|
|
|
.color_states
|
|
|
|
.iter()
|
|
|
|
.map(|c| c.into())
|
|
|
|
.collect::<Vec<wgpu::ColorStateDescriptor>>();
|
|
|
|
|
2020-01-28 09:53:28 +00:00
|
|
|
let mut render_pipeline_descriptor = wgpu::RenderPipelineDescriptor {
|
2020-01-20 08:57:54 +00:00
|
|
|
layout: &pipeline_layout,
|
|
|
|
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
|
|
|
module: &vertex_shader_module,
|
2020-02-17 22:12:10 +00:00
|
|
|
entry_point: "main",
|
2020-01-20 08:57:54 +00:00
|
|
|
},
|
2020-02-16 00:28:17 +00:00
|
|
|
fragment_stage: match fragment_shader {
|
2020-02-18 02:36:31 +00:00
|
|
|
Some(_) => Some(wgpu::ProgrammableStageDescriptor {
|
2020-02-17 22:12:10 +00:00
|
|
|
entry_point: "main",
|
2020-01-20 08:57:54 +00:00
|
|
|
module: fragment_shader_module.as_ref().unwrap(),
|
|
|
|
}),
|
|
|
|
None => None,
|
|
|
|
},
|
2020-03-11 04:57:57 +00:00
|
|
|
rasterization_state: pipeline_descriptor
|
|
|
|
.rasterization_state
|
|
|
|
.as_ref()
|
|
|
|
.map(|r| r.into()),
|
|
|
|
primitive_topology: pipeline_descriptor.primitive_topology.into(),
|
|
|
|
color_states: &color_states,
|
|
|
|
depth_stencil_state: pipeline_descriptor
|
|
|
|
.depth_stencil_state
|
|
|
|
.as_ref()
|
|
|
|
.map(|d| d.into()),
|
|
|
|
index_format: pipeline_descriptor.index_format.into(),
|
2020-03-10 07:53:07 +00:00
|
|
|
vertex_buffers: &owned_vertex_buffer_descriptors
|
2020-01-20 08:57:54 +00:00
|
|
|
.iter()
|
|
|
|
.map(|v| v.into())
|
|
|
|
.collect::<Vec<wgpu::VertexBufferDescriptor>>(),
|
|
|
|
sample_count: pipeline_descriptor.sample_count,
|
|
|
|
sample_mask: pipeline_descriptor.sample_mask,
|
|
|
|
alpha_to_coverage_enabled: pipeline_descriptor.alpha_to_coverage_enabled,
|
|
|
|
};
|
|
|
|
|
2020-01-28 09:53:28 +00:00
|
|
|
device.create_render_pipeline(&mut render_pipeline_descriptor)
|
2020-01-20 08:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_render_pass<'a>(
|
2020-03-05 08:44:53 +00:00
|
|
|
wgpu_resources: &'a WgpuResources,
|
2020-01-20 08:57:54 +00:00
|
|
|
pass_descriptor: &PassDescriptor,
|
|
|
|
encoder: &'a mut wgpu::CommandEncoder,
|
|
|
|
frame: &'a wgpu::SwapChainOutput,
|
|
|
|
) -> wgpu::RenderPass<'a> {
|
|
|
|
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
|
|
color_attachments: &pass_descriptor
|
|
|
|
.color_attachments
|
|
|
|
.iter()
|
2020-03-03 01:53:39 +00:00
|
|
|
.map(|c| Self::create_wgpu_color_attachment_descriptor(wgpu_resources, c, frame))
|
2020-01-20 08:57:54 +00:00
|
|
|
.collect::<Vec<wgpu::RenderPassColorAttachmentDescriptor>>(),
|
2020-03-09 03:06:59 +00:00
|
|
|
depth_stencil_attachment: pass_descriptor.depth_stencil_attachment.as_ref().map(|d| {
|
|
|
|
Self::create_wgpu_depth_stencil_attachment_descriptor(wgpu_resources, d, frame)
|
|
|
|
}),
|
2020-01-20 08:57:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_wgpu_color_attachment_descriptor<'a>(
|
2020-03-03 06:36:11 +00:00
|
|
|
wgpu_resources: &'a WgpuResources,
|
2020-01-20 08:57:54 +00:00
|
|
|
color_attachment_descriptor: &RenderPassColorAttachmentDescriptor,
|
|
|
|
frame: &'a wgpu::SwapChainOutput,
|
|
|
|
) -> wgpu::RenderPassColorAttachmentDescriptor<'a> {
|
|
|
|
let attachment = match color_attachment_descriptor.attachment.as_str() {
|
2020-01-23 08:31:56 +00:00
|
|
|
resource_name::texture::SWAP_CHAIN => &frame.view,
|
2020-02-24 05:13:03 +00:00
|
|
|
_ => {
|
2020-03-03 01:53:39 +00:00
|
|
|
match wgpu_resources
|
2020-02-24 05:13:03 +00:00
|
|
|
.render_resources
|
|
|
|
.get_named_resource(&color_attachment_descriptor.attachment)
|
|
|
|
{
|
2020-03-03 01:53:39 +00:00
|
|
|
Some(resource) => wgpu_resources.textures.get(&resource).unwrap(),
|
2020-02-24 05:13:03 +00:00
|
|
|
None => panic!(
|
|
|
|
"Color attachment {} does not exist",
|
|
|
|
&color_attachment_descriptor.attachment
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2020-01-20 08:57:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let resolve_target = match color_attachment_descriptor.resolve_target {
|
|
|
|
Some(ref target) => match target.as_str() {
|
2020-01-23 08:31:56 +00:00
|
|
|
resource_name::texture::SWAP_CHAIN => Some(&frame.view),
|
2020-03-09 03:06:59 +00:00
|
|
|
_ => match wgpu_resources
|
|
|
|
.render_resources
|
|
|
|
.get_named_resource(target.as_str())
|
|
|
|
{
|
2020-03-03 01:53:39 +00:00
|
|
|
Some(resource) => Some(wgpu_resources.textures.get(&resource).unwrap()),
|
2020-02-24 05:13:03 +00:00
|
|
|
None => panic!(
|
|
|
|
"Color attachment {} does not exist",
|
|
|
|
&color_attachment_descriptor.attachment
|
|
|
|
),
|
|
|
|
},
|
2020-01-20 08:57:54 +00:00
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
wgpu::RenderPassColorAttachmentDescriptor {
|
2020-03-10 07:53:07 +00:00
|
|
|
store_op: color_attachment_descriptor.store_op.into(),
|
|
|
|
load_op: color_attachment_descriptor.load_op.into(),
|
|
|
|
clear_color: color_attachment_descriptor.clear_color.into(),
|
2020-01-20 08:57:54 +00:00
|
|
|
attachment,
|
|
|
|
resolve_target,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_wgpu_depth_stencil_attachment_descriptor<'a>(
|
2020-03-03 01:53:39 +00:00
|
|
|
wgpu_resources: &'a WgpuResources,
|
2020-01-20 08:57:54 +00:00
|
|
|
depth_stencil_attachment_descriptor: &RenderPassDepthStencilAttachmentDescriptor,
|
|
|
|
frame: &'a wgpu::SwapChainOutput,
|
2020-03-05 08:44:53 +00:00
|
|
|
) -> wgpu::RenderPassDepthStencilAttachmentDescriptor<'a> {
|
2020-01-20 08:57:54 +00:00
|
|
|
let attachment = match depth_stencil_attachment_descriptor.attachment.as_str() {
|
2020-01-23 08:31:56 +00:00
|
|
|
resource_name::texture::SWAP_CHAIN => &frame.view,
|
2020-02-24 05:13:03 +00:00
|
|
|
_ => {
|
2020-03-03 01:53:39 +00:00
|
|
|
match wgpu_resources
|
2020-02-24 05:13:03 +00:00
|
|
|
.render_resources
|
|
|
|
.get_named_resource(&depth_stencil_attachment_descriptor.attachment)
|
|
|
|
{
|
2020-03-03 01:53:39 +00:00
|
|
|
Some(ref resource) => wgpu_resources.textures.get(&resource).unwrap(),
|
2020-02-24 05:13:03 +00:00
|
|
|
None => panic!(
|
|
|
|
"Depth stencil attachment {} does not exist",
|
|
|
|
&depth_stencil_attachment_descriptor.attachment
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2020-01-20 08:57:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
wgpu::RenderPassDepthStencilAttachmentDescriptor {
|
|
|
|
attachment,
|
|
|
|
clear_depth: depth_stencil_attachment_descriptor.clear_depth,
|
|
|
|
clear_stencil: depth_stencil_attachment_descriptor.clear_stencil,
|
2020-03-10 07:53:07 +00:00
|
|
|
depth_load_op: depth_stencil_attachment_descriptor.depth_load_op.into(),
|
|
|
|
depth_store_op: depth_stencil_attachment_descriptor.depth_store_op.into(),
|
|
|
|
stencil_load_op: depth_stencil_attachment_descriptor.stencil_load_op.into(),
|
|
|
|
stencil_store_op: depth_stencil_attachment_descriptor.stencil_store_op.into(),
|
2020-01-20 08:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-26 00:33:26 +00:00
|
|
|
|
2020-02-16 00:28:17 +00:00
|
|
|
pub fn create_shader_module(
|
|
|
|
device: &wgpu::Device,
|
|
|
|
shader: &Shader,
|
|
|
|
macros: Option<&[String]>,
|
|
|
|
) -> wgpu::ShaderModule {
|
2020-02-13 17:17:18 +00:00
|
|
|
device.create_shader_module(&shader.get_spirv(macros))
|
|
|
|
}
|
2020-02-18 00:33:46 +00:00
|
|
|
|
|
|
|
pub fn initialize_resource_providers(
|
|
|
|
&mut self,
|
|
|
|
world: &mut World,
|
2020-03-09 06:19:07 +00:00
|
|
|
resources: &mut Resources,
|
2020-02-18 00:33:46 +00:00
|
|
|
render_graph: &mut RenderGraph,
|
|
|
|
) {
|
|
|
|
self.encoder = Some(
|
|
|
|
self.device
|
|
|
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }),
|
|
|
|
);
|
|
|
|
for resource_provider in render_graph.resource_providers.iter_mut() {
|
2020-03-09 06:19:07 +00:00
|
|
|
resource_provider.initialize(self, world, resources);
|
2020-02-18 00:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// consume current encoder
|
|
|
|
let command_buffer = self.encoder.take().unwrap().finish();
|
|
|
|
self.queue.submit(&[command_buffer]);
|
|
|
|
}
|
2020-01-27 09:13:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 08:57:54 +00:00
|
|
|
impl Renderer for WgpuRenderer {
|
2020-03-09 06:19:07 +00:00
|
|
|
fn initialize(
|
|
|
|
&mut self,
|
|
|
|
world: &mut World,
|
|
|
|
resources: &mut Resources,
|
|
|
|
render_graph: &mut RenderGraph,
|
|
|
|
) {
|
2020-01-20 08:57:54 +00:00
|
|
|
let (surface, window_size) = {
|
2020-03-09 06:19:07 +00:00
|
|
|
let window = resources.get::<winit::window::Window>().unwrap();
|
2020-01-20 08:57:54 +00:00
|
|
|
let surface = wgpu::Surface::create(window.deref());
|
|
|
|
let window_size = window.inner_size();
|
|
|
|
(surface, window_size)
|
|
|
|
};
|
|
|
|
|
|
|
|
self.surface = Some(surface);
|
2020-02-18 00:33:46 +00:00
|
|
|
|
2020-03-09 06:19:07 +00:00
|
|
|
self.initialize_resource_providers(world, resources, render_graph);
|
2020-02-05 06:07:02 +00:00
|
|
|
|
2020-03-09 06:19:07 +00:00
|
|
|
self.resize(
|
|
|
|
world,
|
|
|
|
resources,
|
|
|
|
render_graph,
|
|
|
|
window_size.width,
|
|
|
|
window_size.height,
|
|
|
|
);
|
2020-01-20 08:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 05:44:01 +00:00
|
|
|
fn resize(
|
|
|
|
&mut self,
|
|
|
|
world: &mut World,
|
2020-03-09 06:19:07 +00:00
|
|
|
resources: &mut Resources,
|
2020-01-27 05:44:01 +00:00
|
|
|
render_graph: &mut RenderGraph,
|
|
|
|
width: u32,
|
|
|
|
height: u32,
|
|
|
|
) {
|
2020-02-05 06:07:02 +00:00
|
|
|
self.encoder = Some(
|
|
|
|
self.device
|
|
|
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }),
|
|
|
|
);
|
2020-01-20 10:03:05 +00:00
|
|
|
self.swap_chain_descriptor.width = width;
|
|
|
|
self.swap_chain_descriptor.height = height;
|
2020-01-20 08:57:54 +00:00
|
|
|
let swap_chain = self
|
|
|
|
.device
|
|
|
|
.create_swap_chain(self.surface.as_ref().unwrap(), &self.swap_chain_descriptor);
|
|
|
|
|
2020-01-23 08:31:56 +00:00
|
|
|
// WgpuRenderer can't own swap_chain without creating lifetime ergonomics issues, so lets just store it in World.
|
2020-03-09 06:19:07 +00:00
|
|
|
resources.insert(swap_chain);
|
2020-01-27 05:44:01 +00:00
|
|
|
for resource_provider in render_graph.resource_providers.iter_mut() {
|
2020-03-09 06:19:07 +00:00
|
|
|
resource_provider.resize(self, world, resources, width, height);
|
2020-01-27 05:44:01 +00:00
|
|
|
}
|
2020-02-06 01:50:56 +00:00
|
|
|
|
2020-02-05 06:07:02 +00:00
|
|
|
// consume current encoder
|
|
|
|
let command_buffer = self.encoder.take().unwrap().finish();
|
|
|
|
self.queue.submit(&[command_buffer]);
|
2020-01-20 08:57:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 06:19:07 +00:00
|
|
|
fn process_render_graph(
|
|
|
|
&mut self,
|
|
|
|
render_graph: &mut RenderGraph,
|
|
|
|
world: &mut World,
|
|
|
|
resources: &mut Resources,
|
|
|
|
) {
|
2020-02-04 08:06:17 +00:00
|
|
|
// TODO: this self.encoder handoff is a bit gross, but its here to give resource providers access to buffer copies without
|
|
|
|
// exposing the wgpu renderer internals to ResourceProvider traits. if this can be made cleaner that would be pretty cool.
|
2020-02-05 06:07:02 +00:00
|
|
|
self.encoder = Some(
|
|
|
|
self.device
|
|
|
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }),
|
|
|
|
);
|
2020-02-04 08:06:17 +00:00
|
|
|
|
2020-01-27 05:44:01 +00:00
|
|
|
for resource_provider in render_graph.resource_providers.iter_mut() {
|
2020-03-09 06:19:07 +00:00
|
|
|
resource_provider.update(self, world, resources);
|
2020-01-27 05:44:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 06:19:07 +00:00
|
|
|
update_shader_assignments(world, resources, render_graph);
|
2020-02-13 01:58:58 +00:00
|
|
|
|
2020-02-06 01:50:56 +00:00
|
|
|
for (name, texture_descriptor) in render_graph.queued_textures.drain(..) {
|
2020-02-24 07:41:48 +00:00
|
|
|
let resource = self.create_texture(&texture_descriptor, None);
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
|
|
|
.render_resources
|
|
|
|
.set_named_resource(&name, resource);
|
2020-02-08 07:17:51 +00:00
|
|
|
}
|
2020-02-06 01:50:56 +00:00
|
|
|
|
2020-02-04 08:06:17 +00:00
|
|
|
let mut encoder = self.encoder.take().unwrap();
|
|
|
|
|
2020-03-09 06:19:07 +00:00
|
|
|
let mut swap_chain = resources.get_mut::<wgpu::SwapChain>().unwrap();
|
2020-01-20 08:57:54 +00:00
|
|
|
let frame = swap_chain
|
|
|
|
.get_next_texture()
|
|
|
|
.expect("Timeout when acquiring next swap chain texture");
|
|
|
|
|
2020-02-04 05:00:00 +00:00
|
|
|
// self.setup_dynamic_entity_shader_uniforms(world, render_graph, &mut encoder);
|
2020-01-20 08:57:54 +00:00
|
|
|
|
2020-01-27 05:44:01 +00:00
|
|
|
// setup, pipelines, bind groups, and resources
|
2020-03-09 06:19:07 +00:00
|
|
|
let mut pipeline_storage = resources
|
2020-02-16 00:28:17 +00:00
|
|
|
.get_mut::<AssetStorage<PipelineDescriptor>>()
|
|
|
|
.unwrap();
|
2020-03-09 06:19:07 +00:00
|
|
|
let shader_storage = resources.get::<AssetStorage<Shader>>().unwrap();
|
2020-02-16 00:28:17 +00:00
|
|
|
|
|
|
|
for pipeline_descriptor_handle in render_graph.pipeline_descriptors.iter() {
|
|
|
|
let pipeline_descriptor = pipeline_storage
|
|
|
|
.get_mut(pipeline_descriptor_handle)
|
|
|
|
.unwrap();
|
2020-01-27 05:44:01 +00:00
|
|
|
// create pipelines
|
2020-02-16 00:28:17 +00:00
|
|
|
if !self
|
|
|
|
.render_pipelines
|
|
|
|
.contains_key(pipeline_descriptor_handle)
|
|
|
|
{
|
|
|
|
let vertex_shader = shader_storage
|
|
|
|
.get(&pipeline_descriptor.shader_stages.vertex)
|
|
|
|
.unwrap();
|
|
|
|
let fragment_shader = pipeline_descriptor
|
|
|
|
.shader_stages
|
|
|
|
.fragment
|
|
|
|
.as_ref()
|
|
|
|
.map(|handle| &*shader_storage.get(&handle).unwrap());
|
2020-01-27 05:44:01 +00:00
|
|
|
let render_pipeline = WgpuRenderer::create_render_pipeline(
|
2020-03-03 01:53:39 +00:00
|
|
|
&self.wgpu_resources.render_resources,
|
|
|
|
&self.wgpu_resources.dynamic_uniform_buffer_info,
|
2020-01-27 05:44:01 +00:00
|
|
|
pipeline_descriptor,
|
2020-03-03 01:53:39 +00:00
|
|
|
&mut self.wgpu_resources.bind_group_layouts,
|
2020-01-27 05:44:01 +00:00
|
|
|
&self.device,
|
2020-02-16 00:28:17 +00:00
|
|
|
vertex_shader,
|
|
|
|
fragment_shader,
|
2020-01-27 05:44:01 +00:00
|
|
|
);
|
2020-03-05 08:44:53 +00:00
|
|
|
self.render_pipelines
|
2020-02-24 07:50:44 +00:00
|
|
|
.insert(*pipeline_descriptor_handle, render_pipeline);
|
2020-01-27 05:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create bind groups
|
2020-02-17 22:12:10 +00:00
|
|
|
let pipeline_layout = pipeline_descriptor.get_layout().unwrap();
|
|
|
|
for bind_group in pipeline_layout.bind_groups.iter() {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
|
|
|
.setup_bind_group(&self.device, bind_group);
|
2020-01-27 05:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 03:06:59 +00:00
|
|
|
// setup draw targets
|
|
|
|
for (pass_name, _pass_descriptor) in render_graph.pass_descriptors.iter() {
|
|
|
|
if let Some(pass_pipelines) = render_graph.pass_pipelines.get(pass_name) {
|
|
|
|
for pass_pipeline in pass_pipelines.iter() {
|
|
|
|
let pipeline_descriptor = pipeline_storage.get(pass_pipeline).unwrap();
|
|
|
|
for draw_target_name in pipeline_descriptor.draw_targets.iter() {
|
|
|
|
let draw_target =
|
|
|
|
render_graph.draw_targets.get_mut(draw_target_name).unwrap();
|
2020-03-09 06:19:07 +00:00
|
|
|
draw_target.setup(world, resources, self, *pass_pipeline);
|
2020-03-09 03:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// begin render passes
|
2020-01-20 08:57:54 +00:00
|
|
|
for (pass_name, pass_descriptor) in render_graph.pass_descriptors.iter() {
|
2020-03-09 03:06:59 +00:00
|
|
|
let mut render_pass = Self::create_render_pass(
|
|
|
|
&self.wgpu_resources,
|
|
|
|
pass_descriptor,
|
|
|
|
&mut encoder,
|
|
|
|
&frame,
|
|
|
|
);
|
2020-01-20 08:57:54 +00:00
|
|
|
if let Some(pass_pipelines) = render_graph.pass_pipelines.get(pass_name) {
|
|
|
|
for pass_pipeline in pass_pipelines.iter() {
|
2020-02-16 00:28:17 +00:00
|
|
|
let pipeline_descriptor = pipeline_storage.get(pass_pipeline).unwrap();
|
2020-03-05 08:44:53 +00:00
|
|
|
let render_pipeline = self.render_pipelines.get(pass_pipeline).unwrap();
|
2020-02-16 00:28:17 +00:00
|
|
|
render_pass.set_pipeline(render_pipeline);
|
|
|
|
|
2020-03-05 08:44:53 +00:00
|
|
|
let mut wgpu_render_pass = WgpuRenderPass {
|
2020-02-16 00:28:17 +00:00
|
|
|
render_pass: &mut render_pass,
|
|
|
|
pipeline_descriptor,
|
2020-03-05 08:44:53 +00:00
|
|
|
wgpu_resources: &self.wgpu_resources,
|
2020-03-09 03:06:59 +00:00
|
|
|
renderer: &self,
|
2020-02-16 00:28:17 +00:00
|
|
|
};
|
|
|
|
|
2020-02-16 01:08:46 +00:00
|
|
|
for draw_target_name in pipeline_descriptor.draw_targets.iter() {
|
|
|
|
let draw_target = render_graph.draw_targets.get(draw_target_name).unwrap();
|
2020-03-09 06:19:07 +00:00
|
|
|
draw_target.draw(world, resources, &mut wgpu_render_pass, *pass_pipeline);
|
2020-01-20 08:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let command_buffer = encoder.finish();
|
|
|
|
self.queue.submit(&[command_buffer]);
|
|
|
|
}
|
2020-01-24 07:39:56 +00:00
|
|
|
|
|
|
|
fn create_buffer_with_data(
|
|
|
|
&mut self,
|
|
|
|
data: &[u8],
|
2020-03-10 07:53:07 +00:00
|
|
|
buffer_usage: BufferUsage,
|
2020-02-24 05:13:03 +00:00
|
|
|
) -> RenderResource {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
2020-03-10 07:53:07 +00:00
|
|
|
.create_buffer_with_data(&self.device, data, buffer_usage.into())
|
2020-01-20 08:57:54 +00:00
|
|
|
}
|
2020-01-24 07:39:56 +00:00
|
|
|
|
2020-03-10 07:53:07 +00:00
|
|
|
fn create_buffer(&mut self, size: u64, buffer_usage: BufferUsage) -> RenderResource {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
2020-03-10 07:53:07 +00:00
|
|
|
.create_buffer(&self.device, size, buffer_usage.into())
|
2020-01-27 05:44:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 07:17:51 +00:00
|
|
|
fn create_instance_buffer(
|
|
|
|
&mut self,
|
|
|
|
mesh_id: usize,
|
|
|
|
size: usize,
|
|
|
|
count: usize,
|
2020-03-10 07:53:07 +00:00
|
|
|
buffer_usage: BufferUsage,
|
2020-02-24 05:13:03 +00:00
|
|
|
) -> RenderResource {
|
2020-03-10 07:53:07 +00:00
|
|
|
self.wgpu_resources.create_instance_buffer(
|
|
|
|
&self.device,
|
|
|
|
mesh_id,
|
|
|
|
size,
|
|
|
|
count,
|
|
|
|
buffer_usage.into(),
|
|
|
|
)
|
2020-02-08 06:42:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 07:17:51 +00:00
|
|
|
fn create_instance_buffer_with_data(
|
|
|
|
&mut self,
|
|
|
|
mesh_id: usize,
|
|
|
|
data: &[u8],
|
|
|
|
size: usize,
|
|
|
|
count: usize,
|
2020-03-10 07:53:07 +00:00
|
|
|
buffer_usage: BufferUsage,
|
2020-02-24 05:13:03 +00:00
|
|
|
) -> RenderResource {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources.create_instance_buffer_with_data(
|
|
|
|
&self.device,
|
|
|
|
mesh_id,
|
|
|
|
data,
|
|
|
|
size,
|
|
|
|
count,
|
2020-03-10 07:53:07 +00:00
|
|
|
buffer_usage.into(),
|
2020-03-09 03:06:59 +00:00
|
|
|
)
|
2020-02-08 06:42:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 05:13:03 +00:00
|
|
|
fn get_resource_info(&self, resource: RenderResource) -> Option<&ResourceInfo> {
|
2020-03-03 06:36:11 +00:00
|
|
|
self.wgpu_resources.resource_info.get(&resource)
|
2020-01-23 08:31:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 05:13:03 +00:00
|
|
|
fn remove_buffer(&mut self, resource: RenderResource) {
|
2020-03-03 06:36:11 +00:00
|
|
|
self.wgpu_resources.remove_buffer(resource);
|
2020-01-24 07:39:56 +00:00
|
|
|
}
|
2020-02-04 05:00:00 +00:00
|
|
|
|
2020-02-05 06:07:02 +00:00
|
|
|
fn create_buffer_mapped(
|
|
|
|
&mut self,
|
|
|
|
size: usize,
|
2020-03-10 07:53:07 +00:00
|
|
|
buffer_usage: BufferUsage,
|
2020-02-05 06:07:02 +00:00
|
|
|
setup_data: &mut dyn FnMut(&mut [u8]),
|
2020-02-24 05:13:03 +00:00
|
|
|
) -> RenderResource {
|
2020-03-10 07:53:07 +00:00
|
|
|
self.wgpu_resources.create_buffer_mapped(
|
|
|
|
&self.device,
|
|
|
|
size,
|
|
|
|
buffer_usage.into(),
|
|
|
|
setup_data,
|
|
|
|
)
|
2020-02-04 05:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 06:07:02 +00:00
|
|
|
fn copy_buffer_to_buffer(
|
|
|
|
&mut self,
|
2020-02-24 05:13:03 +00:00
|
|
|
source_buffer: RenderResource,
|
2020-02-05 06:07:02 +00:00
|
|
|
source_offset: u64,
|
2020-02-24 05:13:03 +00:00
|
|
|
destination_buffer: RenderResource,
|
2020-02-05 06:07:02 +00:00
|
|
|
destination_offset: u64,
|
|
|
|
size: u64,
|
|
|
|
) {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources.copy_buffer_to_buffer(
|
|
|
|
self.encoder.as_mut().unwrap(),
|
|
|
|
source_buffer,
|
|
|
|
source_offset,
|
|
|
|
destination_buffer,
|
|
|
|
destination_offset,
|
|
|
|
size,
|
|
|
|
);
|
2020-02-04 05:00:00 +00:00
|
|
|
}
|
2020-03-03 06:36:11 +00:00
|
|
|
|
2020-02-24 05:13:03 +00:00
|
|
|
fn get_dynamic_uniform_buffer_info(
|
|
|
|
&self,
|
|
|
|
resource: RenderResource,
|
|
|
|
) -> Option<&DynamicUniformBufferInfo> {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
|
|
|
.get_dynamic_uniform_buffer_info(resource)
|
2020-02-04 05:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 06:07:02 +00:00
|
|
|
fn get_dynamic_uniform_buffer_info_mut(
|
|
|
|
&mut self,
|
2020-02-24 05:13:03 +00:00
|
|
|
resource: RenderResource,
|
2020-02-05 06:07:02 +00:00
|
|
|
) -> Option<&mut DynamicUniformBufferInfo> {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
|
|
|
.get_dynamic_uniform_buffer_info_mut(resource)
|
2020-02-04 05:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 05:13:03 +00:00
|
|
|
fn add_dynamic_uniform_buffer_info(
|
|
|
|
&mut self,
|
|
|
|
resource: RenderResource,
|
|
|
|
info: DynamicUniformBufferInfo,
|
|
|
|
) {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
|
|
|
.add_dynamic_uniform_buffer_info(resource, info);
|
2020-02-04 05:00:00 +00:00
|
|
|
}
|
2020-02-06 01:50:56 +00:00
|
|
|
|
2020-02-24 07:41:48 +00:00
|
|
|
fn create_sampler(&mut self, sampler_descriptor: &SamplerDescriptor) -> RenderResource {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
|
|
|
.create_sampler(&self.device, sampler_descriptor)
|
2020-02-24 05:13:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 07:41:48 +00:00
|
|
|
fn create_texture(
|
2020-02-24 05:13:03 +00:00
|
|
|
&mut self,
|
|
|
|
texture_descriptor: &TextureDescriptor,
|
|
|
|
bytes: Option<&[u8]>,
|
|
|
|
) -> RenderResource {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources.create_texture(
|
|
|
|
&self.device,
|
|
|
|
self.encoder.as_mut().unwrap(),
|
|
|
|
texture_descriptor,
|
|
|
|
bytes,
|
|
|
|
)
|
2020-02-24 05:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_texture(&mut self, resource: RenderResource) {
|
2020-03-03 06:36:11 +00:00
|
|
|
self.wgpu_resources.remove_texture(resource);
|
2020-02-06 01:50:56 +00:00
|
|
|
}
|
2020-02-24 07:41:48 +00:00
|
|
|
|
|
|
|
fn remove_sampler(&mut self, resource: RenderResource) {
|
2020-03-03 06:36:11 +00:00
|
|
|
self.wgpu_resources.remove_sampler(resource);
|
2020-02-24 07:41:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 18:22:58 +00:00
|
|
|
fn get_render_resources(&self) -> &RenderResources {
|
2020-03-03 06:36:11 +00:00
|
|
|
&self.wgpu_resources.render_resources
|
2020-02-24 07:41:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 18:22:58 +00:00
|
|
|
fn get_render_resources_mut(&mut self) -> &mut RenderResources {
|
2020-03-03 06:36:11 +00:00
|
|
|
&mut self.wgpu_resources.render_resources
|
2020-03-01 18:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:53:39 +00:00
|
|
|
fn set_entity_uniform_resource(
|
|
|
|
&mut self,
|
|
|
|
entity: Entity,
|
|
|
|
uniform_name: &str,
|
|
|
|
resource: RenderResource,
|
|
|
|
) {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
|
|
|
.set_entity_uniform_resource(entity, uniform_name, resource)
|
2020-03-01 18:22:58 +00:00
|
|
|
}
|
2020-03-03 01:53:39 +00:00
|
|
|
fn get_entity_uniform_resource(
|
|
|
|
&self,
|
|
|
|
entity: Entity,
|
|
|
|
uniform_name: &str,
|
|
|
|
) -> Option<RenderResource> {
|
2020-03-09 03:06:59 +00:00
|
|
|
self.wgpu_resources
|
|
|
|
.get_entity_uniform_resource(entity, uniform_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_entity_bind_groups(
|
|
|
|
&mut self,
|
|
|
|
entity: Entity,
|
|
|
|
pipeline_descriptor: &PipelineDescriptor,
|
|
|
|
) {
|
|
|
|
let pipeline_layout = pipeline_descriptor.get_layout().unwrap();
|
|
|
|
for bind_group in pipeline_layout.bind_groups.iter() {
|
|
|
|
let bind_group_id = bind_group.get_hash().unwrap();
|
|
|
|
// only setup entity bind groups if there isn't already a "global" bind group created
|
|
|
|
if let None = self.wgpu_resources.bind_groups.get(&bind_group_id) {
|
|
|
|
if let None = self
|
|
|
|
.wgpu_resources
|
|
|
|
.get_entity_bind_group(entity, bind_group_id)
|
|
|
|
{
|
|
|
|
self.wgpu_resources
|
|
|
|
.create_entity_bind_group(&self.device, bind_group, entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 07:41:48 +00:00
|
|
|
}
|
2020-03-18 01:25:27 +00:00
|
|
|
fn set_vertex_buffer_descriptor(
|
|
|
|
&mut self,
|
|
|
|
vertex_buffer_descriptor: VertexBufferDescriptor,
|
|
|
|
) {
|
|
|
|
self.vertex_buffer_descriptors.insert(vertex_buffer_descriptor.name.to_string(), vertex_buffer_descriptor);
|
|
|
|
}
|
|
|
|
fn get_vertex_buffer_descriptor(
|
|
|
|
&self,
|
|
|
|
name: &str
|
|
|
|
) -> Option<&VertexBufferDescriptor> {
|
|
|
|
self.vertex_buffer_descriptors.get(name)
|
|
|
|
}
|
2020-03-11 04:57:57 +00:00
|
|
|
}
|