Move texture loader in Resource

This commit is contained in:
Florian RICHER 2025-06-12 12:56:04 +02:00
parent 37467d5066
commit 6a0491fe51
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
3 changed files with 31 additions and 28 deletions

View file

@ -15,7 +15,7 @@ use crate::core::render::primitives::vulkan_resource::{
VulkanCommandBufferAllocator, VulkanDevice, VulkanGraphicsQueue, VulkanMemoryAllocator, VulkanCommandBufferAllocator, VulkanDevice, VulkanGraphicsQueue, VulkanMemoryAllocator,
VulkanTransferQueue, VulkanTransferQueue,
}; };
use bevy_ecs::world::World; use bevy_ecs::{resource::Resource, world::World};
use super::Texture; use super::Texture;
@ -30,6 +30,7 @@ pub struct TextureLoadInfo {
pub image_format: Format, pub image_format: Format,
} }
#[derive(Resource)]
pub struct TextureLoader { pub struct TextureLoader {
loaded_textures: HashMap<String, Arc<Texture>>, loaded_textures: HashMap<String, Arc<Texture>>,
pending_textures: HashMap<String, TextureLoadInfo>, pending_textures: HashMap<String, TextureLoadInfo>,
@ -40,19 +41,27 @@ pub struct TextureLoader {
} }
impl 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 { Self {
loaded_textures: HashMap::new(), loaded_textures: HashMap::new(),
pending_textures: HashMap::new(), pending_textures: HashMap::new(),
device: VulkanDevice::get_from_world(world).clone(), device,
command_buffer_allocator: VulkanCommandBufferAllocator::get_from_world(world).clone(), command_buffer_allocator,
memory_allocator: VulkanMemoryAllocator::get_from_world(world).clone(), memory_allocator,
queue: Self::select_best_suitable_queue(world), queue,
} }
} }
fn select_best_suitable_queue(world: &World) -> Arc<Queue> { pub fn select_best_suitable_queue(
VulkanTransferQueue::get_from_world(world) graphics_queue: &Arc<Queue>,
transfer_queue: Option<&Arc<Queue>>,
) -> Arc<Queue> {
transfer_queue
.map(|queue| { .map(|queue| {
tracing::trace!( tracing::trace!(
"Select transfer queue for texture loading with family index: {:?}", "Select transfer queue for texture loading with family index: {:?}",
@ -61,7 +70,6 @@ impl TextureLoader {
queue.clone() queue.clone()
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| {
let graphics_queue = VulkanGraphicsQueue::get_from_world(world);
tracing::trace!( tracing::trace!(
"Select graphics queue for texture loading with family index: {:?}", "Select graphics queue for texture loading with family index: {:?}",
graphics_queue.queue_family_index() graphics_queue.queue_family_index()

View file

@ -7,6 +7,7 @@ use crate::core::render::primitives::vulkan_resource::{
VulkanCommandBufferAllocator, VulkanComputeQueue, VulkanDescriptorSetAllocator, VulkanDevice, VulkanCommandBufferAllocator, VulkanComputeQueue, VulkanDescriptorSetAllocator, VulkanDevice,
VulkanGraphicsQueue, VulkanInstance, VulkanMemoryAllocator, VulkanTransferQueue, VulkanGraphicsQueue, VulkanInstance, VulkanMemoryAllocator, VulkanTransferQueue,
}; };
use crate::core::render::resources::texture::TextureLoader;
use bevy_ecs::world::World; use bevy_ecs::world::World;
use super::{AsScene, Scene}; use super::{AsScene, Scene};
@ -14,7 +15,6 @@ use super::{AsScene, Scene};
pub struct SceneManager { pub struct SceneManager {
scenes: Vec<Scene>, scenes: Vec<Scene>,
current_scene_index: Option<usize>, current_scene_index: Option<usize>,
window_context: Option<Rc<RefCell<WindowContext>>>,
} }
impl SceneManager { impl SceneManager {
@ -22,14 +22,9 @@ impl SceneManager {
Self { Self {
scenes: Vec::new(), scenes: Vec::new(),
current_scene_index: None, 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 { fn create_world_with_resources(window_context: &WindowContext) -> World {
let mut world = World::new(); let mut world = World::new();
@ -45,6 +40,13 @@ impl SceneManager {
let vulkan_command_buffer_allocator = vulkan_context.command_buffer_allocator(); let vulkan_command_buffer_allocator = vulkan_context.command_buffer_allocator();
let vulkan_descriptor_set_allocator = vulkan_context.descriptor_set_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(VulkanInstance(vulkan_instance.clone()));
world.insert_resource(VulkanDevice(vulkan_device.clone())); world.insert_resource(VulkanDevice(vulkan_device.clone()));
world.insert_resource(VulkanGraphicsQueue(vulkan_graphics_queue.clone())); world.insert_resource(VulkanGraphicsQueue(vulkan_graphics_queue.clone()));
@ -57,6 +59,7 @@ impl SceneManager {
world.insert_resource(VulkanDescriptorSetAllocator( world.insert_resource(VulkanDescriptorSetAllocator(
vulkan_descriptor_set_allocator.clone(), vulkan_descriptor_set_allocator.clone(),
)); ));
world.insert_resource(texture_loader);
world world
} }

View file

@ -42,7 +42,6 @@ pub struct Square;
pub struct Cube; pub struct Cube;
pub struct MainSceneState { pub struct MainSceneState {
texture_loader: TextureLoader,
pipeline_loader: PipelineLoader, pipeline_loader: PipelineLoader,
square: SquareMesh, square: SquareMesh,
obj: ObjMesh, obj: ObjMesh,
@ -81,7 +80,7 @@ impl AsScene for MainScene {
pipeline_loader.register::<SimplePipeline>()?; pipeline_loader.register::<SimplePipeline>()?;
pipeline_loader.load_pending_pipelines()?; pipeline_loader.load_pending_pipelines()?;
let mut texture_loader = TextureLoader::new(world); let mut texture_loader = world.resource_mut::<TextureLoader>();
texture_loader.add_texture( texture_loader.add_texture(
"wooden-crate".to_string(), "wooden-crate".to_string(),
TextureLoadInfo { TextureLoadInfo {
@ -143,7 +142,6 @@ impl AsScene for MainScene {
camera, camera,
speed: 50.0, speed: 50.0,
scheduler, scheduler,
texture_loader,
}); });
Ok(()) Ok(())
@ -258,6 +256,8 @@ impl AsScene for MainScene {
&cube_transforms, &cube_transforms,
)?; )?;
let texture_loader = world.resource::<TextureLoader>();
state state
.pipeline_loader .pipeline_loader
.with_pipeline::<SimplePipeline, _>(|pipeline| { .with_pipeline::<SimplePipeline, _>(|pipeline| {
@ -269,11 +269,7 @@ impl AsScene for MainScene {
&square_transform_uniform, &square_transform_uniform,
vec![ vec![
camera_uniform.clone() as Arc<dyn AsDescriptorSet>, 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, &cube_transform_uniform,
vec![ vec![
camera_uniform.clone() as Arc<dyn AsDescriptorSet>, camera_uniform.clone() as Arc<dyn AsDescriptorSet>,
state texture_loader.get_texture("cube-diffuse").unwrap().clone(),
.texture_loader
.get_texture("cube-diffuse")
.unwrap()
.clone(),
], ],
)?; )?;