From 7afa4178bdaac44cbff3a1520c0145614b22f16d Mon Sep 17 00:00:00 2001 From: Xiaochun Tong Date: Tue, 10 Oct 2023 23:28:13 -0400 Subject: [PATCH] coalesce bindless modification --- luisa_compute/src/resource.rs | 129 ++++++++++++++++++--------------- luisa_compute/src/runtime.rs | 2 +- luisa_compute_sys/LuisaCompute | 2 +- 3 files changed, 72 insertions(+), 61 deletions(-) diff --git a/luisa_compute/src/resource.rs b/luisa_compute/src/resource.rs index a537721..d8155a9 100644 --- a/luisa_compute/src/resource.rs +++ b/luisa_compute/src/resource.rs @@ -1,4 +1,5 @@ use std::cell::{Cell, RefCell}; +use std::collections::HashMap; use std::fmt; use std::ops::RangeBounds; use std::process::abort; @@ -601,7 +602,7 @@ impl BufferHeapVar { pub struct BindlessArray { pub(crate) device: Device, pub(crate) handle: Arc, - pub(crate) modifications: RefCell>, + pub(crate) modifications: RefCell>, pub(crate) slots: RefCell>, pub(crate) lock: Arc, } @@ -657,16 +658,18 @@ impl BindlessArray { self.lock(); self.modifications .borrow_mut() - .push(api::BindlessArrayUpdateModification { + .entry(index) + .or_insert(api::BindlessArrayUpdateModification { slot: index, - buffer: api::BindlessArrayUpdateBuffer { - op: api::BindlessArrayUpdateOperation::Emplace, - handle: bufferview.handle(), - offset: bufferview.offset, - }, - tex2d: api::BindlessArrayUpdateTexture::default(), - tex3d: api::BindlessArrayUpdateTexture::default(), - }); + buffer: Default::default(), + tex2d: Default::default(), + tex3d: Default::default(), + }) + .buffer = api::BindlessArrayUpdateBuffer { + op: api::BindlessArrayUpdateOperation::Emplace, + handle: bufferview.handle(), + offset: bufferview.offset, + }; let mut slots = self.slots.borrow_mut(); slots[index].buffer = Some(bufferview.buffer.handle.clone()); self.unlock(); @@ -680,16 +683,18 @@ impl BindlessArray { self.lock(); self.modifications .borrow_mut() - .push(api::BindlessArrayUpdateModification { + .entry(index) + .or_insert(api::BindlessArrayUpdateModification { slot: index, - buffer: api::BindlessArrayUpdateBuffer::default(), - tex2d: api::BindlessArrayUpdateTexture { - op: api::BindlessArrayUpdateOperation::Emplace, - handle: texture.handle(), - sampler, - }, - tex3d: api::BindlessArrayUpdateTexture::default(), - }); + buffer: Default::default(), + tex2d: Default::default(), + tex3d: Default::default(), + }) + .tex2d = api::BindlessArrayUpdateTexture { + op: api::BindlessArrayUpdateOperation::Emplace, + handle: texture.handle(), + sampler, + }; let mut slots = self.slots.borrow_mut(); slots[index].tex2d = Some(texture.handle.clone()); self.unlock(); @@ -703,16 +708,18 @@ impl BindlessArray { self.lock(); self.modifications .borrow_mut() - .push(api::BindlessArrayUpdateModification { + .entry(index) + .or_insert(api::BindlessArrayUpdateModification { slot: index, - buffer: api::BindlessArrayUpdateBuffer::default(), - tex2d: api::BindlessArrayUpdateTexture::default(), - tex3d: api::BindlessArrayUpdateTexture { - op: api::BindlessArrayUpdateOperation::Emplace, - handle: texture.handle(), - sampler, - }, - }); + buffer: Default::default(), + tex2d: Default::default(), + tex3d: Default::default(), + }) + .tex3d = api::BindlessArrayUpdateTexture { + op: api::BindlessArrayUpdateOperation::Emplace, + handle: texture.handle(), + sampler, + }; let mut slots = self.slots.borrow_mut(); slots[index].tex3d = Some(texture.handle.clone()); self.unlock(); @@ -721,16 +728,18 @@ impl BindlessArray { self.lock(); self.modifications .borrow_mut() - .push(api::BindlessArrayUpdateModification { + .entry(index) + .or_insert(api::BindlessArrayUpdateModification { slot: index, - buffer: api::BindlessArrayUpdateBuffer { - op: api::BindlessArrayUpdateOperation::Remove, - handle: api::Buffer(INVALID_RESOURCE_HANDLE), - offset: 0, - }, - tex2d: api::BindlessArrayUpdateTexture::default(), - tex3d: api::BindlessArrayUpdateTexture::default(), - }); + buffer: Default::default(), + tex2d: Default::default(), + tex3d: Default::default(), + }) + .buffer = api::BindlessArrayUpdateBuffer { + op: api::BindlessArrayUpdateOperation::Remove, + handle: api::Buffer(INVALID_RESOURCE_HANDLE), + offset: 0, + }; let mut slots = self.slots.borrow_mut(); slots[index].buffer = None; self.unlock(); @@ -739,16 +748,18 @@ impl BindlessArray { self.lock(); self.modifications .borrow_mut() - .push(api::BindlessArrayUpdateModification { + .entry(index) + .or_insert(api::BindlessArrayUpdateModification { slot: index, - buffer: api::BindlessArrayUpdateBuffer::default(), - tex2d: api::BindlessArrayUpdateTexture { - op: api::BindlessArrayUpdateOperation::Remove, - handle: api::Texture(INVALID_RESOURCE_HANDLE), - sampler: Sampler::default(), - }, - tex3d: api::BindlessArrayUpdateTexture::default(), - }); + buffer: Default::default(), + tex2d: Default::default(), + tex3d: Default::default(), + }) + .tex2d = api::BindlessArrayUpdateTexture { + op: api::BindlessArrayUpdateOperation::Remove, + handle: api::Texture(INVALID_RESOURCE_HANDLE), + sampler: Sampler::default(), + }; let mut slots = self.slots.borrow_mut(); slots[index].tex2d = None; self.unlock(); @@ -757,16 +768,18 @@ impl BindlessArray { self.lock(); self.modifications .borrow_mut() - .push(api::BindlessArrayUpdateModification { + .entry(index) + .or_insert(api::BindlessArrayUpdateModification { slot: index, - buffer: api::BindlessArrayUpdateBuffer::default(), - tex2d: api::BindlessArrayUpdateTexture::default(), - tex3d: api::BindlessArrayUpdateTexture { - op: api::BindlessArrayUpdateOperation::Remove, - handle: api::Texture(INVALID_RESOURCE_HANDLE), - sampler: Sampler::default(), - }, - }); + buffer: Default::default(), + tex2d: Default::default(), + tex3d: Default::default(), + }) + .tex3d = api::BindlessArrayUpdateTexture { + op: api::BindlessArrayUpdateOperation::Remove, + handle: api::Texture(INVALID_RESOURCE_HANDLE), + sampler: Sampler::default(), + }; let mut slots = self.slots.borrow_mut(); slots[index].tex3d = None; self.unlock(); @@ -824,10 +837,8 @@ impl BindlessArray { // What lifetime should this be? self.lock(); let mut rt = ResourceTracker::new(); - let modifications = Arc::new(std::mem::replace( - &mut *self.modifications.borrow_mut(), - Vec::new(), - )); + let mut modifications = self.modifications.borrow_mut(); + let modifications = Arc::new(modifications.drain().map(|(k, v)| v).collect::>()); rt.add(modifications.clone()); let lock = self.lock.clone(); Command { diff --git a/luisa_compute/src/runtime.rs b/luisa_compute/src/runtime.rs index 2373045..071ae47 100644 --- a/luisa_compute/src/runtime.rs +++ b/luisa_compute/src/runtime.rs @@ -281,7 +281,7 @@ impl Device { handle: api::BindlessArray(array.handle), native_handle: array.native_handle, }), - modifications: RefCell::new(Vec::new()), + modifications: RefCell::new(HashMap::new()), slots: RefCell::new(vec![ BindlessArraySlot { buffer: None, diff --git a/luisa_compute_sys/LuisaCompute b/luisa_compute_sys/LuisaCompute index 99e6ba2..4e728a3 160000 --- a/luisa_compute_sys/LuisaCompute +++ b/luisa_compute_sys/LuisaCompute @@ -1 +1 @@ -Subproject commit 99e6ba2888612b79b631ad9070bb653e034d2114 +Subproject commit 4e728a3cf61e72211fb0ea24b95e5dbf7ae35949