From c7356e124e129391d2f2dea91234c3a6e3f04cb9 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 25 Jun 2021 10:40:49 -0400 Subject: [PATCH] hal/gles: storage texture bindings --- wgpu-hal/src/gles/adapter.rs | 2 ++ wgpu-hal/src/gles/command.rs | 12 ++++++--- wgpu-hal/src/gles/conv.rs | 8 ++++++ wgpu-hal/src/gles/device.rs | 49 +++++++++++++++++++++++++++++++++--- wgpu-hal/src/gles/egl.rs | 2 ++ wgpu-hal/src/gles/mod.rs | 21 ++++++++++++++-- wgpu-hal/src/gles/queue.rs | 15 ++++++++--- 7 files changed, 97 insertions(+), 12 deletions(-) diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 6ab35ad74..0f5812e34 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -190,6 +190,8 @@ impl super::Adapter { | wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES | wgt::DownlevelFlags::COMPARISON_SAMPLERS; downlevel_flags.set(wgt::DownlevelFlags::COMPUTE_SHADERS, ver >= (3, 1)); + //Note: storage textures aren't supported atm, only buffers. See + // https://github.com/grovesNL/glow/issues/174 downlevel_flags.set( wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE, ver >= (3, 1), diff --git a/wgpu-hal/src/gles/command.rs b/wgpu-hal/src/gles/command.rs index d23845e38..a93263332 100644 --- a/wgpu-hal/src/gles/command.rs +++ b/wgpu-hal/src/gles/command.rs @@ -495,6 +495,10 @@ impl crate::CommandEncoder for super::CommandEncoder { size, }); } + super::RawBinding::Sampler(sampler) => { + dirty_samplers |= 1 << slot; + self.state.samplers[slot as usize] = Some(sampler); + } super::RawBinding::Texture { raw, target } => { dirty_textures |= 1 << slot; self.state.texture_slots[slot as usize].tex_target = target; @@ -504,9 +508,11 @@ impl crate::CommandEncoder for super::CommandEncoder { target, }); } - super::RawBinding::Sampler(sampler) => { - dirty_samplers |= 1 << slot; - self.state.samplers[slot as usize] = Some(sampler); + super::RawBinding::Image(ref binding) => { + self.cmd_buffer.commands.push(C::BindImage { + slot, + binding: binding.clone(), + }); } } } diff --git a/wgpu-hal/src/gles/conv.rs b/wgpu-hal/src/gles/conv.rs index cc75b68d5..0fe545d85 100644 --- a/wgpu-hal/src/gles/conv.rs +++ b/wgpu-hal/src/gles/conv.rs @@ -313,3 +313,11 @@ pub(super) fn map_blend(blend: &wgt::BlendState) -> super::BlendDesc { alpha: map_blend_component(&blend.alpha), } } + +pub(super) fn map_storage_access(access: wgt::StorageTextureAccess) -> u32 { + match access { + wgt::StorageTextureAccess::ReadOnly => glow::READ_ONLY, + wgt::StorageTextureAccess::WriteOnly => glow::WRITE_ONLY, + wgt::StorageTextureAccess::ReadWrite => glow::READ_WRITE, + } +} diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index eae77c887..c024b44f9 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -455,6 +455,12 @@ impl crate::Device for super::Device { Ok(super::Texture { inner, + mip_level_count: desc.mip_level_count, + array_layer_count: if desc.dimension == wgt::TextureDimension::D2 { + desc.size.depth_or_array_layers + } else { + 1 + }, format: desc.format, format_desc, }) @@ -476,6 +482,14 @@ impl crate::Device for super::Device { texture: &super::Texture, desc: &crate::TextureViewDescriptor, ) -> Result { + let end_array_layer = match desc.range.array_layer_count { + Some(count) => desc.range.base_array_layer + count.get(), + None => texture.array_layer_count, + }; + let end_mip_level = match desc.range.mip_level_count { + Some(count) => desc.range.base_mip_level + count.get(), + None => texture.mip_level_count, + }; Ok(super::TextureView { inner: match texture.inner { super::TextureInner::Renderbuffer { raw } => { @@ -489,8 +503,8 @@ impl crate::Device for super::Device { sample_type: texture.format.describe().sample_type, aspects: crate::FormatAspect::from(texture.format) & crate::FormatAspect::from(desc.range.aspect), - base_mip_level: desc.range.base_mip_level, - base_array_layer: desc.range.base_array_layer, + mip_levels: desc.range.base_mip_level..end_mip_level, + array_layers: desc.range.base_array_layer..end_array_layer, }) } unsafe fn destroy_texture_view(&self, _view: super::TextureView) {} @@ -610,8 +624,9 @@ impl crate::Device for super::Device { let sampler = desc.samplers[entry.resource_index as usize]; super::RawBinding::Sampler(sampler.raw) } - wgt::BindingType::Texture { .. } | wgt::BindingType::StorageTexture { .. } => { - match desc.textures[entry.resource_index as usize].view.inner { + wgt::BindingType::Texture { .. } => { + let view = desc.textures[entry.resource_index as usize].view; + match view.inner { super::TextureInner::Renderbuffer { .. } => { panic!("Unable to use a renderbuffer in a group") } @@ -620,6 +635,32 @@ impl crate::Device for super::Device { } } } + wgt::BindingType::StorageTexture { + access, + format, + view_dimension, + } => { + let view = desc.textures[entry.resource_index as usize].view; + let format_desc = self.shared.describe_texture_format(format); + match view.inner { + super::TextureInner::Renderbuffer { .. } => { + panic!("Unable to use a renderbuffer in a group") + } + super::TextureInner::Texture { raw, .. } => { + super::RawBinding::Image(super::ImageBinding { + raw, + mip_level: view.mip_levels.start, + array_layer: match view_dimension { + wgt::TextureViewDimension::D2Array + | wgt::TextureViewDimension::CubeArray => None, + _ => Some(view.array_layers.start), + }, + access: conv::map_storage_access(access), + format: format_desc.internal, + }) + } + } + } }; contents.push(binding); } diff --git a/wgpu-hal/src/gles/egl.rs b/wgpu-hal/src/gles/egl.rs index 35aad685d..5eabfb8bf 100644 --- a/wgpu-hal/src/gles/egl.rs +++ b/wgpu-hal/src/gles/egl.rs @@ -683,6 +683,8 @@ impl crate::Surface for Surface { inner: super::TextureInner::Renderbuffer { raw: sc.renderbuffer, }, + array_layer_count: 1, + mip_level_count: 1, format: sc.format, format_desc: sc.format_desc.clone(), }; diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index 88fc43947..a042dc20a 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -224,6 +224,8 @@ impl TextureInner { #[derive(Debug)] pub struct Texture { inner: TextureInner, + mip_level_count: u32, + array_layer_count: u32, format: wgt::TextureFormat, format_desc: TextureFormatDesc, } @@ -233,8 +235,8 @@ pub struct TextureView { inner: TextureInner, sample_type: wgt::TextureSampleType, aspects: crate::FormatAspect, - base_mip_level: u32, - base_array_layer: u32, + mip_levels: Range, + array_layers: Range, } #[derive(Debug)] @@ -285,7 +287,9 @@ enum RawBinding { Texture { raw: glow::Texture, target: BindTarget, + //TODO: mip levels, array layers }, + Image(ImageBinding), Sampler(glow::Sampler), } @@ -320,6 +324,15 @@ struct BufferBinding { offset: wgt::BufferAddress, } +#[derive(Clone, Debug)] +struct ImageBinding { + raw: glow::Texture, + mip_level: u32, + array_layer: Option, + access: u32, + format: u32, +} + #[derive(Clone, Debug, Default)] struct VertexBufferDesc { step: wgt::InputStepMode, @@ -607,6 +620,10 @@ enum Command { texture: glow::Texture, target: BindTarget, }, + BindImage { + slot: u32, + binding: ImageBinding, + }, InsertDebugMarker(Range), PushDebugGroup(Range), PopDebugGroup, diff --git a/wgpu-hal/src/gles/queue.rs b/wgpu-hal/src/gles/queue.rs index 58d13bd41..362c22109 100644 --- a/wgpu-hal/src/gles/queue.rs +++ b/wgpu-hal/src/gles/queue.rs @@ -346,8 +346,8 @@ impl super::Queue { glow::DRAW_FRAMEBUFFER, attachment, Some(raw), - view.base_mip_level as i32, - view.base_array_layer as i32, + view.mip_levels.start as i32, + view.array_layers.start as i32, ); } else { gl.framebuffer_texture_2d( @@ -355,7 +355,7 @@ impl super::Queue { attachment, target, Some(raw), - view.base_mip_level as i32, + view.mip_levels.start as i32, ); } } @@ -602,6 +602,15 @@ impl super::Queue { gl.active_texture(glow::TEXTURE0 + slot); gl.bind_texture(target, Some(texture)); } + C::BindImage { + slot: _, + binding: _, + } => { + //TODO: https://github.com/grovesNL/glow/issues/174 + //gl.bind_image_texture(slot, Some(binding.raw), binding.mip_level as i32, + // binding.array_layer.is_none(), binding.array_layer.unwrap_or_default(), + // binding.access, binding.format); + } C::InsertDebugMarker(ref range) => { let marker = extract_marker(data_bytes, range); gl.debug_message_insert(