mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
render: remove RenderResources wrapper type
This commit is contained in:
parent
0fec350411
commit
574656b767
18 changed files with 141 additions and 167 deletions
|
@ -3,7 +3,7 @@ use bevy_render::{
|
|||
render_resource::{
|
||||
BufferInfo, BufferUsage, RenderResourceBinding, RenderResourceBindings,
|
||||
},
|
||||
renderer::{RenderContext, RenderResources},
|
||||
renderer::{RenderContext, RenderResourceContext},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -60,7 +60,7 @@ impl SystemNode for LightsNode {
|
|||
let mut command_queue = self.command_queue.clone();
|
||||
let max_lights = self.max_lights;
|
||||
(move |world: &mut SubWorld,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
// TODO: this write on RenderResourceAssignments will prevent this system from running in parallel with other systems that do the same
|
||||
mut render_resource_bindings: ResMut<RenderResourceBindings>,
|
||||
query: &mut Query<(Read<Light>, Read<Transform>, Read<Translation>)>| {
|
||||
|
@ -68,12 +68,12 @@ impl SystemNode for LightsNode {
|
|||
return;
|
||||
}
|
||||
|
||||
let render_resources = &render_resources.context;
|
||||
let render_resource_context = &**render_resource_context;
|
||||
if light_buffer.is_none() {
|
||||
let light_uniform_size = std::mem::size_of::<LightCount>()
|
||||
+ max_lights * std::mem::size_of::<LightRaw>();
|
||||
|
||||
let buffer = render_resources.create_buffer(BufferInfo {
|
||||
let buffer = render_resource_context.create_buffer(BufferInfo {
|
||||
size: light_uniform_size,
|
||||
buffer_usage: BufferUsage::UNIFORM
|
||||
| BufferUsage::COPY_SRC
|
||||
|
@ -103,14 +103,14 @@ impl SystemNode for LightsNode {
|
|||
let light_count_size = std::mem::size_of::<LightCount>();
|
||||
|
||||
if let Some(old_tmp_light_buffer) = tmp_light_buffer {
|
||||
render_resources.remove_buffer(old_tmp_light_buffer);
|
||||
render_resource_context.remove_buffer(old_tmp_light_buffer);
|
||||
}
|
||||
|
||||
if let Some(old_tmp_count_buffer) = tmp_count_buffer {
|
||||
render_resources.remove_buffer(old_tmp_count_buffer);
|
||||
render_resource_context.remove_buffer(old_tmp_count_buffer);
|
||||
}
|
||||
|
||||
tmp_light_buffer = Some(render_resources.create_buffer_mapped(
|
||||
tmp_light_buffer = Some(render_resource_context.create_buffer_mapped(
|
||||
BufferInfo {
|
||||
size: total_size,
|
||||
buffer_usage: BufferUsage::COPY_SRC,
|
||||
|
@ -126,7 +126,7 @@ impl SystemNode for LightsNode {
|
|||
}
|
||||
},
|
||||
));
|
||||
tmp_count_buffer = Some(render_resources.create_buffer_mapped(
|
||||
tmp_count_buffer = Some(render_resource_context.create_buffer_mapped(
|
||||
BufferInfo {
|
||||
size: light_count_size,
|
||||
buffer_usage: BufferUsage::COPY_SRC,
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use crate::{
|
||||
pipeline::{BindGroupDescriptor, BindGroupDescriptorId, PipelineDescriptor},
|
||||
render_resource::{
|
||||
BufferId, BufferUsage, RenderResource, RenderResourceBinding, RenderResourceBindings,
|
||||
BindGroup, BindGroupId, SharedBuffers,
|
||||
BindGroup, BindGroupId, BufferId, BufferUsage, RenderResource, RenderResourceBinding,
|
||||
RenderResourceBindings, SharedBuffers,
|
||||
},
|
||||
renderer::{RenderResourceContext, RenderResources},
|
||||
renderer::RenderResourceContext,
|
||||
};
|
||||
use bevy_asset::{Assets, Handle};
|
||||
use bevy_property::Properties;
|
||||
|
@ -90,7 +90,7 @@ impl Draw {
|
|||
draw: self,
|
||||
pipelines,
|
||||
render_resource_context,
|
||||
render_resource_bindings: render_resource_bindings,
|
||||
render_resource_bindings,
|
||||
shared_buffers,
|
||||
current_pipeline: None,
|
||||
}
|
||||
|
@ -252,15 +252,14 @@ impl Drawable for RenderPipelines {
|
|||
pub fn draw_system<T: Drawable + Component>(
|
||||
pipelines: Res<Assets<PipelineDescriptor>>,
|
||||
render_resource_bindings: Res<RenderResourceBindings>,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
shared_buffers: Res<SharedBuffers>,
|
||||
mut draw: ComMut<Draw>,
|
||||
mut drawable: ComMut<T>,
|
||||
) {
|
||||
let context = &*render_resources.context;
|
||||
let mut draw_context = draw.get_context(
|
||||
&pipelines,
|
||||
context,
|
||||
&**render_resource_context,
|
||||
&render_resource_bindings,
|
||||
&shared_buffers,
|
||||
);
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
|||
AsVertexBufferDescriptor, VertexBufferDescriptor, VertexBufferDescriptors, VertexFormat,
|
||||
},
|
||||
render_resource::{BufferInfo, BufferUsage, RenderResourceId},
|
||||
renderer::{RenderResourceContext, RenderResources},
|
||||
renderer::RenderResourceContext,
|
||||
RenderPipelines, Vertex,
|
||||
};
|
||||
use bevy_app::{EventReader, Events};
|
||||
|
@ -321,20 +321,20 @@ pub mod shape {
|
|||
}
|
||||
|
||||
fn remove_current_mesh_resources(
|
||||
render_resources: &dyn RenderResourceContext,
|
||||
render_resource_context: &dyn RenderResourceContext,
|
||||
handle: Handle<Mesh>,
|
||||
) {
|
||||
if let Some(RenderResourceId::Buffer(buffer)) =
|
||||
render_resources.get_asset_resource(handle, VERTEX_BUFFER_ASSET_INDEX)
|
||||
render_resource_context.get_asset_resource(handle, VERTEX_BUFFER_ASSET_INDEX)
|
||||
{
|
||||
render_resources.remove_buffer(buffer);
|
||||
render_resources.remove_asset_resource(handle, VERTEX_BUFFER_ASSET_INDEX);
|
||||
render_resource_context.remove_buffer(buffer);
|
||||
render_resource_context.remove_asset_resource(handle, VERTEX_BUFFER_ASSET_INDEX);
|
||||
}
|
||||
if let Some(RenderResourceId::Buffer(buffer)) =
|
||||
render_resources.get_asset_resource(handle, INDEX_BUFFER_ASSET_INDEX)
|
||||
render_resource_context.get_asset_resource(handle, INDEX_BUFFER_ASSET_INDEX)
|
||||
{
|
||||
render_resources.remove_buffer(buffer);
|
||||
render_resources.remove_asset_resource(handle, INDEX_BUFFER_ASSET_INDEX);
|
||||
render_resource_context.remove_buffer(buffer);
|
||||
render_resource_context.remove_asset_resource(handle, INDEX_BUFFER_ASSET_INDEX);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -345,12 +345,12 @@ pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box<dyn Sched
|
|||
let vertex_buffer_descriptor = Vertex::as_vertex_buffer_descriptor();
|
||||
vertex_buffer_descriptors.set(vertex_buffer_descriptor.clone());
|
||||
(move |world: &mut SubWorld,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
meshes: Res<Assets<Mesh>>,
|
||||
mesh_events: Res<Events<AssetEvent<Mesh>>>,
|
||||
query: &mut Query<(Read<Handle<Mesh>>, Write<RenderPipelines>)>| {
|
||||
let render_resources = &*render_resources.context;
|
||||
let mut changed_meshes = HashSet::new();
|
||||
let render_resource_context = &**render_resource_context;
|
||||
for event in mesh_event_reader.iter(&mesh_events) {
|
||||
match event {
|
||||
AssetEvent::Created { handle } => {
|
||||
|
@ -358,10 +358,10 @@ pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box<dyn Sched
|
|||
}
|
||||
AssetEvent::Modified { handle } => {
|
||||
changed_meshes.insert(*handle);
|
||||
remove_current_mesh_resources(render_resources, *handle);
|
||||
remove_current_mesh_resources(render_resource_context, *handle);
|
||||
}
|
||||
AssetEvent::Removed { handle } => {
|
||||
remove_current_mesh_resources(render_resources, *handle);
|
||||
remove_current_mesh_resources(render_resource_context, *handle);
|
||||
// if mesh was modified and removed in the same update, ignore the modification
|
||||
// events are ordered so future modification events are ok
|
||||
changed_meshes.remove(handle);
|
||||
|
@ -375,7 +375,7 @@ pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box<dyn Sched
|
|||
.get_vertex_buffer_bytes(&vertex_buffer_descriptor)
|
||||
.unwrap();
|
||||
// TODO: use a staging buffer here
|
||||
let vertex_buffer = render_resources.create_buffer_with_data(
|
||||
let vertex_buffer = render_resource_context.create_buffer_with_data(
|
||||
BufferInfo {
|
||||
buffer_usage: BufferUsage::VERTEX,
|
||||
..Default::default()
|
||||
|
@ -384,7 +384,7 @@ pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box<dyn Sched
|
|||
);
|
||||
|
||||
let index_bytes = mesh.get_index_buffer_bytes(IndexFormat::Uint16).unwrap();
|
||||
let index_buffer = render_resources.create_buffer_with_data(
|
||||
let index_buffer = render_resource_context.create_buffer_with_data(
|
||||
BufferInfo {
|
||||
buffer_usage: BufferUsage::INDEX,
|
||||
..Default::default()
|
||||
|
@ -392,12 +392,12 @@ pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box<dyn Sched
|
|||
&index_bytes,
|
||||
);
|
||||
|
||||
render_resources.set_asset_resource(
|
||||
render_resource_context.set_asset_resource(
|
||||
*changed_mesh_handle,
|
||||
RenderResourceId::Buffer(vertex_buffer),
|
||||
VERTEX_BUFFER_ASSET_INDEX,
|
||||
);
|
||||
render_resources.set_asset_resource(
|
||||
render_resource_context.set_asset_resource(
|
||||
*changed_mesh_handle,
|
||||
RenderResourceId::Buffer(index_buffer),
|
||||
INDEX_BUFFER_ASSET_INDEX,
|
||||
|
@ -415,23 +415,21 @@ pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box<dyn Sched
|
|||
}
|
||||
|
||||
if let Some(RenderResourceId::Buffer(vertex_buffer)) =
|
||||
render_resources.get_asset_resource(*handle, VERTEX_BUFFER_ASSET_INDEX)
|
||||
render_resource_context.get_asset_resource(*handle, VERTEX_BUFFER_ASSET_INDEX)
|
||||
{
|
||||
render_pipelines
|
||||
.render_resource_bindings
|
||||
.set_vertex_buffer(
|
||||
"Vertex",
|
||||
vertex_buffer,
|
||||
render_resources
|
||||
.get_asset_resource(*handle, INDEX_BUFFER_ASSET_INDEX)
|
||||
.and_then(|r| {
|
||||
if let RenderResourceId::Buffer(buffer) = r {
|
||||
Some(buffer)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}),
|
||||
);
|
||||
render_pipelines.render_resource_bindings.set_vertex_buffer(
|
||||
"Vertex",
|
||||
vertex_buffer,
|
||||
render_resource_context
|
||||
.get_asset_resource(*handle, INDEX_BUFFER_ASSET_INDEX)
|
||||
.and_then(|r| {
|
||||
if let RenderResourceId::Buffer(buffer) = r {
|
||||
Some(buffer)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use super::{state_descriptors::PrimitiveTopology, PipelineDescriptor, VertexBufferDescriptors};
|
||||
use crate::{
|
||||
draw::RenderPipelines,
|
||||
renderer::{RenderResourceContext, RenderResources},
|
||||
renderer::RenderResourceContext,
|
||||
shader::{Shader, ShaderSource},
|
||||
};
|
||||
use bevy_asset::{Assets, Handle};
|
||||
|
@ -214,10 +214,10 @@ pub fn compile_pipelines_system(
|
|||
mut shaders: ResMut<Assets<Shader>>,
|
||||
mut pipelines: ResMut<Assets<PipelineDescriptor>>,
|
||||
vertex_buffer_descriptors: Res<VertexBufferDescriptors>,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
query: &mut Query<Write<RenderPipelines>>,
|
||||
) {
|
||||
let render_resource_context = &*render_resources.context;
|
||||
let render_resource_context = &**render_resource_context;
|
||||
|
||||
// TODO: only update when RenderPipelines is changed
|
||||
for mut render_pipelines in query.iter_mut(world) {
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
use crate::{
|
||||
render_graph::{CommandQueue, Node, ResourceSlots, SystemNode},
|
||||
render_resource::{
|
||||
BufferInfo, BufferUsage, RenderResourceBinding, RenderResourceBindings,
|
||||
},
|
||||
renderer::{RenderContext, RenderResources},
|
||||
render_resource::{BufferInfo, BufferUsage, RenderResourceBinding, RenderResourceBindings},
|
||||
renderer::{RenderResourceContext, RenderContext},
|
||||
Camera,
|
||||
};
|
||||
use bevy_core::bytes::AsBytes;
|
||||
|
@ -48,15 +46,15 @@ impl SystemNode for CameraNode {
|
|||
let mut command_queue = self.command_queue.clone();
|
||||
let uniform_name = self.uniform_name.clone();
|
||||
(move |world: &mut SubWorld,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
// PERF: this write on RenderResourceAssignments will prevent this system from running in parallel
|
||||
// with other systems that do the same
|
||||
mut render_resource_bindings: ResMut<RenderResourceBindings>,
|
||||
query: &mut Query<(Read<Camera>, Read<Transform>)>| {
|
||||
let render_resources = &render_resources.context;
|
||||
let render_resource_context = &**render_resource_context;
|
||||
if camera_buffer.is_none() {
|
||||
let size = std::mem::size_of::<[[f32; 4]; 4]>();
|
||||
let buffer = render_resources.create_buffer(BufferInfo {
|
||||
let buffer = render_resource_context.create_buffer(BufferInfo {
|
||||
size,
|
||||
buffer_usage: BufferUsage::COPY_DST | BufferUsage::UNIFORM,
|
||||
..Default::default()
|
||||
|
@ -79,7 +77,7 @@ impl SystemNode for CameraNode {
|
|||
let camera_matrix: [f32; 16] =
|
||||
(camera.view_matrix * transform.value).to_cols_array();
|
||||
|
||||
let tmp_buffer = render_resources.create_buffer_mapped(
|
||||
let tmp_buffer = render_resource_context.create_buffer_mapped(
|
||||
BufferInfo {
|
||||
size: matrix_size,
|
||||
buffer_usage: BufferUsage::COPY_SRC,
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
self, BufferInfo, BufferUsage, RenderResourceBinding, RenderResourceBindings,
|
||||
RenderResourceBindingsId, RenderResourceHints,
|
||||
},
|
||||
renderer::{RenderContext, RenderResourceContext, RenderResources},
|
||||
renderer::{RenderContext, RenderResourceContext},
|
||||
texture,
|
||||
};
|
||||
|
||||
|
@ -196,7 +196,7 @@ where
|
|||
&mut self,
|
||||
uniforms: &T,
|
||||
dynamic_uniforms: bool,
|
||||
render_resources: &dyn RenderResourceContext,
|
||||
render_resource_context: &dyn RenderResourceContext,
|
||||
render_resource_bindings: &mut RenderResourceBindings,
|
||||
staging_buffer: &mut [u8],
|
||||
) {
|
||||
|
@ -209,8 +209,8 @@ where
|
|||
let range = 0..size as u64;
|
||||
let (target_buffer, target_offset) = if dynamic_uniforms {
|
||||
let buffer = uniform_buffer_status.buffer.unwrap();
|
||||
let index = uniform_buffer_status
|
||||
.get_or_assign_index(render_resource_bindings.id);
|
||||
let index =
|
||||
uniform_buffer_status.get_or_assign_index(render_resource_bindings.id);
|
||||
render_resource_bindings.set(
|
||||
render_resource_name,
|
||||
RenderResourceBinding::Buffer {
|
||||
|
@ -225,13 +225,11 @@ where
|
|||
} else {
|
||||
let mut matching_buffer = None;
|
||||
let mut buffer_to_remove = None;
|
||||
if let Some(binding) =
|
||||
render_resource_bindings.get(render_resource_name)
|
||||
{
|
||||
if let Some(binding) = render_resource_bindings.get(render_resource_name) {
|
||||
let buffer_id = binding.get_buffer().unwrap();
|
||||
if let Some(BufferInfo {
|
||||
size: current_size, ..
|
||||
}) = render_resources.get_buffer_info(buffer_id)
|
||||
}) = render_resource_context.get_buffer_info(buffer_id)
|
||||
{
|
||||
if size == current_size {
|
||||
matching_buffer = Some(buffer_id);
|
||||
|
@ -243,7 +241,7 @@ where
|
|||
}
|
||||
|
||||
if let Some(buffer) = buffer_to_remove {
|
||||
render_resources.remove_buffer(buffer);
|
||||
render_resource_context.remove_buffer(buffer);
|
||||
}
|
||||
|
||||
let resource = if let Some(matching_buffer) = matching_buffer {
|
||||
|
@ -258,7 +256,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
let buffer = render_resources.create_buffer(BufferInfo {
|
||||
let buffer = render_resource_context.create_buffer(BufferInfo {
|
||||
size,
|
||||
buffer_usage: BufferUsage::COPY_DST | usage,
|
||||
..Default::default()
|
||||
|
@ -373,10 +371,10 @@ where
|
|||
let dynamic_uniforms = self.dynamic_uniforms;
|
||||
// TODO: maybe run "update" here
|
||||
(move |world: &mut SubWorld,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
query: &mut Query<(Read<T>, Read<Draw>, Write<RenderPipelines>)>| {
|
||||
let render_resource_context = &*render_resources.context;
|
||||
|
||||
|
||||
let render_resource_context = &** render_resource_context;
|
||||
uniform_buffer_arrays.reset_changed_item_counts();
|
||||
// update uniforms info
|
||||
for (uniforms, draw, _render_pipelines) in query.iter_mut(world) {
|
||||
|
@ -424,7 +422,7 @@ where
|
|||
size: staging_buffer_size,
|
||||
..Default::default()
|
||||
},
|
||||
&mut |mut staging_buffer, _render_resources| {
|
||||
&mut |mut staging_buffer, _render_resource_context| {
|
||||
for (uniforms, draw, mut render_pipelines) in query.iter_mut(world) {
|
||||
if !draw.is_visible {
|
||||
return;
|
||||
|
@ -505,9 +503,9 @@ where
|
|||
(move |world: &mut SubWorld,
|
||||
assets: Res<Assets<T>>,
|
||||
// asset_events: Res<Events<AssetEvent<T>>>,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
query: &mut Query<(Read<Handle<T>>, Read<Draw>, Write<RenderPipelines>)>| {
|
||||
let render_resource_context = &*render_resources.context;
|
||||
let render_resource_context = &**render_resource_context;
|
||||
uniform_buffer_arrays.reset_changed_item_counts();
|
||||
|
||||
let modified_assets = assets
|
||||
|
@ -575,7 +573,7 @@ where
|
|||
size: staging_buffer_size,
|
||||
..Default::default()
|
||||
},
|
||||
&mut |mut staging_buffer, _render_resources| {
|
||||
&mut |mut staging_buffer, _render_resource_context| {
|
||||
for asset_handle in modified_assets.iter() {
|
||||
let asset = assets.get(&asset_handle).expect(EXPECT_ASSET_MESSAGE);
|
||||
let mut render_resource_bindings = asset_render_resource_bindings
|
||||
|
@ -599,9 +597,7 @@ where
|
|||
}
|
||||
|
||||
for (asset_handle, _draw, mut render_pipelines) in query.iter_mut(world) {
|
||||
if let Some(asset_bindings) =
|
||||
asset_render_resource_bindings.get(&asset_handle)
|
||||
{
|
||||
if let Some(asset_bindings) = asset_render_resource_bindings.get(&asset_handle) {
|
||||
render_pipelines
|
||||
.render_resource_bindings
|
||||
.extend(asset_bindings);
|
||||
|
|
|
@ -54,7 +54,7 @@ impl Node for WindowSwapChainNode {
|
|||
.expect("Received window resized event for non-existent window"),
|
||||
};
|
||||
|
||||
let render_resources = render_context.resources_mut();
|
||||
let render_resource_context = render_context.resources_mut();
|
||||
|
||||
// create window swapchain when window is resized or created
|
||||
if self
|
||||
|
@ -66,10 +66,10 @@ impl Node for WindowSwapChainNode {
|
|||
.find_latest(&window_resized_events, |e| e.id == window.id)
|
||||
.is_some()
|
||||
{
|
||||
render_resources.create_swap_chain(window);
|
||||
render_resource_context.create_swap_chain(window);
|
||||
}
|
||||
|
||||
let swap_chain_texture = render_resources.next_swap_chain_texture(window.id);
|
||||
let swap_chain_texture = render_resource_context.next_swap_chain_texture(window.id);
|
||||
output.set(WINDOW_TEXTURE, RenderResourceId::Texture(swap_chain_texture));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,14 +66,14 @@ impl Node for WindowTextureNode {
|
|||
.find_latest(&window_resized_events, |e| e.id == window.id)
|
||||
.is_some()
|
||||
{
|
||||
let render_resources = render_context.resources_mut();
|
||||
let render_resource_context = render_context.resources_mut();
|
||||
if let Some(RenderResourceId::Texture(old_texture)) = output.get(WINDOW_TEXTURE) {
|
||||
render_resources.remove_texture(old_texture);
|
||||
render_resource_context.remove_texture(old_texture);
|
||||
}
|
||||
|
||||
self.descriptor.size.width = window.width;
|
||||
self.descriptor.size.height = window.height;
|
||||
let texture_resource = render_resources.create_texture(self.descriptor);
|
||||
let texture_resource = render_resource_context.create_texture(self.descriptor);
|
||||
output.set(WINDOW_TEXTURE, RenderResourceId::Texture(texture_resource));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use bevy_asset::Assets;
|
||||
use crate::{
|
||||
draw::RenderPipelines,
|
||||
pipeline::{PipelineCompiler, PipelineDescriptor},
|
||||
render_resource::{RenderResourceBindings, BindGroupStatus},
|
||||
renderer::{RenderResourceContext, RenderResources},
|
||||
render_resource::{BindGroupStatus, RenderResourceBindings},
|
||||
renderer::RenderResourceContext,
|
||||
};
|
||||
use bevy_asset::Assets;
|
||||
use legion::prelude::*;
|
||||
|
||||
fn update_bind_groups(
|
||||
|
@ -20,7 +20,7 @@ fn update_bind_groups(
|
|||
.get_bind_group(id)
|
||||
.expect("RenderResourceSet was just changed, so it should exist");
|
||||
render_resource_context.create_bind_group(bind_group_descriptor.id, bind_group);
|
||||
},
|
||||
}
|
||||
// TODO: Don't re-create bind groups if they havent changed. this will require cleanup of orphan bind groups and
|
||||
// removal of global context.clear_bind_groups()
|
||||
// PERF: see above
|
||||
|
@ -29,7 +29,7 @@ fn update_bind_groups(
|
|||
.get_bind_group(id)
|
||||
.expect("RenderResourceSet was just changed, so it should exist");
|
||||
render_resource_context.create_bind_group(bind_group_descriptor.id, bind_group);
|
||||
},
|
||||
}
|
||||
BindGroupStatus::NoMatch => {
|
||||
// ignore unchanged / unmatched render resource sets
|
||||
}
|
||||
|
@ -41,11 +41,11 @@ pub fn bind_groups_system(
|
|||
world: &mut SubWorld,
|
||||
pipelines: Res<Assets<PipelineDescriptor>>,
|
||||
pipeline_compiler: Res<PipelineCompiler>,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
mut render_resource_bindings: ResMut<RenderResourceBindings>,
|
||||
query: &mut Query<Write<RenderPipelines>>,
|
||||
) {
|
||||
let render_resource_context = &*render_resources.context;
|
||||
let render_resource_context = &**render_resource_context;
|
||||
for compiled_pipeline_handle in pipeline_compiler.iter_all_compiled_pipelines() {
|
||||
let pipeline = pipelines.get(compiled_pipeline_handle).unwrap();
|
||||
update_bind_groups(
|
||||
|
|
|
@ -8,21 +8,6 @@ use bevy_asset::{Assets, Handle, HandleUntyped};
|
|||
use bevy_window::{Window, WindowId};
|
||||
use downcast_rs::{impl_downcast, Downcast};
|
||||
|
||||
pub struct RenderResources {
|
||||
pub context: Box<dyn RenderResourceContext>,
|
||||
}
|
||||
|
||||
impl RenderResources {
|
||||
pub fn new<T>(context: T) -> RenderResources
|
||||
where
|
||||
T: RenderResourceContext,
|
||||
{
|
||||
RenderResources {
|
||||
context: Box::new(context),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RenderResourceContext: Downcast + Send + Sync + 'static {
|
||||
fn create_swap_chain(&self, window: &Window);
|
||||
fn next_swap_chain_texture(&self, window_id: WindowId) -> TextureId;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use super::{SamplerDescriptor, TextureDescriptor};
|
||||
use crate::{
|
||||
renderer::{RenderResourceContext, RenderResources},
|
||||
render_resource::{ResourceInfo, RenderResource, RenderResourceId},
|
||||
render_resource::{RenderResource, RenderResourceId, ResourceInfo},
|
||||
renderer::RenderResourceContext,
|
||||
};
|
||||
use bevy_app::{EventReader, Events};
|
||||
use bevy_asset::{AssetEvent, Assets, Handle};
|
||||
|
@ -47,11 +47,11 @@ impl Texture {
|
|||
|
||||
pub fn texture_resource_system(
|
||||
mut state: ResMut<TextureResourceSystemState>,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
textures: Res<Assets<Texture>>,
|
||||
texture_events: Res<Events<AssetEvent<Texture>>>,
|
||||
) {
|
||||
let render_resources = &*render_resources.context;
|
||||
let render_resource_context = &**render_resource_context;
|
||||
let mut changed_textures = HashSet::new();
|
||||
for event in state.event_reader.iter(&texture_events) {
|
||||
match event {
|
||||
|
@ -60,10 +60,10 @@ impl Texture {
|
|||
}
|
||||
AssetEvent::Modified { handle } => {
|
||||
changed_textures.insert(*handle);
|
||||
Self::remove_current_texture_resources(render_resources, *handle);
|
||||
Self::remove_current_texture_resources(render_resource_context, *handle);
|
||||
}
|
||||
AssetEvent::Removed { handle } => {
|
||||
Self::remove_current_texture_resources(render_resources, *handle);
|
||||
Self::remove_current_texture_resources(render_resource_context, *handle);
|
||||
// if texture was modified and removed in the same update, ignore the modification
|
||||
// events are ordered so future modification events are ok
|
||||
changed_textures.remove(handle);
|
||||
|
@ -74,17 +74,17 @@ impl Texture {
|
|||
for texture_handle in changed_textures.iter() {
|
||||
if let Some(texture) = textures.get(texture_handle) {
|
||||
let texture_descriptor: TextureDescriptor = texture.into();
|
||||
let texture_resource = render_resources.create_texture(texture_descriptor);
|
||||
let texture_resource = render_resource_context.create_texture(texture_descriptor);
|
||||
|
||||
let sampler_descriptor: SamplerDescriptor = texture.into();
|
||||
let sampler_resource = render_resources.create_sampler(&sampler_descriptor);
|
||||
let sampler_resource = render_resource_context.create_sampler(&sampler_descriptor);
|
||||
|
||||
render_resources.set_asset_resource(
|
||||
render_resource_context.set_asset_resource(
|
||||
*texture_handle,
|
||||
RenderResourceId::Texture(texture_resource),
|
||||
TEXTURE_ASSET_INDEX,
|
||||
);
|
||||
render_resources.set_asset_resource(
|
||||
render_resource_context.set_asset_resource(
|
||||
*texture_handle,
|
||||
RenderResourceId::Sampler(sampler_resource),
|
||||
SAMPLER_ASSET_INDEX,
|
||||
|
@ -94,16 +94,20 @@ impl Texture {
|
|||
}
|
||||
|
||||
fn remove_current_texture_resources(
|
||||
render_resources: &dyn RenderResourceContext,
|
||||
render_resource_context: &dyn RenderResourceContext,
|
||||
handle: Handle<Texture>,
|
||||
) {
|
||||
if let Some(RenderResourceId::Texture(resource)) = render_resources.get_asset_resource(handle, TEXTURE_ASSET_INDEX) {
|
||||
render_resources.remove_texture(resource);
|
||||
render_resources.remove_asset_resource(handle, TEXTURE_ASSET_INDEX);
|
||||
if let Some(RenderResourceId::Texture(resource)) =
|
||||
render_resource_context.get_asset_resource(handle, TEXTURE_ASSET_INDEX)
|
||||
{
|
||||
render_resource_context.remove_texture(resource);
|
||||
render_resource_context.remove_asset_resource(handle, TEXTURE_ASSET_INDEX);
|
||||
}
|
||||
if let Some(RenderResourceId::Sampler(resource)) = render_resources.get_asset_resource(handle, SAMPLER_ASSET_INDEX) {
|
||||
render_resources.remove_sampler(resource);
|
||||
render_resources.remove_asset_resource(handle, SAMPLER_ASSET_INDEX);
|
||||
if let Some(RenderResourceId::Sampler(resource)) =
|
||||
render_resource_context.get_asset_resource(handle, SAMPLER_ASSET_INDEX)
|
||||
{
|
||||
render_resource_context.remove_sampler(resource);
|
||||
render_resource_context.remove_asset_resource(handle, SAMPLER_ASSET_INDEX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,8 +121,7 @@ impl RenderResource for Option<Handle<Texture>> {
|
|||
fn resource_info(&self) -> Option<ResourceInfo> {
|
||||
self.map(|_texture| ResourceInfo::Texture(None))
|
||||
}
|
||||
fn write_buffer_bytes(&self, _buffer: &mut [u8]) {
|
||||
}
|
||||
fn write_buffer_bytes(&self, _buffer: &mut [u8]) {}
|
||||
fn buffer_byte_len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
|
@ -131,12 +134,11 @@ impl RenderResource for Handle<Texture> {
|
|||
fn resource_info(&self) -> Option<ResourceInfo> {
|
||||
Some(ResourceInfo::Texture(None))
|
||||
}
|
||||
fn write_buffer_bytes(&self, _buffer: &mut [u8]) {
|
||||
}
|
||||
fn write_buffer_bytes(&self, _buffer: &mut [u8]) {}
|
||||
fn buffer_byte_len(&self) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
fn texture(&self) -> Option<Handle<Texture>> {
|
||||
Some(self.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use bevy_render::{
|
|||
draw::Draw,
|
||||
pipeline::PipelineDescriptor,
|
||||
render_resource::{RenderResourceBindings, SharedBuffers},
|
||||
renderer::RenderResources,
|
||||
renderer::RenderResourceContext,
|
||||
texture::Texture,
|
||||
Color,
|
||||
};
|
||||
|
@ -80,7 +80,7 @@ impl Label {
|
|||
pub fn draw_label_system(
|
||||
_pipelines: Res<Assets<PipelineDescriptor>>,
|
||||
_render_resource_bindings: Res<RenderResourceBindings>,
|
||||
_render_resources: Res<RenderResources>,
|
||||
_render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
_shared_buffers: Res<SharedBuffers>,
|
||||
_fonts: Res<Assets<Font>>,
|
||||
_font_atlas_sets: Res<Assets<FontAtlasSet>>,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::renderer::WgpuRenderResourceContext;
|
||||
use bevy_app::{AppBuilder, AppPlugin};
|
||||
use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics};
|
||||
use bevy_render::renderer::RenderResources;
|
||||
use bevy_render::renderer::RenderResourceContext;
|
||||
use legion::prelude::{IntoSystem, Res, ResMut};
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -94,10 +94,9 @@ impl WgpuResourceDiagnosticsPlugin {
|
|||
|
||||
pub fn diagnostic_system(
|
||||
mut diagnostics: ResMut<Diagnostics>,
|
||||
render_resources: Res<RenderResources>,
|
||||
render_resource_context: Res<Box<dyn RenderResourceContext>>,
|
||||
) {
|
||||
let render_resource_context = render_resources
|
||||
.context
|
||||
let render_resource_context = render_resource_context
|
||||
.downcast_ref::<WgpuRenderResourceContext>()
|
||||
.unwrap();
|
||||
|
||||
|
|
|
@ -10,10 +10,7 @@ pub use wgpu_renderer::*;
|
|||
pub use wgpu_resources::*;
|
||||
|
||||
use bevy_app::{AppBuilder, AppPlugin};
|
||||
use bevy_render::{
|
||||
render_resource::{free_shared_buffers_system, SharedBuffers},
|
||||
renderer::RenderResources,
|
||||
};
|
||||
use bevy_render::{renderer::RenderResourceContext, render_resource::{free_shared_buffers_system, SharedBuffers}};
|
||||
use legion::prelude::*;
|
||||
use renderer::WgpuRenderResourceContext;
|
||||
|
||||
|
@ -33,7 +30,7 @@ impl AppPlugin for WgpuPlugin {
|
|||
|
||||
pub fn wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut World, &mut Resources) {
|
||||
let mut wgpu_renderer = pollster::block_on(WgpuRenderer::new());
|
||||
resources.insert(RenderResources::new(WgpuRenderResourceContext::new(
|
||||
resources.insert::<Box<dyn RenderResourceContext>>(Box::new(WgpuRenderResourceContext::new(
|
||||
wgpu_renderer.device.clone(),
|
||||
)));
|
||||
resources.insert(SharedBuffers::new(Box::new(
|
||||
|
|
|
@ -51,14 +51,14 @@ impl LazyCommandEncoder {
|
|||
pub struct WgpuRenderContext {
|
||||
pub device: Arc<wgpu::Device>,
|
||||
pub command_encoder: LazyCommandEncoder,
|
||||
pub render_resources: WgpuRenderResourceContext,
|
||||
pub render_resource_context: WgpuRenderResourceContext,
|
||||
}
|
||||
|
||||
impl WgpuRenderContext {
|
||||
pub fn new(device: Arc<wgpu::Device>, resources: WgpuRenderResourceContext) -> Self {
|
||||
WgpuRenderContext {
|
||||
device,
|
||||
render_resources: resources,
|
||||
render_resource_context: resources,
|
||||
command_encoder: LazyCommandEncoder::default(),
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ impl RenderContext for WgpuRenderContext {
|
|||
destination_offset: u64,
|
||||
size: u64,
|
||||
) {
|
||||
self.render_resources.copy_buffer_to_buffer(
|
||||
self.render_resource_context.copy_buffer_to_buffer(
|
||||
self.command_encoder.get_or_create(&self.device),
|
||||
source_buffer,
|
||||
source_offset,
|
||||
|
@ -99,7 +99,7 @@ impl RenderContext for WgpuRenderContext {
|
|||
destination_mip_level: u32,
|
||||
size: Extent3d,
|
||||
) {
|
||||
self.render_resources.copy_buffer_to_texture(
|
||||
self.render_resource_context.copy_buffer_to_texture(
|
||||
self.command_encoder.get_or_create(&self.device),
|
||||
source_buffer,
|
||||
source_offset,
|
||||
|
@ -112,10 +112,10 @@ impl RenderContext for WgpuRenderContext {
|
|||
}
|
||||
|
||||
fn resources(&self) -> &dyn RenderResourceContext {
|
||||
&self.render_resources
|
||||
&self.render_resource_context
|
||||
}
|
||||
fn resources_mut(&mut self) -> &mut dyn RenderResourceContext {
|
||||
&mut self.render_resources
|
||||
&mut self.render_resource_context
|
||||
}
|
||||
|
||||
fn begin_pass(
|
||||
|
@ -127,7 +127,7 @@ impl RenderContext for WgpuRenderContext {
|
|||
if !self.command_encoder.is_some() {
|
||||
self.command_encoder.create(&self.device);
|
||||
}
|
||||
let resource_lock = self.render_resources.resources.read();
|
||||
let resource_lock = self.render_resource_context.resources.read();
|
||||
let refs = resource_lock.refs();
|
||||
let mut encoder = self.command_encoder.take().unwrap();
|
||||
{
|
||||
|
@ -140,7 +140,7 @@ impl RenderContext for WgpuRenderContext {
|
|||
let mut wgpu_render_pass = WgpuRenderPass {
|
||||
render_pass,
|
||||
render_context: self,
|
||||
render_resources: refs,
|
||||
wgpu_resources: refs,
|
||||
pipeline_descriptor: None,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use super::{WgpuRenderContext, WgpuRenderResourceContext};
|
||||
use bevy_render::{
|
||||
render_graph::{Edge, NodeId, ResourceSlots, StageBorrow},
|
||||
renderer::RenderResources,
|
||||
renderer::RenderResourceContext,
|
||||
};
|
||||
use legion::prelude::{Resources, World};
|
||||
use std::{
|
||||
|
@ -22,9 +22,10 @@ impl WgpuRenderGraphExecutor {
|
|||
queue: &mut wgpu::Queue,
|
||||
stages: &mut [StageBorrow],
|
||||
) {
|
||||
let mut render_resources = resources.get_mut::<RenderResources>().unwrap();
|
||||
let wgpu_render_resources = render_resources
|
||||
.context
|
||||
let mut render_resource_context = resources
|
||||
.get_mut::<Box<dyn RenderResourceContext>>()
|
||||
.unwrap();
|
||||
let render_resource_context = render_resource_context
|
||||
.downcast_mut::<WgpuRenderResourceContext>()
|
||||
.unwrap();
|
||||
let node_outputs: Arc<RwLock<HashMap<NodeId, ResourceSlots>>> = Default::default();
|
||||
|
@ -41,10 +42,10 @@ impl WgpuRenderGraphExecutor {
|
|||
let world = &*world;
|
||||
actual_thread_count += 1;
|
||||
let device = device.clone();
|
||||
let wgpu_render_resources = wgpu_render_resources.clone();
|
||||
let render_resource_context = render_resource_context.clone();
|
||||
let node_outputs = node_outputs.clone();
|
||||
// s.spawn(move |_| {
|
||||
let mut render_context = WgpuRenderContext::new(device, wgpu_render_resources);
|
||||
let mut render_context = WgpuRenderContext::new(device, render_resource_context);
|
||||
for job in jobs_chunk.iter_mut() {
|
||||
for node_state in job.node_states.iter_mut() {
|
||||
// bind inputs from connected node outputs
|
||||
|
|
|
@ -11,7 +11,7 @@ use std::ops::Range;
|
|||
pub struct WgpuRenderPass<'a> {
|
||||
pub render_pass: wgpu::RenderPass<'a>,
|
||||
pub render_context: &'a WgpuRenderContext,
|
||||
pub render_resources: WgpuResourceRefs<'a>,
|
||||
pub wgpu_resources: WgpuResourceRefs<'a>,
|
||||
pub pipeline_descriptor: Option<&'a PipelineDescriptor>,
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ impl<'a> RenderPass for WgpuRenderPass<'a> {
|
|||
}
|
||||
|
||||
fn set_vertex_buffer(&mut self, start_slot: u32, buffer_id: BufferId, offset: u64) {
|
||||
let buffer = self.render_resources.buffers.get(&buffer_id).unwrap();
|
||||
let buffer = self.wgpu_resources.buffers.get(&buffer_id).unwrap();
|
||||
self.render_pass
|
||||
.set_vertex_buffer(start_slot, buffer.slice(offset..));
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ impl<'a> RenderPass for WgpuRenderPass<'a> {
|
|||
}
|
||||
|
||||
fn set_index_buffer(&mut self, buffer_id: BufferId, offset: u64) {
|
||||
let buffer = self.render_resources.buffers.get(&buffer_id).unwrap();
|
||||
let buffer = self.wgpu_resources.buffers.get(&buffer_id).unwrap();
|
||||
self.render_pass.set_index_buffer(buffer.slice(offset..));
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ impl<'a> RenderPass for WgpuRenderPass<'a> {
|
|||
dynamic_uniform_indices: Option<&[u32]>,
|
||||
) {
|
||||
if let Some(bind_group_info) = self
|
||||
.render_resources
|
||||
.wgpu_resources
|
||||
.bind_groups
|
||||
.get(&bind_group_descriptor)
|
||||
{
|
||||
|
@ -83,7 +83,7 @@ impl<'a> RenderPass for WgpuRenderPass<'a> {
|
|||
}
|
||||
|
||||
fn set_pipeline(&mut self, pipeline_handle: Handle<PipelineDescriptor>) {
|
||||
let pipeline = self.render_resources.render_pipelines.get(&pipeline_handle).expect(
|
||||
let pipeline = self.wgpu_resources.render_pipelines.get(&pipeline_handle).expect(
|
||||
"Attempted to use a pipeline that does not exist in this RenderPass's RenderContext",
|
||||
);
|
||||
self.render_pass.set_pipeline(pipeline);
|
||||
|
|
|
@ -2,7 +2,7 @@ use crate::renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext};
|
|||
use bevy_app::{EventReader, Events};
|
||||
use bevy_render::{
|
||||
render_graph::{DependentNodeStager, RenderGraph, RenderGraphStager},
|
||||
renderer::RenderResources,
|
||||
renderer::RenderResourceContext,
|
||||
};
|
||||
use bevy_window::{WindowCreated, WindowResized, Windows};
|
||||
use legion::prelude::*;
|
||||
|
@ -54,9 +54,10 @@ impl WgpuRenderer {
|
|||
}
|
||||
|
||||
pub fn handle_window_created_events(&mut self, resources: &Resources) {
|
||||
let mut render_resources = resources.get_mut::<RenderResources>().unwrap();
|
||||
let render_resource_context = render_resources
|
||||
.context
|
||||
let mut render_resource_context = resources
|
||||
.get_mut::<Box<dyn RenderResourceContext>>()
|
||||
.unwrap();
|
||||
let render_resource_context = render_resource_context
|
||||
.downcast_mut::<WgpuRenderResourceContext>()
|
||||
.unwrap();
|
||||
let windows = resources.get::<Windows>().unwrap();
|
||||
|
@ -102,10 +103,8 @@ impl WgpuRenderer {
|
|||
self.handle_window_created_events(resources);
|
||||
self.run_graph(world, resources);
|
||||
|
||||
let render_resource_context = resources.get::<RenderResources>().unwrap();
|
||||
render_resource_context
|
||||
.context
|
||||
.drop_all_swap_chain_textures();
|
||||
render_resource_context.context.clear_bind_groups();
|
||||
let render_resource_context = resources.get::<Box<dyn RenderResourceContext>>().unwrap();
|
||||
render_resource_context.drop_all_swap_chain_textures();
|
||||
render_resource_context.clear_bind_groups();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue