Move texture loader in Resource
This commit is contained in:
parent
37467d5066
commit
6a0491fe51
3 changed files with 31 additions and 28 deletions
|
@ -15,7 +15,7 @@ use crate::core::render::primitives::vulkan_resource::{
|
|||
VulkanCommandBufferAllocator, VulkanDevice, VulkanGraphicsQueue, VulkanMemoryAllocator,
|
||||
VulkanTransferQueue,
|
||||
};
|
||||
use bevy_ecs::world::World;
|
||||
use bevy_ecs::{resource::Resource, world::World};
|
||||
|
||||
use super::Texture;
|
||||
|
||||
|
@ -30,6 +30,7 @@ pub struct TextureLoadInfo {
|
|||
pub image_format: Format,
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct TextureLoader {
|
||||
loaded_textures: HashMap<String, Arc<Texture>>,
|
||||
pending_textures: HashMap<String, TextureLoadInfo>,
|
||||
|
@ -40,19 +41,27 @@ pub struct TextureLoader {
|
|||
}
|
||||
|
||||
impl TextureLoader {
|
||||
pub fn new(world: &World) -> Self {
|
||||
pub fn new(
|
||||
device: Arc<Device>,
|
||||
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
||||
memory_allocator: Arc<StandardMemoryAllocator>,
|
||||
queue: Arc<Queue>,
|
||||
) -> Self {
|
||||
Self {
|
||||
loaded_textures: HashMap::new(),
|
||||
pending_textures: HashMap::new(),
|
||||
device: VulkanDevice::get_from_world(world).clone(),
|
||||
command_buffer_allocator: VulkanCommandBufferAllocator::get_from_world(world).clone(),
|
||||
memory_allocator: VulkanMemoryAllocator::get_from_world(world).clone(),
|
||||
queue: Self::select_best_suitable_queue(world),
|
||||
device,
|
||||
command_buffer_allocator,
|
||||
memory_allocator,
|
||||
queue,
|
||||
}
|
||||
}
|
||||
|
||||
fn select_best_suitable_queue(world: &World) -> Arc<Queue> {
|
||||
VulkanTransferQueue::get_from_world(world)
|
||||
pub fn select_best_suitable_queue(
|
||||
graphics_queue: &Arc<Queue>,
|
||||
transfer_queue: Option<&Arc<Queue>>,
|
||||
) -> Arc<Queue> {
|
||||
transfer_queue
|
||||
.map(|queue| {
|
||||
tracing::trace!(
|
||||
"Select transfer queue for texture loading with family index: {:?}",
|
||||
|
@ -61,7 +70,6 @@ impl TextureLoader {
|
|||
queue.clone()
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
let graphics_queue = VulkanGraphicsQueue::get_from_world(world);
|
||||
tracing::trace!(
|
||||
"Select graphics queue for texture loading with family index: {:?}",
|
||||
graphics_queue.queue_family_index()
|
||||
|
|
|
@ -7,6 +7,7 @@ use crate::core::render::primitives::vulkan_resource::{
|
|||
VulkanCommandBufferAllocator, VulkanComputeQueue, VulkanDescriptorSetAllocator, VulkanDevice,
|
||||
VulkanGraphicsQueue, VulkanInstance, VulkanMemoryAllocator, VulkanTransferQueue,
|
||||
};
|
||||
use crate::core::render::resources::texture::TextureLoader;
|
||||
use bevy_ecs::world::World;
|
||||
|
||||
use super::{AsScene, Scene};
|
||||
|
@ -14,7 +15,6 @@ use super::{AsScene, Scene};
|
|||
pub struct SceneManager {
|
||||
scenes: Vec<Scene>,
|
||||
current_scene_index: Option<usize>,
|
||||
window_context: Option<Rc<RefCell<WindowContext>>>,
|
||||
}
|
||||
|
||||
impl SceneManager {
|
||||
|
@ -22,14 +22,9 @@ impl SceneManager {
|
|||
Self {
|
||||
scenes: Vec::new(),
|
||||
current_scene_index: None,
|
||||
window_context: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_window_context(&mut self, window_context: Rc<RefCell<WindowContext>>) {
|
||||
self.window_context = Some(window_context);
|
||||
}
|
||||
|
||||
fn create_world_with_resources(window_context: &WindowContext) -> World {
|
||||
let mut world = World::new();
|
||||
|
||||
|
@ -45,6 +40,13 @@ impl SceneManager {
|
|||
let vulkan_command_buffer_allocator = vulkan_context.command_buffer_allocator();
|
||||
let vulkan_descriptor_set_allocator = vulkan_context.descriptor_set_allocator();
|
||||
|
||||
let texture_loader = TextureLoader::new(
|
||||
vulkan_device.clone(),
|
||||
vulkan_command_buffer_allocator.clone(),
|
||||
vulkan_memory_allocator.clone(),
|
||||
TextureLoader::select_best_suitable_queue(vulkan_graphics_queue, vulkan_transfer_queue),
|
||||
);
|
||||
|
||||
world.insert_resource(VulkanInstance(vulkan_instance.clone()));
|
||||
world.insert_resource(VulkanDevice(vulkan_device.clone()));
|
||||
world.insert_resource(VulkanGraphicsQueue(vulkan_graphics_queue.clone()));
|
||||
|
@ -57,6 +59,7 @@ impl SceneManager {
|
|||
world.insert_resource(VulkanDescriptorSetAllocator(
|
||||
vulkan_descriptor_set_allocator.clone(),
|
||||
));
|
||||
world.insert_resource(texture_loader);
|
||||
|
||||
world
|
||||
}
|
||||
|
|
|
@ -42,7 +42,6 @@ pub struct Square;
|
|||
pub struct Cube;
|
||||
|
||||
pub struct MainSceneState {
|
||||
texture_loader: TextureLoader,
|
||||
pipeline_loader: PipelineLoader,
|
||||
square: SquareMesh,
|
||||
obj: ObjMesh,
|
||||
|
@ -81,7 +80,7 @@ impl AsScene for MainScene {
|
|||
pipeline_loader.register::<SimplePipeline>()?;
|
||||
pipeline_loader.load_pending_pipelines()?;
|
||||
|
||||
let mut texture_loader = TextureLoader::new(world);
|
||||
let mut texture_loader = world.resource_mut::<TextureLoader>();
|
||||
texture_loader.add_texture(
|
||||
"wooden-crate".to_string(),
|
||||
TextureLoadInfo {
|
||||
|
@ -143,7 +142,6 @@ impl AsScene for MainScene {
|
|||
camera,
|
||||
speed: 50.0,
|
||||
scheduler,
|
||||
texture_loader,
|
||||
});
|
||||
|
||||
Ok(())
|
||||
|
@ -258,6 +256,8 @@ impl AsScene for MainScene {
|
|||
&cube_transforms,
|
||||
)?;
|
||||
|
||||
let texture_loader = world.resource::<TextureLoader>();
|
||||
|
||||
state
|
||||
.pipeline_loader
|
||||
.with_pipeline::<SimplePipeline, _>(|pipeline| {
|
||||
|
@ -269,11 +269,7 @@ impl AsScene for MainScene {
|
|||
&square_transform_uniform,
|
||||
vec![
|
||||
camera_uniform.clone() as Arc<dyn AsDescriptorSet>,
|
||||
state
|
||||
.texture_loader
|
||||
.get_texture("wooden-crate")
|
||||
.unwrap()
|
||||
.clone(),
|
||||
texture_loader.get_texture("wooden-crate").unwrap().clone(),
|
||||
],
|
||||
)?;
|
||||
|
||||
|
@ -285,11 +281,7 @@ impl AsScene for MainScene {
|
|||
&cube_transform_uniform,
|
||||
vec![
|
||||
camera_uniform.clone() as Arc<dyn AsDescriptorSet>,
|
||||
state
|
||||
.texture_loader
|
||||
.get_texture("cube-diffuse")
|
||||
.unwrap()
|
||||
.clone(),
|
||||
texture_loader.get_texture("cube-diffuse").unwrap().clone(),
|
||||
],
|
||||
)?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue