mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
cargo fmt
This commit is contained in:
parent
c213865cbf
commit
71460baa3d
15 changed files with 174 additions and 127 deletions
|
@ -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() {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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>,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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},
|
||||
|
@ -46,7 +44,8 @@ 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>>,
|
||||
}
|
||||
|
||||
|
@ -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,20 +360,57 @@ 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(
|
||||
|
|
Loading…
Reference in a new issue