Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ResourceDevice actually generic over the backend #150

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions blade-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
10 changes: 7 additions & 3 deletions blade-graphics/src/gles/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,15 @@ impl crate::traits::ResourceDevice for super::Context {
}
}

fn create_texture_view(&self, desc: crate::TextureViewDesc) -> super::TextureView {
fn create_texture_view(
&self,
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(),
}
}
Expand Down
1 change: 0 additions & 1 deletion blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ pub struct TextureSubresources {
#[derive(Debug)]
pub struct TextureViewDesc<'a> {
pub name: &'a str,
pub texture: Texture,
pub format: TextureFormat,
pub dimension: ViewDimension,
pub subresources: &'a TextureSubresources,
Expand Down
8 changes: 6 additions & 2 deletions blade-graphics/src/metal/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,12 @@ impl crate::traits::ResourceDevice for super::Context {
}
}

fn create_texture_view(&self, desc: crate::TextureViewDesc) -> super::TextureView {
let texture = desc.texture.as_ref();
fn create_texture_view(
&self,
texture: super::Texture,
desc: crate::TextureViewDesc,
) -> super::TextureView {
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 {
Expand Down
6 changes: 5 additions & 1 deletion blade-graphics/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +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);
Expand Down
12 changes: 8 additions & 4 deletions blade-graphics/src/vulkan/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,15 @@ impl crate::traits::ResourceDevice for super::Context {
self.free_memory(texture.memory_handle);
}

fn create_texture_view(&self, desc: crate::TextureViewDesc) -> super::TextureView {
fn create_texture_view(
&self,
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,
Expand All @@ -331,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,
}
Expand Down
48 changes: 27 additions & 21 deletions blade-render/src/render/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down
36 changes: 20 additions & 16 deletions blade-render/src/render/env_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
20 changes: 11 additions & 9 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,19 @@ impl<const N: usize> RenderTarget<N> {

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 }
Expand Down
10 changes: 5 additions & 5 deletions blade-render/src/texture/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
fmt, io, mem, ptr, slice, str,

Check warning on line 2 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused imports: `io`, `mem`, `slice`

Check warning on line 2 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused imports: `io`, `mem`, `slice`

Check warning on line 2 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused imports: `io`, `mem`, `slice`
sync::{Arc, Mutex},
};

Expand Down Expand Up @@ -107,13 +107,13 @@
type Output = Texture;
fn cook(
&self,
source: &[u8],

Check warning on line 110 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `source`
extension: &str,
meta: Meta,
cooker: Arc<blade_asset::Cooker<Self>>,
exe_context: &choir::ExecutionContext,
) {
use blade_graphics::TextureFormat as Tf;

Check warning on line 116 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused import: `blade_graphics::TextureFormat as Tf`

Check warning on line 116 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused import: `blade_graphics::TextureFormat as Tf`

Check warning on line 116 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused import: `blade_graphics::TextureFormat as Tf`

type LdrTexel = [u8; 4];
type HdrTexel = [f32; 3];
Expand All @@ -127,7 +127,7 @@
data: PlainData,
}

let src: PlainImage = match extension {

Check warning on line 130 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unused variable: `src`

Check warning on line 130 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (Linux, ubuntu-latest, x86_64-unknown-linux-gnu)

unused variable: `src`

Check warning on line 130 in blade-render/src/texture/mod.rs

View workflow job for this annotation

GitHub Actions / build (Windows, windows-latest, x86_64-pc-windows-msvc)

unused variable: `src`
#[cfg(feature = "asset")]
"png" => {
profiling::scope!("decode png");
Expand Down Expand Up @@ -397,15 +397,15 @@
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()
Expand Down
14 changes: 8 additions & 6 deletions examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 9 additions & 7 deletions examples/init/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <EnvSampleData as gpu::ShaderData>::layout();
let init_pipeline = context.create_render_pipeline(gpu::RenderPipelineDesc {
Expand Down
22 changes: 12 additions & 10 deletions examples/mini/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

Expand Down
16 changes: 9 additions & 7 deletions examples/ray-query/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading