From 5c75f69b4eec4ff24d665f854afbb52a26ad4636 Mon Sep 17 00:00:00 2001 From: Lukas Lihotzki Date: Thu, 25 Jul 2024 10:48:35 +0200 Subject: [PATCH] Refactor to separate texture parameter --- blade-egui/src/lib.rs | 16 +++++---- blade-graphics/src/gles/resource.rs | 7 ++-- blade-graphics/src/lib.rs | 3 +- blade-graphics/src/metal/resource.rs | 5 +-- blade-graphics/src/traits.rs | 7 ++-- blade-graphics/src/vulkan/resource.rs | 9 ++--- blade-render/src/render/dummy.rs | 48 +++++++++++++++------------ blade-render/src/render/env_map.rs | 36 +++++++++++--------- blade-render/src/render/mod.rs | 20 ++++++----- blade-render/src/texture/mod.rs | 10 +++--- examples/bunnymark/main.rs | 14 ++++---- examples/init/main.rs | 16 +++++---- examples/mini/main.rs | 22 ++++++------ examples/ray-query/main.rs | 16 +++++---- 14 files changed, 128 insertions(+), 101 deletions(-) diff --git a/blade-egui/src/lib.rs b/blade-egui/src/lib.rs index 4bbd8fd6..c8e8b825 100644 --- a/blade-egui/src/lib.rs +++ b/blade-egui/src/lib.rs @@ -71,13 +71,15 @@ impl GuiTexture { dimension: blade_graphics::TextureDimension::D2, usage: blade_graphics::TextureUsage::COPY | blade_graphics::TextureUsage::RESOURCE, }); - let view = context.create_texture_view(blade_graphics::TextureViewDesc { - name, - texture: allocation, - format, - dimension: blade_graphics::ViewDimension::D2, - subresources: &blade_graphics::TextureSubresources::default(), - }); + let view = context.create_texture_view( + allocation, + blade_graphics::TextureViewDesc { + name, + format, + dimension: blade_graphics::ViewDimension::D2, + subresources: &blade_graphics::TextureSubresources::default(), + }, + ); Self { allocation, view } } diff --git a/blade-graphics/src/gles/resource.rs b/blade-graphics/src/gles/resource.rs index bf20f51a..426a7759 100644 --- a/blade-graphics/src/gles/resource.rs +++ b/blade-graphics/src/gles/resource.rs @@ -233,12 +233,13 @@ impl crate::traits::ResourceDevice for super::Context { fn create_texture_view( &self, - desc: crate::TextureViewDesc, + texture: super::Texture, + desc: crate::TextureViewDesc, ) -> super::TextureView { //TODO: actual reinterpretation super::TextureView { - inner: desc.texture.inner, - target_size: desc.texture.target_size, + inner: texture.inner, + target_size: texture.target_size, aspects: desc.format.aspects(), } } diff --git a/blade-graphics/src/lib.rs b/blade-graphics/src/lib.rs index afadaa2c..dc7b3720 100644 --- a/blade-graphics/src/lib.rs +++ b/blade-graphics/src/lib.rs @@ -388,9 +388,8 @@ pub struct TextureSubresources { } #[derive(Debug)] -pub struct TextureViewDesc<'a, T> { +pub struct TextureViewDesc<'a> { pub name: &'a str, - pub texture: T, pub format: TextureFormat, pub dimension: ViewDimension, pub subresources: &'a TextureSubresources, diff --git a/blade-graphics/src/metal/resource.rs b/blade-graphics/src/metal/resource.rs index c95abb3d..2b725b02 100644 --- a/blade-graphics/src/metal/resource.rs +++ b/blade-graphics/src/metal/resource.rs @@ -220,9 +220,10 @@ impl crate::traits::ResourceDevice for super::Context { fn create_texture_view( &self, - desc: crate::TextureViewDesc, + texture: super::Texture, + desc: crate::TextureViewDesc, ) -> super::TextureView { - let texture = desc.texture.as_ref(); + let texture = texture.as_ref(); let mtl_format = super::map_texture_format(desc.format); let mtl_type = map_view_dimension(desc.dimension); let mip_level_count = match desc.subresources.mip_level_count { diff --git a/blade-graphics/src/traits.rs b/blade-graphics/src/traits.rs index 41f3e511..365127d3 100644 --- a/blade-graphics/src/traits.rs +++ b/blade-graphics/src/traits.rs @@ -11,8 +11,11 @@ pub trait ResourceDevice { fn destroy_buffer(&self, buffer: Self::Buffer); fn create_texture(&self, desc: super::TextureDesc) -> Self::Texture; fn destroy_texture(&self, texture: Self::Texture); - fn create_texture_view(&self, desc: super::TextureViewDesc) - -> Self::TextureView; + fn create_texture_view( + &self, + texture: Self::Texture, + desc: super::TextureViewDesc, + ) -> Self::TextureView; fn destroy_texture_view(&self, view: Self::TextureView); fn create_sampler(&self, desc: super::SamplerDesc) -> Self::Sampler; fn destroy_sampler(&self, sampler: Self::Sampler); diff --git a/blade-graphics/src/vulkan/resource.rs b/blade-graphics/src/vulkan/resource.rs index 2b2808bf..d1dd7200 100644 --- a/blade-graphics/src/vulkan/resource.rs +++ b/blade-graphics/src/vulkan/resource.rs @@ -314,12 +314,13 @@ impl crate::traits::ResourceDevice for super::Context { fn create_texture_view( &self, - desc: crate::TextureViewDesc, + texture: super::Texture, + desc: crate::TextureViewDesc, ) -> super::TextureView { let aspects = desc.format.aspects(); let subresource_range = super::map_subresource_range(desc.subresources, aspects); let vk_info = vk::ImageViewCreateInfo { - image: desc.texture.raw, + image: texture.raw, view_type: map_view_dimension(desc.dimension), format: super::map_texture_format(desc.format), subresource_range: subresource_range, @@ -334,8 +335,8 @@ impl crate::traits::ResourceDevice for super::Context { super::TextureView { raw, target_size: [ - (desc.texture.target_size[0] >> desc.subresources.base_mip_level).max(1), - (desc.texture.target_size[1] >> desc.subresources.base_mip_level).max(1), + (texture.target_size[0] >> desc.subresources.base_mip_level).max(1), + (texture.target_size[1] >> desc.subresources.base_mip_level).max(1), ], aspects, } diff --git a/blade-render/src/render/dummy.rs b/blade-render/src/render/dummy.rs index b038079c..d3daa3e7 100644 --- a/blade-render/src/render/dummy.rs +++ b/blade-render/src/render/dummy.rs @@ -30,13 +30,15 @@ impl DummyResources { dimension: blade_graphics::TextureDimension::D2, usage: blade_graphics::TextureUsage::COPY | blade_graphics::TextureUsage::RESOURCE, }); - let white_view = gpu.create_texture_view(blade_graphics::TextureViewDesc { - name: "dummy/white", - texture: white_texture, - format: blade_graphics::TextureFormat::Rgba8Unorm, - dimension: blade_graphics::ViewDimension::D2, - subresources: &blade_graphics::TextureSubresources::default(), - }); + let white_view = gpu.create_texture_view( + white_texture, + blade_graphics::TextureViewDesc { + name: "dummy/white", + format: blade_graphics::TextureFormat::Rgba8Unorm, + dimension: blade_graphics::ViewDimension::D2, + subresources: &blade_graphics::TextureSubresources::default(), + }, + ); let black_texture = gpu.create_texture(blade_graphics::TextureDesc { name: "dummy/black", format: blade_graphics::TextureFormat::Rgba8Unorm, @@ -46,13 +48,15 @@ impl DummyResources { dimension: blade_graphics::TextureDimension::D2, usage: blade_graphics::TextureUsage::COPY | blade_graphics::TextureUsage::RESOURCE, }); - let black_view = gpu.create_texture_view(blade_graphics::TextureViewDesc { - name: "dummy/black", - texture: black_texture, - format: blade_graphics::TextureFormat::Rgba8Unorm, - dimension: blade_graphics::ViewDimension::D2, - subresources: &blade_graphics::TextureSubresources::default(), - }); + let black_view = gpu.create_texture_view( + black_texture, + blade_graphics::TextureViewDesc { + name: "dummy/black", + format: blade_graphics::TextureFormat::Rgba8Unorm, + dimension: blade_graphics::ViewDimension::D2, + subresources: &blade_graphics::TextureSubresources::default(), + }, + ); let red_texture = gpu.create_texture(blade_graphics::TextureDesc { name: "dummy/red", format: blade_graphics::TextureFormat::Rgba8Unorm, @@ -62,13 +66,15 @@ impl DummyResources { dimension: blade_graphics::TextureDimension::D2, usage: blade_graphics::TextureUsage::COPY | blade_graphics::TextureUsage::RESOURCE, }); - let red_view = gpu.create_texture_view(blade_graphics::TextureViewDesc { - name: "dummy/red", - texture: red_texture, - format: blade_graphics::TextureFormat::Rgba8Unorm, - dimension: blade_graphics::ViewDimension::D2, - subresources: &blade_graphics::TextureSubresources::default(), - }); + let red_view = gpu.create_texture_view( + red_texture, + blade_graphics::TextureViewDesc { + name: "dummy/red", + format: blade_graphics::TextureFormat::Rgba8Unorm, + dimension: blade_graphics::ViewDimension::D2, + subresources: &blade_graphics::TextureSubresources::default(), + }, + ); command_encoder.init_texture(white_texture); command_encoder.init_texture(black_texture); diff --git a/blade-render/src/render/env_map.rs b/blade-render/src/render/env_map.rs index 4e1256d3..58235e19 100644 --- a/blade-render/src/render/env_map.rs +++ b/blade-render/src/render/env_map.rs @@ -112,25 +112,29 @@ impl EnvironmentMap { mip_level_count, usage: blade_graphics::TextureUsage::RESOURCE | blade_graphics::TextureUsage::STORAGE, }); - self.weight_view = gpu.create_texture_view(blade_graphics::TextureViewDesc { - name: "env-weight", - texture: self.weight_texture, - format, - dimension: blade_graphics::ViewDimension::D2, - subresources: &Default::default(), - }); - for base_mip_level in 0..mip_level_count { - let view = gpu.create_texture_view(blade_graphics::TextureViewDesc { - name: &format!("env-weight-mip{}", base_mip_level), - texture: self.weight_texture, + self.weight_view = gpu.create_texture_view( + self.weight_texture, + blade_graphics::TextureViewDesc { + name: "env-weight", format, dimension: blade_graphics::ViewDimension::D2, - subresources: &blade_graphics::TextureSubresources { - base_mip_level, - mip_level_count: NonZeroU32::new(1), - ..Default::default() + subresources: &Default::default(), + }, + ); + for base_mip_level in 0..mip_level_count { + let view = gpu.create_texture_view( + self.weight_texture, + blade_graphics::TextureViewDesc { + name: &format!("env-weight-mip{}", base_mip_level), + format, + dimension: blade_graphics::ViewDimension::D2, + subresources: &blade_graphics::TextureSubresources { + base_mip_level, + mip_level_count: NonZeroU32::new(1), + ..Default::default() + }, }, - }); + ); self.weight_mips.push(view); } diff --git a/blade-render/src/render/mod.rs b/blade-render/src/render/mod.rs index 12460ee0..68c4544a 100644 --- a/blade-render/src/render/mod.rs +++ b/blade-render/src/render/mod.rs @@ -172,17 +172,19 @@ impl RenderTarget { let mut views = [blade_graphics::TextureView::default(); N]; for (i, view) in views.iter_mut().enumerate() { - *view = gpu.create_texture_view(blade_graphics::TextureViewDesc { - name: &format!("{name}{i}"), + *view = gpu.create_texture_view( texture, - format, - dimension: blade_graphics::ViewDimension::D2, - subresources: &blade_graphics::TextureSubresources { - base_array_layer: i as u32, - array_layer_count: NonZeroU32::new(1), - ..Default::default() + blade_graphics::TextureViewDesc { + name: &format!("{name}{i}"), + format, + dimension: blade_graphics::ViewDimension::D2, + subresources: &blade_graphics::TextureSubresources { + base_array_layer: i as u32, + array_layer_count: NonZeroU32::new(1), + ..Default::default() + }, }, - }); + ); } Self { texture, views } diff --git a/blade-render/src/texture/mod.rs b/blade-render/src/texture/mod.rs index 4dcb9019..5ef59c40 100644 --- a/blade-render/src/texture/mod.rs +++ b/blade-render/src/texture/mod.rs @@ -397,15 +397,15 @@ impl blade_asset::Baker for Baker { dimension: blade_graphics::TextureDimension::D2, usage: blade_graphics::TextureUsage::COPY | blade_graphics::TextureUsage::RESOURCE, }); - let view = self - .gpu_context - .create_texture_view(blade_graphics::TextureViewDesc { + let view = self.gpu_context.create_texture_view( + texture, + blade_graphics::TextureViewDesc { name, - texture, format: image.format.0, dimension: blade_graphics::ViewDimension::D2, subresources: &Default::default(), - }); + }, + ); self.pending_operations .lock() .unwrap() diff --git a/examples/bunnymark/main.rs b/examples/bunnymark/main.rs index 1e0740d4..50223fed 100644 --- a/examples/bunnymark/main.rs +++ b/examples/bunnymark/main.rs @@ -131,13 +131,15 @@ impl Example { mip_level_count: 1, usage: gpu::TextureUsage::RESOURCE | gpu::TextureUsage::COPY, }); - let view = context.create_texture_view(gpu::TextureViewDesc { - name: "view", + let view = context.create_texture_view( texture, - format: gpu::TextureFormat::Rgba8Unorm, - dimension: gpu::ViewDimension::D2, - subresources: &Default::default(), - }); + gpu::TextureViewDesc { + name: "view", + format: gpu::TextureFormat::Rgba8Unorm, + dimension: gpu::ViewDimension::D2, + subresources: &Default::default(), + }, + ); let upload_buffer = context.create_buffer(gpu::BufferDesc { name: "staging", diff --git a/examples/init/main.rs b/examples/init/main.rs index ee5fadc7..d01f52fa 100644 --- a/examples/init/main.rs +++ b/examples/init/main.rs @@ -30,13 +30,15 @@ impl EnvMapSampler { dimension: gpu::TextureDimension::D2, usage: gpu::TextureUsage::TARGET, }); - let accum_view = context.create_texture_view(gpu::TextureViewDesc { - texture: accum_texture, - name: "env-test", - format, - dimension: gpu::ViewDimension::D2, - subresources: &gpu::TextureSubresources::default(), - }); + let accum_view = context.create_texture_view( + accum_texture, + gpu::TextureViewDesc { + name: "env-test", + format, + dimension: gpu::ViewDimension::D2, + subresources: &gpu::TextureSubresources::default(), + }, + ); let layout = ::layout(); let init_pipeline = context.create_render_pipeline(gpu::RenderPipelineDesc { diff --git a/examples/mini/main.rs b/examples/mini/main.rs index 047a5c0a..d566b338 100644 --- a/examples/mini/main.rs +++ b/examples/mini/main.rs @@ -62,18 +62,20 @@ fn main() { }); let views = (0..mip_level_count) .map(|i| { - context.create_texture_view(gpu::TextureViewDesc { - name: &format!("mip-{}", i), + context.create_texture_view( texture, - format: gpu::TextureFormat::Rgba8Unorm, - dimension: gpu::ViewDimension::D2, - subresources: &gpu::TextureSubresources { - base_mip_level: i, - mip_level_count: NonZeroU32::new(1), - base_array_layer: 0, - array_layer_count: None, + gpu::TextureViewDesc { + name: &format!("mip-{}", i), + format: gpu::TextureFormat::Rgba8Unorm, + dimension: gpu::ViewDimension::D2, + subresources: &gpu::TextureSubresources { + base_mip_level: i, + mip_level_count: NonZeroU32::new(1), + base_array_layer: 0, + array_layer_count: None, + }, }, - }) + ) }) .collect::>(); diff --git a/examples/ray-query/main.rs b/examples/ray-query/main.rs index 4b74e595..76ad48a9 100644 --- a/examples/ray-query/main.rs +++ b/examples/ray-query/main.rs @@ -77,13 +77,15 @@ impl Example { mip_level_count: 1, usage: gpu::TextureUsage::RESOURCE | gpu::TextureUsage::STORAGE, }); - let target_view = context.create_texture_view(gpu::TextureViewDesc { - name: "main", - texture: target, - format: TARGET_FORMAT, - dimension: gpu::ViewDimension::D2, - subresources: &gpu::TextureSubresources::default(), - }); + let target_view = context.create_texture_view( + target, + gpu::TextureViewDesc { + name: "main", + format: TARGET_FORMAT, + dimension: gpu::ViewDimension::D2, + subresources: &gpu::TextureSubresources::default(), + }, + ); let surface_info = context.resize(gpu::SurfaceConfig { size: screen_size,