From 3a796c22446ff0248953a538b7cc4e080f979979 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 28 Jun 2021 23:43:40 -0400 Subject: [PATCH] hal/gles: fix buffer mapping --- wgpu-hal/src/gles/device.rs | 54 ++++++++++++++------------------ wgpu-hal/src/vulkan/instance.rs | 8 +++-- wgpu-hal/src/vulkan/mod.rs | 1 - wgpu/examples/boids/compute.wgsl | 6 ++-- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index ff51975b5..1dd1f1d00 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -306,16 +306,17 @@ impl crate::Device for super::Device { glow::ARRAY_BUFFER }; - let mut map_flags = 0; - if desc + let is_host_visible = desc .usage - .intersects(crate::BufferUse::MAP_READ | crate::BufferUse::MAP_WRITE) - { + .intersects(crate::BufferUse::MAP_READ | crate::BufferUse::MAP_WRITE); + let is_coherent = desc + .memory_flags + .contains(crate::MemoryFlag::PREFER_COHERENT); + let mut map_flags = 0; + + if is_host_visible { map_flags |= glow::MAP_PERSISTENT_BIT; - if desc - .memory_flags - .contains(crate::MemoryFlag::PREFER_COHERENT) - { + if is_coherent { map_flags |= glow::MAP_COHERENT_BIT; } } @@ -335,6 +336,11 @@ impl crate::Device for super::Device { gl.buffer_storage(target, raw_size, None, map_flags); gl.bind_buffer(target, None); + if !is_coherent && desc.usage.contains(crate::BufferUse::MAP_WRITE) { + map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT; + } + //TODO: do we need `glow::MAP_UNSYNCHRONIZED_BIT`? + if let Some(label) = desc.label { if gl.supports_debug() { gl.object_label(glow::BUFFER, raw, Some(label)); @@ -361,17 +367,13 @@ impl crate::Device for super::Device { let gl = &self.shared.context; let is_coherent = buffer.map_flags & glow::MAP_COHERENT_BIT != 0; - let mut flags = buffer.map_flags | glow::MAP_UNSYNCHRONIZED_BIT; - if !is_coherent { - flags |= glow::MAP_FLUSH_EXPLICIT_BIT; - } gl.bind_buffer(buffer.target, Some(buffer.raw)); let ptr = gl.map_buffer_range( buffer.target, range.start as i32, (range.end - range.start) as i32, - flags, + buffer.map_flags, ); gl.bind_buffer(buffer.target, None); @@ -401,19 +403,8 @@ impl crate::Device for super::Device { ); } } - unsafe fn invalidate_mapped_ranges(&self, buffer: &super::Buffer, ranges: I) - where - I: Iterator, - { - let gl = &self.shared.context; - gl.bind_buffer(buffer.target, Some(buffer.raw)); - for range in ranges { - gl.invalidate_buffer_sub_data( - buffer.target, - range.start as i32, - (range.end - range.start) as i32, - ); - } + unsafe fn invalidate_mapped_ranges(&self, _buffer: &super::Buffer, _ranges: I) { + //TODO: do we need to do anything? } unsafe fn create_texture( @@ -592,11 +583,6 @@ impl crate::Device for super::Device { let gl = &self.shared.context; let raw = gl.create_sampler().unwrap(); - if let Some(label) = desc.label { - if gl.supports_debug() { - gl.object_label(glow::SAMPLER, raw, Some(label)); - } - } let (min, mag) = conv::map_filter_modes(desc.min_filter, desc.mag_filter, desc.mipmap_filter); @@ -652,6 +638,12 @@ impl crate::Device for super::Device { ); } + if let Some(label) = desc.label { + if gl.supports_debug() { + gl.object_label(glow::SAMPLER, raw, Some(label)); + } + } + Ok(super::Sampler { raw }) } unsafe fn destroy_sampler(&self, sampler: super::Sampler) { diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index 584435d65..1f046ec02 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -287,10 +287,14 @@ impl super::Instance { } } -impl Drop for super::Instance { +impl Drop for super::InstanceShared { fn drop(&mut self) { unsafe { - self.shared.raw.destroy_instance(None); + if let Some(du) = self.debug_utils.take() { + du.extension + .destroy_debug_utils_messenger(du.messenger, None); + } + self.raw.destroy_instance(None); } } } diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index dd27836c7..133bb21f9 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -48,7 +48,6 @@ impl crate::Api for Api { struct DebugUtils { extension: ext::DebugUtils, - #[allow(dead_code)] messenger: vk::DebugUtilsMessengerEXT, } diff --git a/wgpu/examples/boids/compute.wgsl b/wgpu/examples/boids/compute.wgsl index bddb260a2..e0c6e9e71 100644 --- a/wgpu/examples/boids/compute.wgsl +++ b/wgpu/examples/boids/compute.wgsl @@ -41,8 +41,6 @@ fn main([[builtin(global_invocation_id)]] global_invocation_id: vec3) { var cMassCount : i32 = 0; var cVelCount : i32 = 0; - var pos : vec2; - var vel : vec2; var i : u32 = 0u; loop { if (i >= total) { @@ -52,8 +50,8 @@ fn main([[builtin(global_invocation_id)]] global_invocation_id: vec3) { continue; } - pos = particlesSrc.particles[i].pos; - vel = particlesSrc.particles[i].vel; + let pos = particlesSrc.particles[i].pos; + let vel = particlesSrc.particles[i].vel; if (distance(pos, vPos) < params.rule1Distance) { cMass = cMass + pos;