Skip to content

Commit 758e50b

Browse files
committed
Finalize sync.
1 parent d00c97c commit 758e50b

5 files changed

Lines changed: 24 additions & 36 deletions

File tree

crates/processing_ffi/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,8 @@ pub unsafe extern "C" fn processing_compute_set_float(
19651965
});
19661966
}
19671967

1968-
/// Set a buffer property on a compute shader.
1968+
/// # Safety
1969+
/// `name` must be a valid null-terminated C string.
19691970
#[unsafe(no_mangle)]
19701971
pub unsafe extern "C" fn processing_compute_set_buffer(
19711972
compute_id: u64,

crates/processing_render/src/compute.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{BTreeSet, HashMap};
1+
use std::collections::BTreeSet;
22

33
use bevy::asset::RenderAssetUsages;
44
use bevy::reflect::PartialReflect;
@@ -98,10 +98,7 @@ pub fn write_buffer_cpu(
9898
let mut asset = buffers
9999
.get_mut(&handle)
100100
.ok_or(ProcessingError::BufferNotFound)?;
101-
let dst = asset
102-
.data
103-
.as_mut()
104-
.ok_or(ProcessingError::BufferNotFound)?;
101+
let dst = asset.data.as_mut().ok_or(ProcessingError::BufferNotFound)?;
105102
let start = offset as usize;
106103
let end = start + data.len();
107104
dst[start..end].copy_from_slice(&data);
@@ -144,7 +141,7 @@ pub fn read_buffer_gpu(
144141

145142
pub fn invalidate_rw_buffers(mut buffers: Query<&mut Buffer>) {
146143
for mut buf in &mut buffers {
147-
if buf.bound_rw {
144+
if buf.bound_rw && buf.synced {
148145
buf.synced = false;
149146
}
150147
}
@@ -161,7 +158,6 @@ pub struct Compute {
161158
pub entry_point: String,
162159
pub pipeline_id: CachedComputePipelineId,
163160
pub bind_group_layout_descriptors: Vec<(u32, BindGroupLayoutDescriptor)>,
164-
pub rw_buffers: HashMap<String, Entity>,
165161
}
166162

167163
fn queue_pipeline(
@@ -225,8 +221,7 @@ pub fn create_compute(app: &mut App, shader_entity: Entity) -> Result<Entity> {
225221
.collect();
226222

227223
let max_group = groups.iter().last().copied().map_or(0, |g| g + 1);
228-
let mut layout_descriptors =
229-
vec![BindGroupLayoutDescriptor::default(); max_group as usize];
224+
let mut layout_descriptors = vec![BindGroupLayoutDescriptor::default(); max_group as usize];
230225
for (group, desc) in &bind_group_layout_descriptors {
231226
layout_descriptors[*group as usize] = desc.clone();
232227
}
@@ -263,7 +258,6 @@ pub fn create_compute(app: &mut App, shader_entity: Entity) -> Result<Entity> {
263258
entry_point,
264259
pipeline_id,
265260
bind_group_layout_descriptors,
266-
rw_buffers: HashMap::new(),
267261
})
268262
.id());
269263
}
@@ -296,10 +290,7 @@ pub fn set_compute_property(
296290
.get_mut(*buf_entity)
297291
.map_err(|_| ProcessingError::BufferNotFound)?;
298292
compute.shader.insert(&name, buffer.handle.clone());
299-
if read_only {
300-
compute.rw_buffers.remove(&name);
301-
} else {
302-
compute.rw_buffers.insert(name.clone(), *buf_entity);
293+
if !read_only {
303294
buffer.bound_rw = true;
304295
}
305296
Ok(())

crates/processing_render/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,12 +1549,13 @@ fn ensure_buffer_synced(app: &mut App, entity: Entity) -> error::Result<()> {
15491549
.unwrap()?;
15501550

15511551
let world = app.world_mut();
1552-
let mut buffers = world.resource_mut::<Assets<bevy::render::storage::ShaderBuffer>>();
1553-
let asset = buffers
1554-
.get_mut_untracked(handle.id())
1555-
.ok_or(error::ProcessingError::BufferNotFound)?;
1556-
asset.data = Some(bytes);
1557-
drop(buffers);
1552+
{
1553+
let mut buffers = world.resource_mut::<Assets<bevy::render::storage::ShaderBuffer>>();
1554+
let asset = buffers
1555+
.get_mut_untracked(handle.id())
1556+
.ok_or(error::ProcessingError::BufferNotFound)?;
1557+
asset.data = Some(bytes);
1558+
}
15581559

15591560
let mut buf = world
15601561
.get_mut::<compute::Buffer>(entity)

crates/processing_render/src/material/custom.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ pub(crate) fn shader_value_to_reflect(value: &ShaderValue) -> Result<Box<dyn Par
307307
ShaderValue::Mat4(v) => Box::new(Mat4::from_cols_array(v)),
308308
ShaderValue::Texture(_) | ShaderValue::Buffer(_) => {
309309
return Err(ProcessingError::InvalidArgument(
310-
"Texture/Buffer must be bound via set_property, not as a uniform value"
311-
.to_string(),
310+
"Texture/Buffer must be bound via set_property, not as a uniform value".to_string(),
312311
));
313312
}
314313
})

crates/processing_render/src/material/mod.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,16 @@ pub fn set_property(
9595
.map(|p| p.category())
9696
.ok_or_else(|| ProcessingError::UnknownShaderProperty(name.clone()))?;
9797

98-
match category {
99-
ParameterCategory::Storage { read_only } => {
100-
mat.shader.insert(&name, buffer.handle.clone());
101-
if !read_only {
102-
buffer.bound_rw = true;
103-
}
104-
return Ok(());
105-
}
106-
cat => {
107-
return Err(ProcessingError::InvalidArgument(format!(
108-
"property `{name}` expects {cat:?}, got Buffer"
109-
)));
110-
}
98+
let ParameterCategory::Storage { read_only } = category else {
99+
return Err(ProcessingError::InvalidArgument(format!(
100+
"property `{name}` expects {category:?}, got Buffer"
101+
)));
102+
};
103+
mat.shader.insert(&name, buffer.handle.clone());
104+
if !read_only {
105+
buffer.bound_rw = true;
111106
}
107+
return Ok(());
112108
}
113109

114110
return custom::set_property(&mut mat, &name, &value);

0 commit comments

Comments
 (0)