mirror of
https://github.com/servo/servo.git
synced 2025-08-12 08:55:32 +01:00
Auto merge of #27126 - kunalmohan:gpu-mapped-range, r=kvark
Implement GPUBuffer.getMappedRange() <!-- Please describe your changes on the following line: --> The new fields added to `GPUBuffer` are- 1. `mapping`- This is the buffer data. This is not directly exposed to user and is refcounted to keep track of the number of `ArrayBuffer`s that point to the content in it. 2. `mapping_range`- The range of mapped portion of the GPUBuffer. 3. `mapped_ranges`- Ranges that the various `ArrayBuffer`s expose to the user. Used for validation of `getMappedRange()` 4. `js_buffers`- Actual `ArrayBuffer`s that expose the data to the user. They are detached on unmap. 5. `map_promise`- Promise that represents the pending mapping. This PR also changes the order of the exit of threads. WebGL thread is responsible for sending `Exit` message to WebRender and therefore should be exited after WebGPU threads. r?@kvark @jdm --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [ ] These changes fix #___ (GitHub issue number if applicable) <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because ___ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
commit
8713954a53
4 changed files with 144 additions and 112 deletions
|
@ -2769,13 +2769,6 @@ where
|
||||||
warn!("Exit Canvas Paint thread failed ({})", e);
|
warn!("Exit Canvas Paint thread failed ({})", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(webgl_threads) = self.webgl_threads.as_ref() {
|
|
||||||
debug!("Exiting WebGL thread.");
|
|
||||||
if let Err(e) = webgl_threads.exit() {
|
|
||||||
warn!("Exit WebGL Thread failed ({})", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
debug!("Exiting WebGPU threads.");
|
debug!("Exiting WebGPU threads.");
|
||||||
let receivers = self
|
let receivers = self
|
||||||
.browsing_context_group_set
|
.browsing_context_group_set
|
||||||
|
@ -2800,6 +2793,13 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(webgl_threads) = self.webgl_threads.as_ref() {
|
||||||
|
debug!("Exiting WebGL thread.");
|
||||||
|
if let Err(e) = webgl_threads.exit() {
|
||||||
|
warn!("Exit WebGL Thread failed ({})", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
debug!("Exiting GLPlayer thread.");
|
debug!("Exiting GLPlayer thread.");
|
||||||
if let Some(glplayer_threads) = self.glplayer_threads.as_ref() {
|
if let Some(glplayer_threads) = self.glplayer_threads.as_ref() {
|
||||||
if let Err(e) = glplayer_threads.exit() {
|
if let Err(e) = glplayer_threads.exit() {
|
||||||
|
|
|
@ -5,29 +5,32 @@
|
||||||
use crate::dom::bindings::cell::DomRefCell;
|
use crate::dom::bindings::cell::DomRefCell;
|
||||||
use crate::dom::bindings::codegen::Bindings::GPUBufferBinding::{GPUBufferMethods, GPUSize64};
|
use crate::dom::bindings::codegen::Bindings::GPUBufferBinding::{GPUBufferMethods, GPUSize64};
|
||||||
use crate::dom::bindings::codegen::Bindings::GPUMapModeBinding::GPUMapModeConstants;
|
use crate::dom::bindings::codegen::Bindings::GPUMapModeBinding::GPUMapModeConstants;
|
||||||
use crate::dom::bindings::error::Error;
|
use crate::dom::bindings::error::{Error, Fallible};
|
||||||
use crate::dom::bindings::reflector::DomObject;
|
use crate::dom::bindings::reflector::DomObject;
|
||||||
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
|
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
|
||||||
use crate::dom::bindings::root::DomRoot;
|
use crate::dom::bindings::root::DomRoot;
|
||||||
use crate::dom::bindings::str::DOMString;
|
use crate::dom::bindings::str::DOMString;
|
||||||
use crate::dom::bindings::trace::RootedTraceableBox;
|
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
use crate::dom::gpu::{response_async, AsyncWGPUListener};
|
use crate::dom::gpu::{response_async, AsyncWGPUListener};
|
||||||
use crate::dom::promise::Promise;
|
use crate::dom::promise::Promise;
|
||||||
use crate::realms::InRealm;
|
use crate::realms::InRealm;
|
||||||
|
use crate::script_runtime::JSContext;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
|
use js::jsapi::DetachArrayBuffer;
|
||||||
|
use js::jsapi::NewExternalArrayBuffer;
|
||||||
use js::jsapi::{Heap, JSObject};
|
use js::jsapi::{Heap, JSObject};
|
||||||
use js::jsval::UndefinedValue;
|
use std::cell::{Cell, RefCell};
|
||||||
use js::rust::jsapi_wrapped::{DetachArrayBuffer, IsPromiseObject, RejectPromise};
|
use std::ffi::c_void;
|
||||||
use js::typedarray::{ArrayBuffer, CreateWith};
|
|
||||||
use std::cell::Cell;
|
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use std::ptr;
|
use std::ptr::NonNull;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use webgpu::{
|
use webgpu::{
|
||||||
wgpu::device::HostMap, WebGPU, WebGPUBuffer, WebGPUDevice, WebGPURequest, WebGPUResponse,
|
wgpu::device::HostMap, WebGPU, WebGPUBuffer, WebGPUDevice, WebGPURequest, WebGPUResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const RANGE_OFFSET_ALIGN_MASK: u64 = 8;
|
||||||
|
const RANGE_SIZE_ALIGN_MASK: u64 = 4;
|
||||||
|
|
||||||
// https://gpuweb.github.io/gpuweb/#buffer-state
|
// https://gpuweb.github.io/gpuweb/#buffer-state
|
||||||
#[derive(Clone, Copy, MallocSizeOf, PartialEq)]
|
#[derive(Clone, Copy, MallocSizeOf, PartialEq)]
|
||||||
pub enum GPUBufferState {
|
pub enum GPUBufferState {
|
||||||
|
@ -38,6 +41,17 @@ pub enum GPUBufferState {
|
||||||
Destroyed,
|
Destroyed,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(JSTraceable, MallocSizeOf)]
|
||||||
|
pub struct GPUBufferMapInfo {
|
||||||
|
#[ignore_malloc_size_of = "Rc"]
|
||||||
|
pub mapping: Rc<RefCell<Vec<u8>>>,
|
||||||
|
pub mapping_range: Range<u64>,
|
||||||
|
pub mapped_ranges: Vec<Range<u64>>,
|
||||||
|
#[ignore_malloc_size_of = "defined in mozjs"]
|
||||||
|
pub js_buffers: Vec<Box<Heap<*mut JSObject>>>,
|
||||||
|
pub map_mode: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct GPUBuffer {
|
pub struct GPUBuffer {
|
||||||
reflector_: Reflector,
|
reflector_: Reflector,
|
||||||
|
@ -48,11 +62,10 @@ pub struct GPUBuffer {
|
||||||
buffer: WebGPUBuffer,
|
buffer: WebGPUBuffer,
|
||||||
device: WebGPUDevice,
|
device: WebGPUDevice,
|
||||||
valid: Cell<bool>,
|
valid: Cell<bool>,
|
||||||
#[ignore_malloc_size_of = "defined in mozjs"]
|
|
||||||
mapping: RootedTraceableBox<Heap<*mut JSObject>>,
|
|
||||||
mapping_range: DomRefCell<Range<u64>>,
|
|
||||||
size: GPUSize64,
|
size: GPUSize64,
|
||||||
map_mode: Cell<Option<u32>>,
|
#[ignore_malloc_size_of = "promises are hard"]
|
||||||
|
map_promise: DomRefCell<Option<Rc<Promise>>>,
|
||||||
|
map_info: DomRefCell<Option<GPUBufferMapInfo>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GPUBuffer {
|
impl GPUBuffer {
|
||||||
|
@ -63,8 +76,7 @@ impl GPUBuffer {
|
||||||
state: GPUBufferState,
|
state: GPUBufferState,
|
||||||
size: GPUSize64,
|
size: GPUSize64,
|
||||||
valid: bool,
|
valid: bool,
|
||||||
mapping: RootedTraceableBox<Heap<*mut JSObject>>,
|
map_info: DomRefCell<Option<GPUBufferMapInfo>>,
|
||||||
mapping_range: Range<u64>,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
reflector_: Reflector::new(),
|
reflector_: Reflector::new(),
|
||||||
|
@ -74,10 +86,9 @@ impl GPUBuffer {
|
||||||
valid: Cell::new(valid),
|
valid: Cell::new(valid),
|
||||||
device,
|
device,
|
||||||
buffer,
|
buffer,
|
||||||
mapping,
|
map_promise: DomRefCell::new(None),
|
||||||
size,
|
size,
|
||||||
mapping_range: DomRefCell::new(mapping_range),
|
map_info,
|
||||||
map_mode: Cell::new(None),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,19 +101,11 @@ impl GPUBuffer {
|
||||||
state: GPUBufferState,
|
state: GPUBufferState,
|
||||||
size: GPUSize64,
|
size: GPUSize64,
|
||||||
valid: bool,
|
valid: bool,
|
||||||
mapping: RootedTraceableBox<Heap<*mut JSObject>>,
|
map_info: DomRefCell<Option<GPUBufferMapInfo>>,
|
||||||
mapping_range: Range<u64>,
|
|
||||||
) -> DomRoot<Self> {
|
) -> DomRoot<Self> {
|
||||||
reflect_dom_object(
|
reflect_dom_object(
|
||||||
Box::new(GPUBuffer::new_inherited(
|
Box::new(GPUBuffer::new_inherited(
|
||||||
channel,
|
channel, buffer, device, state, size, valid, map_info,
|
||||||
buffer,
|
|
||||||
device,
|
|
||||||
state,
|
|
||||||
size,
|
|
||||||
valid,
|
|
||||||
mapping,
|
|
||||||
mapping_range,
|
|
||||||
)),
|
)),
|
||||||
global,
|
global,
|
||||||
)
|
)
|
||||||
|
@ -142,51 +145,32 @@ impl GPUBufferMethods for GPUBuffer {
|
||||||
},
|
},
|
||||||
// Step 3
|
// Step 3
|
||||||
GPUBufferState::Mapped | GPUBufferState::MappedAtCreation => {
|
GPUBufferState::Mapped | GPUBufferState::MappedAtCreation => {
|
||||||
match ArrayBuffer::from(self.mapping.get()) {
|
let mut info = self.map_info.borrow_mut();
|
||||||
Ok(array_buffer) => {
|
let m_info = info.as_mut().unwrap();
|
||||||
// Step 3.2
|
let m_range = m_info.mapping_range.clone();
|
||||||
self.channel
|
if let Err(e) = self.channel.0.send(WebGPURequest::UnmapBuffer {
|
||||||
.0
|
|
||||||
.send(WebGPURequest::UnmapBuffer {
|
|
||||||
buffer_id: self.id().0,
|
buffer_id: self.id().0,
|
||||||
array_buffer: array_buffer.to_vec(),
|
array_buffer: m_info.mapping.borrow().clone(),
|
||||||
is_map_read: self.map_mode.get() == Some(GPUMapModeConstants::READ),
|
is_map_read: m_info.map_mode == Some(GPUMapModeConstants::READ),
|
||||||
offset: self.mapping_range.borrow().start,
|
offset: m_range.start,
|
||||||
size: self.mapping_range.borrow().end -
|
size: m_range.end - m_range.start,
|
||||||
self.mapping_range.borrow().start,
|
}) {
|
||||||
})
|
warn!("Failed to send Buffer unmap ({:?}) ({})", self.buffer.0, e);
|
||||||
.unwrap();
|
|
||||||
// Step 3.3
|
|
||||||
unsafe {
|
|
||||||
DetachArrayBuffer(*cx, self.mapping.handle());
|
|
||||||
}
|
}
|
||||||
},
|
// Step 3.3
|
||||||
Err(_) => {
|
m_info.js_buffers.drain(..).for_each(|obj| unsafe {
|
||||||
warn!(
|
DetachArrayBuffer(*cx, obj.handle());
|
||||||
"Could not find ArrayBuffer of Mapped buffer ({:?})",
|
});
|
||||||
self.buffer.0
|
|
||||||
);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
// Step 2
|
// Step 2
|
||||||
GPUBufferState::MappingPending => unsafe {
|
GPUBufferState::MappingPending => {
|
||||||
if IsPromiseObject(self.mapping.handle()) {
|
let promise = self.map_promise.borrow_mut().take().unwrap();
|
||||||
let err = Error::Operation;
|
promise.reject_error(Error::Operation);
|
||||||
rooted!(in(*cx) let mut undef = UndefinedValue());
|
|
||||||
err.to_jsval(*cx, &self.global(), undef.handle_mut());
|
|
||||||
RejectPromise(*cx, self.mapping.handle(), undef.handle());
|
|
||||||
} else {
|
|
||||||
warn!("No promise object for pending mapping found");
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
// Step 3.3
|
|
||||||
self.mapping.set(ptr::null_mut());
|
|
||||||
// Step 4
|
// Step 4
|
||||||
self.state.set(GPUBufferState::Unmapped);
|
self.state.set(GPUBufferState::Unmapped);
|
||||||
self.map_mode.set(None);
|
*self.map_info.borrow_mut() = None;
|
||||||
*self.mapping_range.borrow_mut() = 0..0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// https://gpuweb.github.io/gpuweb/#dom-gpubuffer-destroy
|
/// https://gpuweb.github.io/gpuweb/#dom-gpubuffer-destroy
|
||||||
|
@ -213,7 +197,13 @@ impl GPUBufferMethods for GPUBuffer {
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
/// https://gpuweb.github.io/gpuweb/#dom-gpubuffer-mapasync-offset-size
|
/// https://gpuweb.github.io/gpuweb/#dom-gpubuffer-mapasync-offset-size
|
||||||
fn MapAsync(&self, mode: u32, offset: u64, size: u64, comp: InRealm) -> Rc<Promise> {
|
fn MapAsync(
|
||||||
|
&self,
|
||||||
|
mode: u32,
|
||||||
|
offset: GPUSize64,
|
||||||
|
size: GPUSize64,
|
||||||
|
comp: InRealm,
|
||||||
|
) -> Rc<Promise> {
|
||||||
let promise = Promise::new_in_current_realm(&self.global(), comp);
|
let promise = Promise::new_in_current_realm(&self.global(), comp);
|
||||||
let map_range = if size == 0 {
|
let map_range = if size == 0 {
|
||||||
offset..self.size
|
offset..self.size
|
||||||
|
@ -237,7 +227,6 @@ impl GPUBufferMethods for GPUBuffer {
|
||||||
promise.reject_error(Error::Abort);
|
promise.reject_error(Error::Abort);
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
self.mapping.set(*promise.promise_obj());
|
|
||||||
|
|
||||||
let sender = response_async(&promise, self);
|
let sender = response_async(&promise, self);
|
||||||
if let Err(e) = self.channel.0.send(WebGPURequest::BufferMapAsync {
|
if let Err(e) = self.channel.0.send(WebGPURequest::BufferMapAsync {
|
||||||
|
@ -255,11 +244,65 @@ impl GPUBufferMethods for GPUBuffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.state.set(GPUBufferState::MappingPending);
|
self.state.set(GPUBufferState::MappingPending);
|
||||||
self.map_mode.set(Some(mode));
|
*self.map_info.borrow_mut() = Some(GPUBufferMapInfo {
|
||||||
*self.mapping_range.borrow_mut() = map_range;
|
mapping: Rc::new(RefCell::new(Vec::with_capacity(0))),
|
||||||
|
mapping_range: map_range,
|
||||||
|
mapped_ranges: Vec::new(),
|
||||||
|
js_buffers: Vec::new(),
|
||||||
|
map_mode: Some(mode),
|
||||||
|
});
|
||||||
|
*self.map_promise.borrow_mut() = Some(promise.clone());
|
||||||
promise
|
promise
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// https://gpuweb.github.io/gpuweb/#dom-gpubuffer-getmappedrange
|
||||||
|
#[allow(unsafe_code)]
|
||||||
|
fn GetMappedRange(
|
||||||
|
&self,
|
||||||
|
cx: JSContext,
|
||||||
|
offset: GPUSize64,
|
||||||
|
size: GPUSize64,
|
||||||
|
) -> Fallible<NonNull<JSObject>> {
|
||||||
|
let m_end = if size == 0 { self.size } else { offset + size };
|
||||||
|
let mut info = self.map_info.borrow_mut();
|
||||||
|
let m_info = info.as_mut().unwrap();
|
||||||
|
|
||||||
|
let mut valid = match self.state.get() {
|
||||||
|
GPUBufferState::Mapped | GPUBufferState::MappedAtCreation => true,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
valid &= offset % RANGE_OFFSET_ALIGN_MASK == 0 &&
|
||||||
|
(m_end - offset) % RANGE_SIZE_ALIGN_MASK == 0 &&
|
||||||
|
offset >= m_info.mapping_range.start &&
|
||||||
|
m_end <= m_info.mapping_range.end;
|
||||||
|
valid &= m_info
|
||||||
|
.mapped_ranges
|
||||||
|
.iter()
|
||||||
|
.all(|range| range.start >= m_end || range.end <= offset);
|
||||||
|
if !valid {
|
||||||
|
return Err(Error::Operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe extern "C" fn free_func(_contents: *mut c_void, free_user_data: *mut c_void) {
|
||||||
|
let _ = Rc::from_raw(free_user_data as _);
|
||||||
|
}
|
||||||
|
|
||||||
|
let array_buffer = unsafe {
|
||||||
|
NewExternalArrayBuffer(
|
||||||
|
*cx,
|
||||||
|
(m_end - offset) as usize,
|
||||||
|
m_info.mapping.borrow_mut()[offset as usize..m_end as usize].as_mut_ptr() as _,
|
||||||
|
Some(free_func),
|
||||||
|
Rc::into_raw(m_info.mapping.clone()) as _,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
m_info.mapped_ranges.push(offset..m_end);
|
||||||
|
m_info.js_buffers.push(Heap::boxed(array_buffer));
|
||||||
|
|
||||||
|
Ok(NonNull::new(array_buffer).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
/// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
|
/// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
|
||||||
fn GetLabel(&self) -> Option<DOMString> {
|
fn GetLabel(&self) -> Option<DOMString> {
|
||||||
self.label.borrow().clone()
|
self.label.borrow().clone()
|
||||||
|
@ -276,21 +319,14 @@ impl AsyncWGPUListener for GPUBuffer {
|
||||||
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) {
|
fn handle_response(&self, response: WebGPUResponse, promise: &Rc<Promise>) {
|
||||||
match response {
|
match response {
|
||||||
WebGPUResponse::BufferMapAsync(bytes) => {
|
WebGPUResponse::BufferMapAsync(bytes) => {
|
||||||
let cx = self.global().get_cx();
|
*self
|
||||||
rooted!(in(*cx) let mut array_buffer = ptr::null_mut::<JSObject>());
|
.map_info
|
||||||
match unsafe {
|
.borrow_mut()
|
||||||
ArrayBuffer::create(*cx, CreateWith::Slice(&bytes), array_buffer.handle_mut())
|
.as_mut()
|
||||||
} {
|
.unwrap()
|
||||||
Ok(_) => promise.resolve_native(&()),
|
.mapping
|
||||||
Err(()) => {
|
.borrow_mut() = bytes;
|
||||||
warn!(
|
promise.resolve_native(&());
|
||||||
"Failed to create ArrayBuffer for buffer({:?})",
|
|
||||||
self.buffer.0
|
|
||||||
);
|
|
||||||
promise.reject_error(Error::Operation);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
self.mapping.set(array_buffer.get());
|
|
||||||
self.state.set(GPUBufferState::Mapped);
|
self.state.set(GPUBufferState::Mapped);
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -298,6 +334,7 @@ impl AsyncWGPUListener for GPUBuffer {
|
||||||
promise.reject_error(Error::Operation);
|
promise.reject_error(Error::Operation);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
*self.map_promise.borrow_mut() = None;
|
||||||
if let Err(e) = self
|
if let Err(e) = self
|
||||||
.channel
|
.channel
|
||||||
.0
|
.0
|
||||||
|
|
|
@ -41,7 +41,7 @@ use crate::dom::globalscope::GlobalScope;
|
||||||
use crate::dom::gpuadapter::GPUAdapter;
|
use crate::dom::gpuadapter::GPUAdapter;
|
||||||
use crate::dom::gpubindgroup::GPUBindGroup;
|
use crate::dom::gpubindgroup::GPUBindGroup;
|
||||||
use crate::dom::gpubindgrouplayout::GPUBindGroupLayout;
|
use crate::dom::gpubindgrouplayout::GPUBindGroupLayout;
|
||||||
use crate::dom::gpubuffer::{GPUBuffer, GPUBufferState};
|
use crate::dom::gpubuffer::{GPUBuffer, GPUBufferMapInfo, GPUBufferState};
|
||||||
use crate::dom::gpucommandencoder::GPUCommandEncoder;
|
use crate::dom::gpucommandencoder::GPUCommandEncoder;
|
||||||
use crate::dom::gpucomputepipeline::GPUComputePipeline;
|
use crate::dom::gpucomputepipeline::GPUComputePipeline;
|
||||||
use crate::dom::gpupipelinelayout::GPUPipelineLayout;
|
use crate::dom::gpupipelinelayout::GPUPipelineLayout;
|
||||||
|
@ -54,8 +54,9 @@ use crate::script_runtime::JSContext as SafeJSContext;
|
||||||
use arrayvec::ArrayVec;
|
use arrayvec::ArrayVec;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use js::jsapi::{Heap, JSObject};
|
use js::jsapi::{Heap, JSObject};
|
||||||
use js::typedarray::{ArrayBuffer, CreateWith};
|
use std::cell::RefCell;
|
||||||
use std::ptr::{self, NonNull};
|
use std::ptr::NonNull;
|
||||||
|
use std::rc::Rc;
|
||||||
use webgpu::wgpu::binding_model::BufferBinding;
|
use webgpu::wgpu::binding_model::BufferBinding;
|
||||||
use webgpu::{self, wgt, WebGPU, WebGPUBindings, WebGPURequest};
|
use webgpu::{self, wgt, WebGPU, WebGPUBindings, WebGPURequest};
|
||||||
|
|
||||||
|
@ -177,26 +178,21 @@ impl GPUDeviceMethods for GPUDevice {
|
||||||
.expect("Failed to create WebGPU buffer");
|
.expect("Failed to create WebGPU buffer");
|
||||||
|
|
||||||
let buffer = webgpu::WebGPUBuffer(id);
|
let buffer = webgpu::WebGPUBuffer(id);
|
||||||
let mapping = RootedTraceableBox::new(Heap::default());
|
let map_info;
|
||||||
let state;
|
let state;
|
||||||
let mapping_range;
|
|
||||||
if descriptor.mappedAtCreation {
|
if descriptor.mappedAtCreation {
|
||||||
let cx = self.global().get_cx();
|
let buf_data = vec![0u8; descriptor.size as usize];
|
||||||
rooted!(in(*cx) let mut array_buffer = ptr::null_mut::<JSObject>());
|
map_info = DomRefCell::new(Some(GPUBufferMapInfo {
|
||||||
unsafe {
|
mapping: Rc::new(RefCell::new(buf_data)),
|
||||||
assert!(ArrayBuffer::create(
|
mapping_range: 0..descriptor.size,
|
||||||
*cx,
|
mapped_ranges: Vec::new(),
|
||||||
CreateWith::Length(descriptor.size as u32),
|
js_buffers: Vec::new(),
|
||||||
array_buffer.handle_mut(),
|
map_mode: None,
|
||||||
)
|
}));
|
||||||
.is_ok());
|
|
||||||
}
|
|
||||||
mapping.set(array_buffer.get());
|
|
||||||
state = GPUBufferState::MappedAtCreation;
|
state = GPUBufferState::MappedAtCreation;
|
||||||
mapping_range = 0..descriptor.size;
|
|
||||||
} else {
|
} else {
|
||||||
|
map_info = DomRefCell::new(None);
|
||||||
state = GPUBufferState::Unmapped;
|
state = GPUBufferState::Unmapped;
|
||||||
mapping_range = 0..0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GPUBuffer::new(
|
GPUBuffer::new(
|
||||||
|
@ -207,8 +203,7 @@ impl GPUDeviceMethods for GPUDevice {
|
||||||
state,
|
state,
|
||||||
descriptor.size,
|
descriptor.size,
|
||||||
true,
|
true,
|
||||||
mapping,
|
map_info,
|
||||||
mapping_range,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom.webgpu.enabled"]
|
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom.webgpu.enabled"]
|
||||||
interface GPUBuffer {
|
interface GPUBuffer {
|
||||||
Promise<void> mapAsync(GPUMapModeFlags mode, optional GPUSize64 offset = 0, optional GPUSize64 size = 0);
|
Promise<void> mapAsync(GPUMapModeFlags mode, optional GPUSize64 offset = 0, optional GPUSize64 size = 0);
|
||||||
//ArrayBuffer getMappedRange(optional GPUSize64 offset = 0, optional GPUSize64 size = 0);
|
[Throws] ArrayBuffer getMappedRange(optional GPUSize64 offset = 0, optional GPUSize64 size = 0);
|
||||||
void unmap();
|
void unmap();
|
||||||
|
|
||||||
void destroy();
|
void destroy();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue