diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index a42d30b4de..3468edb95a 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -113,7 +113,7 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( } else { extent = Some(view.extent); } - let query = tracker.query(view.source_id.0); + let query = tracker.query(view.texture_id.0); let (_, layout) = conv::map_texture_state( query.usage, hal::format::Aspects::DEPTH | hal::format::Aspects::STENCIL, @@ -137,7 +137,7 @@ pub extern "C" fn wgpu_command_buffer_begin_render_pass( } else { extent = Some(view.extent); } - let query = tracker.query(view.source_id.0); + let query = tracker.query(view.texture_id.0); let (_, layout) = conv::map_texture_state(query.usage, hal::format::Aspects::COLOR); hal::pass::Attachment { format: Some(conv::map_texture_format(view.format)), diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index 88aa3310b1..13c2a0317a 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -59,15 +59,12 @@ pub extern "C" fn wgpu_render_pass_end_pass( .consume(pass.texture_tracker) .map(|(id, transit)| { let t = texture_guard.get(id); + let aspects = t.full_range.aspects; hal::memory::Barrier::Image { - states: conv::map_texture_state(transit.start, t.aspects) .. - conv::map_texture_state(transit.end, t.aspects), + states: conv::map_texture_state(transit.start, aspects) .. + conv::map_texture_state(transit.end, aspects), target: &t.raw, - range: hal::image::SubresourceRange { //TODO! - aspects: t.aspects, - levels: 0 .. 1, - layers: 0 .. 1, - }, + range: t.full_range.clone(), //TODO? } }); diff --git a/wgpu-native/src/conv.rs b/wgpu-native/src/conv.rs index f22249adcd..407f2b391b 100644 --- a/wgpu-native/src/conv.rs +++ b/wgpu-native/src/conv.rs @@ -277,11 +277,11 @@ fn checked_u32_as_u16(value: u32) -> u16 { } pub fn map_texture_dimension_size( - dimension: resource::TextureDimension, size: Extent3d + dimension: resource::TextureDimension, + Extent3d { width, height, depth }: Extent3d, ) -> hal::image::Kind { use hal::image::Kind as H; use resource::TextureDimension::*; - let Extent3d { width, height, depth } = size; match dimension { D1 => { assert_eq!(height, 1); @@ -292,6 +292,40 @@ pub fn map_texture_dimension_size( } } +pub fn map_texture_view_dimension( + dimension: resource::TextureViewDimension, +) -> hal::image::ViewKind { + use hal::image::ViewKind as H; + use resource::TextureViewDimension::*; + match dimension { + D1 => H::D1, + D2 => H::D2, + D2Array => H::D2Array, + Cube => H::Cube, + CubeArray => H::CubeArray, + D3 => H::D3, + } +} + +pub fn map_texture_aspect_flags( + aspect: resource::TextureAspectFlags +) -> hal::format::Aspects { + use resource::TextureAspectFlags as Taf; + use hal::format::Aspects; + + let mut flags = Aspects::empty(); + if aspect.contains(Taf::COLOR) { + flags |= Aspects::COLOR; + } + if aspect.contains(Taf::DEPTH) { + flags |= Aspects::DEPTH; + } + if aspect.contains(Taf::STENCIL) { + flags |= Aspects::STENCIL; + } + flags +} + pub fn map_buffer_state( usage: resource::BufferUsageFlags, ) -> hal::buffer::State { diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index b0ef75f7ed..cceae15137 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -2,8 +2,10 @@ use {back, binding_model, command, conv, pipeline, resource}; use registry::{HUB, Items, Registry}; use track::{BufferTracker, TextureTracker}; use { + Stored, AttachmentStateId, BindGroupLayoutId, BlendStateId, CommandBufferId, DepthStencilStateId, - DeviceId, PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId, TextureId, + DeviceId, PipelineLayoutId, QueueId, RenderPipelineId, ShaderModuleId, + TextureId, TextureViewId, }; use hal::command::RawCommandBuffer; @@ -68,6 +70,7 @@ pub(crate) struct ShaderModule { pub raw: B::ShaderModule, } + #[no_mangle] pub extern "C" fn wgpu_device_create_texture( device_id: DeviceId, @@ -109,11 +112,20 @@ pub extern "C" fn wgpu_device_create_texture( .bind_image_memory(&image_memory, 0, image_unbound) .unwrap(); + let full_range = hal::image::SubresourceRange { + aspects, + levels: 0 .. 1, //TODO: mips + layers: 0 .. 1, //TODO + }; + let id = HUB.textures .lock() .register(resource::Texture { raw: bound_image, - aspects, + device_id: Stored(device_id), + kind, + format: desc.format, + full_range, }); let query = device.texture_tracker .lock() @@ -124,6 +136,80 @@ pub extern "C" fn wgpu_device_create_texture( id } +#[no_mangle] +pub extern "C" fn wgpu_texture_create_texture_view( + texture_id: TextureId, + desc: &resource::TextureViewDescriptor, +) -> TextureViewId { + let texture_guard = HUB.textures.lock(); + let texture = texture_guard.get(texture_id); + + let raw = HUB.devices + .lock() + .get(texture.device_id.0) + .raw + .create_image_view( + &texture.raw, + conv::map_texture_view_dimension(desc.dimension), + conv::map_texture_format(desc.format), + hal::format::Swizzle::NO, + hal::image::SubresourceRange { + aspects: conv::map_texture_aspect_flags(desc.aspect), + levels: desc.base_mip_level as u8 .. (desc.base_mip_level + desc.level_count) as u8, + layers: desc.base_array_layer as u16 .. (desc.base_array_layer + desc.array_count) as u16, + }, + ) + .unwrap(); + + HUB.texture_views + .lock() + .register(resource::TextureView { + raw, + texture_id: Stored(texture_id), + format: texture.format, + extent: texture.kind.extent(), + samples: texture.kind.num_samples(), + }) +} + +#[no_mangle] +pub extern "C" fn wgpu_texture_create_default_texture_view( + texture_id: TextureId, +) -> TextureViewId { + let texture_guard = HUB.textures.lock(); + let texture = texture_guard.get(texture_id); + + let view_kind = match texture.kind { + hal::image::Kind::D1(..) => hal::image::ViewKind::D1, + hal::image::Kind::D2(..) => hal::image::ViewKind::D2, //TODO: array + hal::image::Kind::D3(..) => hal::image::ViewKind::D3, + }; + + let raw = HUB.devices + .lock() + .get(texture.device_id.0) + .raw + .create_image_view( + &texture.raw, + view_kind, + conv::map_texture_format(texture.format), + hal::format::Swizzle::NO, + texture.full_range.clone(), + ) + .unwrap(); + + HUB.texture_views + .lock() + .register(resource::TextureView { + raw, + texture_id: Stored(texture_id), + format: texture.format, + extent: texture.kind.extent(), + samples: texture.kind.num_samples(), + }) +} + + #[no_mangle] pub extern "C" fn wgpu_device_create_bind_group_layout( device_id: DeviceId, diff --git a/wgpu-native/src/resource.rs b/wgpu-native/src/resource.rs index 9139e6b32d..f914da45ae 100644 --- a/wgpu-native/src/resource.rs +++ b/wgpu-native/src/resource.rs @@ -1,4 +1,7 @@ -use {Extent3d, Stored, TextureId}; +use { + Extent3d, Stored, + DeviceId, TextureId, +}; use hal; @@ -75,17 +78,53 @@ pub struct TextureDescriptor { pub(crate) struct Texture { pub raw: B::Image, - pub aspects: hal::format::Aspects, + pub device_id: Stored, + pub kind: hal::image::Kind, + pub format: TextureFormat, + pub full_range: hal::image::SubresourceRange, +} + + +bitflags! { + #[repr(transparent)] + pub struct TextureAspectFlags: u32 { + const COLOR = 1; + const DEPTH = 2; + const STENCIL = 4; + } +} + +#[repr(C)] +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum TextureViewDimension { + D1, + D2, + D2Array, + Cube, + CubeArray, + D3, +} + +#[repr(C)] +pub struct TextureViewDescriptor { + pub format: TextureFormat, + pub dimension: TextureViewDimension, + pub aspect: TextureAspectFlags, + pub base_mip_level: u32, + pub level_count: u32, + pub base_array_layer: u32, + pub array_count: u32, } pub(crate) struct TextureView { pub raw: B::ImageView, - pub source_id: Stored, + pub texture_id: Stored, pub format: TextureFormat, pub extent: hal::image::Extent, pub samples: hal::image::NumSamples, } + #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] pub enum AddressMode {