mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-23 07:51:08 +01:00
Merge #570
570: web: temporarily polyfill `create_buffer_init` r=kvark a=grovesNL Temporarily polyfill `create_buffer_init` on the web backend by using the old buffer mapping API. This is just a temporary fix until the new buffer mapping API is available in Nightly. This polyfill allows us to run `cube` from `master` on Nightly (there's still lots missing from other examples). But at least some simple use cases (e.g. projects only using `create_buffer_init`/`write_buffer` and no other buffer mapping functions) can run with `master`. Co-authored-by: Joshua Groves <josh@joshgroves.com>
This commit is contained in:
commit
96b70210a8
2 files changed
+46
-10
No files matched your search
@@ -42,6 +42,30 @@ impl fmt::Debug for Context {
|
||||
}
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn create_buffer_init_polyfill(
|
||||
device: &Sendable<web_sys::GpuDevice>,
|
||||
desc: &BufferDescriptor<'_>,
|
||||
contents: &[u8],
|
||||
) -> Sendable<web_sys::GpuBuffer> {
|
||||
// Emulate buffer mapping with the old API. This is a temporary
|
||||
// polyfill until the new buffer mapping API is available on gecko.
|
||||
let mut mapped_desc =
|
||||
web_sys::GpuBufferDescriptor::new(desc.size as f64, desc.usage.bits());
|
||||
if let Some(label) = desc.label {
|
||||
mapped_desc.label(label);
|
||||
}
|
||||
let pair = device.0.create_buffer_mapped(&mapped_desc);
|
||||
let buffer: web_sys::GpuBuffer = pair.get(0).into();
|
||||
let array_buffer: js_sys::ArrayBuffer = pair.get(1).into();
|
||||
let mapped = js_sys::Uint8Array::new(&array_buffer);
|
||||
let js_contents: js_sys::Uint8Array = contents.into();
|
||||
mapped.set(&js_contents, 0);
|
||||
buffer.unmap();
|
||||
Sendable(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ComputePass(web_sys::GpuComputePassEncoder);
|
||||
#[derive(Debug)]
|
||||
|
||||
+22
-10
@@ -74,21 +74,33 @@ impl DeviceExt for crate::Device {
|
||||
let mut map_context = crate::MapContext::new(padded_size);
|
||||
|
||||
map_context.initial_range = 0..padded_size;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let buffer = crate::Buffer {
|
||||
context: Arc::clone(&self.context),
|
||||
id: crate::Context::device_create_buffer(&*self.context, &self.id, &wgt_descriptor),
|
||||
id: crate::backend::Context::create_buffer_init_polyfill(&self.id, &wgt_descriptor, descriptor.contents),
|
||||
map_context: parking_lot::Mutex::new(map_context),
|
||||
usage: descriptor.usage,
|
||||
};
|
||||
|
||||
let range =
|
||||
crate::Context::buffer_get_mapped_range_mut(&*self.context, &buffer.id, 0..padded_size);
|
||||
range[0..unpadded_size as usize].copy_from_slice(descriptor.contents);
|
||||
for i in unpadded_size..padded_size {
|
||||
range[i as usize] = 0;
|
||||
}
|
||||
|
||||
buffer.unmap();
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let buffer = {
|
||||
let buffer = crate::Buffer {
|
||||
context: Arc::clone(&self.context),
|
||||
id: crate::Context::device_create_buffer(&*self.context, &self.id, &wgt_descriptor),
|
||||
map_context: parking_lot::Mutex::new(map_context),
|
||||
usage: descriptor.usage,
|
||||
};
|
||||
|
||||
let range =
|
||||
crate::Context::buffer_get_mapped_range_mut(&*self.context, &buffer.id, 0..padded_size);
|
||||
range[0..unpadded_size as usize].copy_from_slice(descriptor.contents);
|
||||
for i in unpadded_size..padded_size {
|
||||
range[i as usize] = 0;
|
||||
}
|
||||
|
||||
buffer.unmap();
|
||||
buffer
|
||||
};
|
||||
buffer
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user