cargo fmt

This commit is contained in:
Carter Anderson 2020-04-15 15:55:00 -07:00
parent c213865cbf
commit 71460baa3d
15 changed files with 174 additions and 127 deletions

View file

@ -27,7 +27,8 @@ impl DrawTarget for AssignedBatchesDrawTarget {
resources.get::<RenderResourceAssignments>().unwrap();
render_pass.set_render_resources(pipeline_descriptor, &global_render_resource_assignments);
for batch in asset_batches.get_batches() {
let indices = render_pass.set_render_resources(pipeline_descriptor, &batch.render_resource_assignments);
let indices = render_pass
.set_render_resources(pipeline_descriptor, &batch.render_resource_assignments);
log::trace!("drawing batch {:?}", batch.render_resource_assignments.id);
log::trace!("{:#?}", batch);
for batched_entity in batch.entities.iter() {
@ -38,8 +39,10 @@ impl DrawTarget for AssignedBatchesDrawTarget {
log::trace!("start drawing batched entity: {:?}", batched_entity);
log::trace!("{:#?}", renderable.render_resource_assignments);
let entity_indices =
render_pass.set_render_resources(pipeline_descriptor, &renderable.render_resource_assignments);
let entity_indices = render_pass.set_render_resources(
pipeline_descriptor,
&renderable.render_resource_assignments,
);
let mut draw_indices = &indices;
if entity_indices.is_some() {
if indices.is_some() {

View file

@ -53,11 +53,12 @@ impl DrawTarget for AssignedMeshesDrawTarget {
let render_context = render_pass.get_render_context();
let render_resources = render_context.resources();
if current_mesh_handle != Some(mesh_handle) {
if let Some(vertex_buffer_resource) =
render_resources.get_asset_resource(mesh_handle, mesh::VERTEX_BUFFER_ASSET_INDEX)
if let Some(vertex_buffer_resource) = render_resources
.get_asset_resource(mesh_handle, mesh::VERTEX_BUFFER_ASSET_INDEX)
{
let index_buffer_resource =
render_resources.get_asset_resource(mesh_handle, mesh::INDEX_BUFFER_ASSET_INDEX).unwrap();
let index_buffer_resource = render_resources
.get_asset_resource(mesh_handle, mesh::INDEX_BUFFER_ASSET_INDEX)
.unwrap();
render_resources.get_resource_info(
index_buffer_resource,
&mut |resource_info| match resource_info {

View file

@ -32,19 +32,21 @@ impl DrawTarget for MeshesDrawTarget {
let render_context = render_pass.get_render_context();
let render_resources = render_context.resources();
if current_mesh_handle != Some(*mesh_handle) {
if let Some(vertex_buffer_resource) =
render_resources.get_asset_resource(*mesh_handle, mesh::VERTEX_BUFFER_ASSET_INDEX)
if let Some(vertex_buffer_resource) = render_resources
.get_asset_resource(*mesh_handle, mesh::VERTEX_BUFFER_ASSET_INDEX)
{
let index_buffer_resource =
render_resources.get_asset_resource(*mesh_handle, mesh::INDEX_BUFFER_ASSET_INDEX).unwrap();
render_resources.get_resource_info(index_buffer_resource, &mut |resource_info| {
match resource_info {
let index_buffer_resource = render_resources
.get_asset_resource(*mesh_handle, mesh::INDEX_BUFFER_ASSET_INDEX)
.unwrap();
render_resources.get_resource_info(
index_buffer_resource,
&mut |resource_info| match resource_info {
Some(ResourceInfo::Buffer(buffer_info)) => {
current_mesh_index_len = (buffer_info.size / 2) as u32
}
_ => panic!("expected a buffer type"),
}
});
},
);
render_pass.set_index_buffer(index_buffer_resource, 0);
render_pass.set_vertex_buffer(0, vertex_buffer_resource, 0);
}

View file

@ -42,9 +42,9 @@ impl DrawTarget for UiDrawTarget {
let index_count = {
let mut index_count = None;
let render_context = render_pass.get_render_context();
render_context
.resources()
.get_resource_info(ui_instances_buffer, &mut |resource_info| {
render_context.resources().get_resource_info(
ui_instances_buffer,
&mut |resource_info| {
if let Some(ResourceInfo::Buffer(BufferInfo {
array_info: Some(array_info),
..
@ -52,7 +52,8 @@ impl DrawTarget for UiDrawTarget {
{
index_count = Some(array_info.item_capacity);
}
});
},
);
index_count
};

View file

@ -3,8 +3,8 @@ use crate::{
render_resource::{RenderResource, RenderResourceAssignments},
renderer_2::RenderContext,
};
use std::ops::Range;
use bevy_asset::Handle;
use std::ops::Range;
pub trait RenderPass {
fn get_render_context(&self) -> &dyn RenderContext;

View file

@ -60,22 +60,21 @@ impl PipelineCompiler {
for bind_group in layout.bind_groups.iter_mut() {
for binding in bind_group.bindings.iter_mut() {
if let Some(render_resource) = render_resource_assignments.get(&binding.name) {
render_context.resources().get_resource_info(
render_resource,
&mut |resource_info| {
if let Some(ResourceInfo::Buffer(BufferInfo {
is_dynamic, ..
})) = resource_info
render_context.resources().get_resource_info(
render_resource,
&mut |resource_info| {
if let Some(ResourceInfo::Buffer(BufferInfo { is_dynamic, .. })) =
resource_info
{
if let BindType::Uniform {
ref mut dynamic, ..
} = binding.bind_type
{
if let BindType::Uniform {
ref mut dynamic, ..
} = binding.bind_type
{
*dynamic = *is_dynamic
}
*dynamic = *is_dynamic
}
}
);
},
);
}
}
}

View file

@ -8,4 +8,4 @@ impl RenderResource {
pub fn new() -> Self {
RenderResource(Uuid::new_v4())
}
}
}

View file

@ -23,33 +23,42 @@ impl MeshResourceProvider {
render_resource_assignments: &mut RenderResourceAssignments,
) {
let render_resources = render_context.resources_mut();
let (vertex_buffer, index_buffer) =
if let Some(vertex_buffer) = render_resources.get_asset_resource(handle, mesh::VERTEX_BUFFER_ASSET_INDEX) {
(
vertex_buffer,
render_resources.get_asset_resource(handle, mesh::INDEX_BUFFER_ASSET_INDEX),
)
} else {
let mesh_asset = mesh_storage.get(&handle).unwrap();
let vertex_buffer = render_resources.create_buffer_with_data(
BufferInfo {
buffer_usage: BufferUsage::VERTEX,
..Default::default()
},
mesh_asset.vertices.as_bytes(),
);
let index_buffer = render_resources.create_buffer_with_data(
BufferInfo {
buffer_usage: BufferUsage::INDEX,
..Default::default()
},
mesh_asset.indices.as_bytes(),
);
let (vertex_buffer, index_buffer) = if let Some(vertex_buffer) =
render_resources.get_asset_resource(handle, mesh::VERTEX_BUFFER_ASSET_INDEX)
{
(
vertex_buffer,
render_resources.get_asset_resource(handle, mesh::INDEX_BUFFER_ASSET_INDEX),
)
} else {
let mesh_asset = mesh_storage.get(&handle).unwrap();
let vertex_buffer = render_resources.create_buffer_with_data(
BufferInfo {
buffer_usage: BufferUsage::VERTEX,
..Default::default()
},
mesh_asset.vertices.as_bytes(),
);
let index_buffer = render_resources.create_buffer_with_data(
BufferInfo {
buffer_usage: BufferUsage::INDEX,
..Default::default()
},
mesh_asset.indices.as_bytes(),
);
render_resources.set_asset_resource(handle, vertex_buffer, mesh::VERTEX_BUFFER_ASSET_INDEX);
render_resources.set_asset_resource(handle, index_buffer, mesh::INDEX_BUFFER_ASSET_INDEX);
(vertex_buffer, Some(index_buffer))
};
render_resources.set_asset_resource(
handle,
vertex_buffer,
mesh::VERTEX_BUFFER_ASSET_INDEX,
);
render_resources.set_asset_resource(
handle,
index_buffer,
mesh::INDEX_BUFFER_ASSET_INDEX,
);
(vertex_buffer, Some(index_buffer))
};
render_resource_assignments.set_vertex_buffer("Vertex", vertex_buffer, index_buffer);
}

View file

@ -6,7 +6,7 @@ use crate::{
},
renderer_2::{RenderContext, RenderResourceContext},
shader::{AsUniforms, FieldBindType},
texture::{SamplerDescriptor, Texture, TextureDescriptor, self},
texture::{self, SamplerDescriptor, Texture, TextureDescriptor},
Renderable,
};
use bevy_asset::{AssetStorage, Handle};
@ -401,9 +401,16 @@ where
let sampler_resource =
render_resources.create_sampler(&sampler_descriptor);
render_resources.set_asset_resource(texture_handle, texture_resource, 0);
render_resources
.set_asset_resource(texture_handle, sampler_resource, 1);
render_resources.set_asset_resource(
texture_handle,
texture_resource,
0,
);
render_resources.set_asset_resource(
texture_handle,
sampler_resource,
1,
);
(texture_resource, sampler_resource)
}
};

View file

@ -50,6 +50,6 @@ pub trait RenderContext {
&mut self,
pass_descriptor: &PassDescriptor,
render_resource_assignments: &RenderResourceAssignments,
run_pass: &mut dyn Fn(&mut dyn RenderPass)
run_pass: &mut dyn Fn(&mut dyn RenderPass),
);
}

View file

@ -5,7 +5,7 @@ use crate::{
};
use bevy_asset::{AssetStorage, Handle, HandleUntyped};
use bevy_window::{Window, WindowId};
use downcast_rs::{Downcast, impl_downcast};
use downcast_rs::{impl_downcast, Downcast};
pub struct GlobalRenderResourceContext {
pub context: Box<dyn RenderResourceContext>,
@ -80,4 +80,4 @@ impl dyn RenderResourceContext {
}
}
impl_downcast!(RenderResourceContext);
impl_downcast!(RenderResourceContext);

View file

@ -170,7 +170,9 @@ impl RenderContext for WgpuRenderContext {
BindType::SampledTexture { .. } => {
if let Some(ResourceInfo::Texture) = resource_info {
let texture = textures.get(&resource).unwrap();
Some(wgpu::BindingResource::TextureView(texture))
Some(wgpu::BindingResource::TextureView(
texture,
))
} else {
panic!("expected a Texture resource");
}
@ -268,7 +270,8 @@ impl RenderContext for WgpuRenderContext {
.bind_group_layouts
.read()
.unwrap()
.get(&bind_group.id).is_none()
.get(&bind_group.id)
.is_none()
{
let bind_group_layout_binding = bind_group
.bindings

View file

@ -3,7 +3,7 @@ use crate::WgpuResources;
use bevy_asset::{AssetStorage, Handle, HandleUntyped};
use bevy_render::{
render_resource::{BufferInfo, RenderResource, ResourceInfo},
renderer_2::{RenderResourceContext},
renderer_2::RenderResourceContext,
shader::Shader,
texture::{SamplerDescriptor, TextureDescriptor},
};
@ -25,8 +25,6 @@ impl WgpuRenderResourceContext {
}
}
// impl AnyRenderResourceContext for WgpuRenderResourceContext {}
impl RenderResourceContext for WgpuRenderResourceContext {
fn create_sampler(&mut self, sampler_descriptor: &SamplerDescriptor) -> RenderResource {
self.wgpu_resources

View file

@ -23,21 +23,13 @@ impl<'a> RenderPass for WgpuRenderPass<'a> {
}
fn set_vertex_buffer(&mut self, start_slot: u32, resource: RenderResource, offset: u64) {
let buffer = self
.render_resources
.buffers
.get(&resource)
.unwrap();
let buffer = self.render_resources.buffers.get(&resource).unwrap();
self.render_pass
.set_vertex_buffer(start_slot, &buffer, offset, 0);
}
fn set_index_buffer(&mut self, resource: RenderResource, offset: u64) {
let buffer = self
.render_resources
.buffers
.get(&resource)
.unwrap();
let buffer = self.render_resources.buffers.get(&resource).unwrap();
self.render_pass.set_index_buffer(&buffer, offset, 0);
}
@ -92,10 +84,7 @@ impl<'a> RenderPass for WgpuRenderPass<'a> {
if let Some((render_resource_set_id, dynamic_uniform_indices)) =
render_resource_assignments.get_render_resource_set_id(bind_group.id)
{
if let Some(bind_group_info) = self
.render_resources
.bind_groups
.get(&bind_group.id)
if let Some(bind_group_info) = self.render_resources.bind_groups.get(&bind_group.id)
{
if let Some(wgpu_bind_group) =
bind_group_info.bind_groups.get(render_resource_set_id)

View file

@ -1,10 +1,8 @@
use crate::{renderer_2::WgpuRenderResourceContext, wgpu_type_converter::WgpuInto};
use bevy_asset::{HandleUntyped, Handle};
use bevy_asset::{Handle, HandleUntyped};
use bevy_render::{
pipeline::{BindGroupDescriptorId, PipelineDescriptor},
render_resource::{
BufferInfo, RenderResource, RenderResourceSetId, ResourceInfo,
},
render_resource::{BufferInfo, RenderResource, RenderResourceSetId, ResourceInfo},
renderer_2::RenderResourceContext,
shader::Shader,
texture::{SamplerDescriptor, TextureDescriptor},
@ -20,33 +18,34 @@ pub struct WgpuBindGroupInfo {
pub bind_groups: HashMap<RenderResourceSetId, wgpu::BindGroup>,
}
/// Grabs a read lock on all wgpu resources. When paired with WgpuResourceRefs, this allows
/// Grabs a read lock on all wgpu resources. When paired with WgpuResourceRefs, this allows
/// us to pass in wgpu resources to wgpu::RenderPass<'a> with the appropriate lifetime. This is accomplished by
/// grabbing a WgpuResourcesReadLock _before_ creating a wgpu::RenderPass, getting a WgpuResourcesRefs, and storing that
/// in the pass.
///
/// in the pass.
///
/// This is only a problem because RwLockReadGuard.read() erases the guard's lifetime and creates a new anonymous lifetime. If
/// you call RwLockReadGuard.read() during a pass, the reference will have an anonymous lifetime that lives for less than the
/// pass, which violates the lifetime constraints in place.
///
///
/// The biggest implication of this design (other than the additional boilerplate here) is that beginning a render pass
/// blocks writes to these resources. This means that if the pass attempts to write any resource, a deadlock will occur. WgpuResourceRefs
/// only has immutable references, so the only way to make a deadlock happen is to access WgpuResources directly in the pass. It also means
/// that other threads attempting to write resources will need to wait for pass encoding to finish. Almost all writes should occur before
/// passes start, so this hopefully won't be a problem.
///
/// passes start, so this hopefully won't be a problem.
///
/// It is worth comparing the performance of this to transactional / copy-based approaches. This lock based design guarantees
/// consistency, doesn't perform redundant allocations, and only blocks when a write is occurring. A copy based approach would
/// consistency, doesn't perform redundant allocations, and only blocks when a write is occurring. A copy based approach would
/// never block, but would require more allocations / state-synchronization, which I expect will be more expensive. It would also be
/// "eventually consistent" instead of "strongly consistent".
///
///
/// Single threaded implementations don't need to worry about these lifetimes constraints at all. RenderPasses can use a RenderContext's
/// WgpuResources directly. RenderContext already has a lifetime greater than the RenderPass.
pub struct WgpuResourcesReadLock<'a> {
pub buffers: RwLockReadGuard<'a, HashMap<RenderResource, wgpu::Buffer>>,
pub textures: RwLockReadGuard<'a, HashMap<RenderResource, wgpu::TextureView>>,
pub swap_chain_outputs: RwLockReadGuard<'a, HashMap<WindowId, wgpu::SwapChainOutput>>,
pub render_pipelines: RwLockReadGuard<'a, HashMap<Handle<PipelineDescriptor>, wgpu::RenderPipeline>>,
pub render_pipelines:
RwLockReadGuard<'a, HashMap<Handle<PipelineDescriptor>, wgpu::RenderPipeline>>,
pub bind_groups: RwLockReadGuard<'a, HashMap<BindGroupDescriptorId, WgpuBindGroupInfo>>,
}
@ -59,10 +58,10 @@ impl<'a> WgpuResourcesReadLock<'a> {
render_pipelines: &self.render_pipelines,
bind_groups: &self.bind_groups,
}
}
}
}
/// Stores read only references to WgpuResource collections. See WgpuResourcesReadLock docs for context on why this exists
/// Stores read only references to WgpuResource collections. See WgpuResourcesReadLock docs for context on why this exists
pub struct WgpuResourceRefs<'a> {
pub buffers: &'a HashMap<RenderResource, wgpu::Buffer>,
pub textures: &'a HashMap<RenderResource, wgpu::TextureView>,
@ -97,7 +96,7 @@ impl WgpuResources {
render_pipelines: self.render_pipelines.read().unwrap(),
bind_groups: self.bind_groups.read().unwrap(),
}
}
}
pub fn set_window_surface(&self, window_id: WindowId, surface: wgpu::Surface) {
self.window_surfaces
@ -107,13 +106,8 @@ impl WgpuResources {
}
pub fn next_swap_chain_texture(&self, window_id: WindowId) {
let mut swap_chain_outputs = self
.window_swap_chains
.write()
.unwrap();
let swap_chain_output = swap_chain_outputs
.get_mut(&window_id)
.unwrap();
let mut swap_chain_outputs = self.window_swap_chains.write().unwrap();
let swap_chain_output = swap_chain_outputs.get_mut(&window_id).unwrap();
let next_texture = swap_chain_output.get_next_texture().unwrap();
self.swap_chain_outputs
.write()
@ -160,7 +154,10 @@ impl WgpuResources {
.unwrap()
.get(&bind_group_descriptor_id)
{
bind_group_info.bind_groups.get(&render_resource_set_id).is_some()
bind_group_info
.bind_groups
.get(&render_resource_set_id)
.is_some()
} else {
false
}
@ -186,10 +183,7 @@ impl WgpuResources {
render_resource_set_id: RenderResourceSetId,
bind_group: wgpu::BindGroup,
) {
let mut bind_groups = self
.bind_groups
.write()
.unwrap();
let mut bind_groups = self.bind_groups.write().unwrap();
let bind_group_info = bind_groups
.entry(bind_group_descriptor_id)
.or_insert_with(|| WgpuBindGroupInfo::default());
@ -224,7 +218,11 @@ impl WgpuResources {
}
// TODO: taking a closure isn't fantastic. is there any way to make this better without exposing the lock in the interface?
pub fn get_resource_info(&self, resource: RenderResource, handle_info: &mut dyn FnMut(Option<&ResourceInfo>)) {
pub fn get_resource_info(
&self,
resource: RenderResource,
handle_info: &mut dyn FnMut(Option<&ResourceInfo>),
) {
let resource_info = self.resource_info.read().unwrap();
let info = resource_info.get(&resource);
handle_info(info);
@ -362,21 +360,58 @@ impl WgpuResources {
self.resource_info.write().unwrap().remove(&resource);
}
pub fn set_asset_resource<T>(&mut self, handle: Handle<T>, render_resource: RenderResource, index: usize) where T: 'static {
self.asset_resources.write().unwrap().insert((handle.into(), index), render_resource);
}
pub fn set_asset_resource<T>(
&mut self,
handle: Handle<T>,
render_resource: RenderResource,
index: usize,
) where
T: 'static,
{
self.asset_resources
.write()
.unwrap()
.insert((handle.into(), index), render_resource);
}
pub fn get_asset_resource<T>(&mut self, handle: Handle<T>, index: usize) -> Option<RenderResource> where T: 'static {
self.asset_resources.write().unwrap().get(&(handle.into(), index)).cloned()
}
pub fn get_asset_resource<T>(
&mut self,
handle: Handle<T>,
index: usize,
) -> Option<RenderResource>
where
T: 'static,
{
self.asset_resources
.write()
.unwrap()
.get(&(handle.into(), index))
.cloned()
}
pub fn set_asset_resource_untyped(&mut self, handle: HandleUntyped, render_resource: RenderResource, index: usize) {
self.asset_resources.write().unwrap().insert((handle, index), render_resource);
}
pub fn set_asset_resource_untyped(
&mut self,
handle: HandleUntyped,
render_resource: RenderResource,
index: usize,
) {
self.asset_resources
.write()
.unwrap()
.insert((handle, index), render_resource);
}
pub fn get_asset_resource_untyped(&self, handle: HandleUntyped, index: usize) -> Option<RenderResource> {
self.asset_resources.write().unwrap().get(&(handle, index)).cloned()
}
pub fn get_asset_resource_untyped(
&self,
handle: HandleUntyped,
index: usize,
) -> Option<RenderResource> {
self.asset_resources
.write()
.unwrap()
.get(&(handle, index))
.cloned()
}
pub fn create_bind_group_layout(
&self,