Auto merge of #26769 - kunalmohan:gpu-render-pass, r=kvark

Implement GPURenderPassEncoder

Add webidls for GPURenderPassEncoder and  GPURenderEncoderBase and
implement relevant methods.

<!-- Please describe your changes on the following line: -->
r?@kvark

---
<!-- 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:
bors-servo 2020-06-03 21:56:03 -04:00 committed by GitHub
commit 0281acea95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 572 additions and 115 deletions

5
Cargo.lock generated
View file

@ -1895,7 +1895,6 @@ dependencies = [
"raw-window-handle",
"smallvec 1.3.0",
"winapi",
"x11",
]
[[package]]
@ -6592,7 +6591,7 @@ dependencies = [
[[package]]
name = "wgpu-core"
version = "0.5.0"
source = "git+https://github.com/gfx-rs/wgpu#a203333c3e144cfd431c812213966ee32ae59d98"
source = "git+https://github.com/gfx-rs/wgpu#ac9587e9ced5b043abad68e260cb8c9e812cffb5"
dependencies = [
"arrayvec 0.5.1",
"bitflags",
@ -6621,7 +6620,7 @@ dependencies = [
[[package]]
name = "wgpu-types"
version = "0.5.0"
source = "git+https://github.com/gfx-rs/wgpu#a203333c3e144cfd431c812213966ee32ae59d98"
source = "git+https://github.com/gfx-rs/wgpu#ac9587e9ced5b043abad68e260cb8c9e812cffb5"
dependencies = [
"bitflags",
"peek-poke 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -39,8 +39,8 @@ impl GPUAdapter {
name: DOMString,
extensions: Heap<*mut JSObject>,
adapter: WebGPUAdapter,
) -> GPUAdapter {
GPUAdapter {
) -> Self {
Self {
reflector_: Reflector::new(),
channel,
name,
@ -55,7 +55,7 @@ impl GPUAdapter {
name: DOMString,
extensions: Heap<*mut JSObject>,
adapter: WebGPUAdapter,
) -> DomRoot<GPUAdapter> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUAdapter::new_inherited(
channel, name, extensions, adapter,
@ -81,9 +81,7 @@ impl GPUAdapterMethods for GPUAdapter {
let promise = Promise::new_in_current_realm(&self.global(), comp);
let sender = response_async(&promise, self);
let desc = wgt::DeviceDescriptor {
extensions: wgt::Extensions {
anisotropic_filtering: descriptor.extensions.anisotropicFiltering,
},
extensions: wgt::Extensions::empty(),
limits: wgt::Limits {
max_bind_groups: descriptor.limits.maxBindGroups,
},

View file

@ -21,7 +21,7 @@ pub struct GPUBindGroup {
}
impl GPUBindGroup {
fn new_inherited(bind_group: WebGPUBindGroup, valid: bool) -> GPUBindGroup {
fn new_inherited(bind_group: WebGPUBindGroup, valid: bool) -> Self {
Self {
reflector_: Reflector::new(),
label: DomRefCell::new(None),
@ -30,11 +30,7 @@ impl GPUBindGroup {
}
}
pub fn new(
global: &GlobalScope,
bind_group: WebGPUBindGroup,
valid: bool,
) -> DomRoot<GPUBindGroup> {
pub fn new(global: &GlobalScope, bind_group: WebGPUBindGroup, valid: bool) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUBindGroup::new_inherited(bind_group, valid)),
global,

View file

@ -32,7 +32,7 @@ impl GPUBindGroupLayout {
bind_group_layout: WebGPUBindGroupLayout,
bindings: Vec<GPUBindGroupLayoutEntry>,
valid: bool,
) -> GPUBindGroupLayout {
) -> Self {
Self {
reflector_: Reflector::new(),
channel,
@ -49,7 +49,7 @@ impl GPUBindGroupLayout {
bind_group_layout: WebGPUBindGroupLayout,
bindings: Vec<GPUBindGroupLayoutEntry>,
valid: bool,
) -> DomRoot<GPUBindGroupLayout> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUBindGroupLayout::new_inherited(
channel,

View file

@ -57,7 +57,7 @@ impl GPUBuffer {
usage: u32,
valid: bool,
mapping: RootedTraceableBox<Heap<*mut JSObject>>,
) -> GPUBuffer {
) -> Self {
Self {
reflector_: Reflector::new(),
channel,
@ -83,7 +83,7 @@ impl GPUBuffer {
usage: u32,
valid: bool,
mapping: RootedTraceableBox<Heap<*mut JSObject>>,
) -> DomRoot<GPUBuffer> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUBuffer::new_inherited(
channel, buffer, device, state, size, usage, valid, mapping,

View file

@ -37,8 +37,8 @@ impl GPUCommandBuffer {
channel: WebGPU,
command_buffer: WebGPUCommandBuffer,
buffers: HashSet<DomRoot<GPUBuffer>>,
) -> GPUCommandBuffer {
GPUCommandBuffer {
) -> Self {
Self {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
@ -52,7 +52,7 @@ impl GPUCommandBuffer {
channel: WebGPU,
command_buffer: WebGPUCommandBuffer,
buffers: HashSet<DomRoot<GPUBuffer>>,
) -> DomRoot<GPUCommandBuffer> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUCommandBuffer::new_inherited(
channel,

View file

@ -6,6 +6,10 @@ use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::GPUBufferBinding::GPUSize64;
use crate::dom::bindings::codegen::Bindings::GPUCommandEncoderBinding::{
GPUCommandBufferDescriptor, GPUCommandEncoderMethods, GPUComputePassDescriptor,
GPURenderPassDescriptor, GPUStencilLoadValue, GPUStoreOp,
};
use crate::dom::bindings::codegen::UnionTypes::{
GPULoadOpOrDoubleSequenceOrGPUColorDict as GPUColorLoad, GPULoadOpOrFloat,
};
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
@ -15,17 +19,20 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubuffer::GPUBuffer;
use crate::dom::gpucommandbuffer::GPUCommandBuffer;
use crate::dom::gpucomputepassencoder::GPUComputePassEncoder;
use crate::dom::gpurenderpassencoder::GPURenderPassEncoder;
use dom_struct::dom_struct;
use std::cell::Cell;
use std::collections::HashSet;
use webgpu::wgt::BufferUsage;
use webgpu::{self, WebGPU, WebGPURequest};
use webgpu::wgpu::command::{
RawPass, RenderPassColorAttachmentDescriptor, RenderPassDepthStencilAttachmentDescriptor,
RenderPassDescriptor,
};
use webgpu::{self, wgt, WebGPU, WebGPUDevice, WebGPURequest};
const BUFFER_COPY_ALIGN_MASK: u64 = 3;
// https://gpuweb.github.io/gpuweb/#enumdef-encoder-state
#[derive(MallocSizeOf, PartialEq)]
#[allow(dead_code)]
pub enum GPUCommandEncoderState {
Open,
EncodingRenderPass,
@ -42,19 +49,22 @@ pub struct GPUCommandEncoder {
encoder: webgpu::WebGPUCommandEncoder,
buffers: DomRefCell<HashSet<DomRoot<GPUBuffer>>>,
state: DomRefCell<GPUCommandEncoderState>,
device: WebGPUDevice,
valid: Cell<bool>,
}
impl GPUCommandEncoder {
pub fn new_inherited(
channel: WebGPU,
device: WebGPUDevice,
encoder: webgpu::WebGPUCommandEncoder,
valid: bool,
) -> GPUCommandEncoder {
GPUCommandEncoder {
) -> Self {
Self {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
device,
encoder,
buffers: DomRefCell::new(HashSet::new()),
state: DomRefCell::new(GPUCommandEncoderState::Open),
@ -65,11 +75,14 @@ impl GPUCommandEncoder {
pub fn new(
global: &GlobalScope,
channel: WebGPU,
device: WebGPUDevice,
encoder: webgpu::WebGPUCommandEncoder,
valid: bool,
) -> DomRoot<GPUCommandEncoder> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUCommandEncoder::new_inherited(channel, encoder, valid)),
Box::new(GPUCommandEncoder::new_inherited(
channel, device, encoder, valid,
)),
global,
)
}
@ -113,6 +126,94 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
GPUComputePassEncoder::new(&self.global(), self.channel.clone(), &self)
}
#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-beginrenderpass
fn BeginRenderPass(
&self,
descriptor: &GPURenderPassDescriptor,
) -> DomRoot<GPURenderPassEncoder> {
self.set_state(
GPUCommandEncoderState::EncodingRenderPass,
GPUCommandEncoderState::Open,
);
let colors = descriptor
.colorAttachments
.iter()
.map(|color| {
let (load_op, clear_color) = match color.loadValue {
GPUColorLoad::GPULoadOp(_) => (wgt::LoadOp::Load, wgt::Color::TRANSPARENT),
GPUColorLoad::DoubleSequence(ref s) => (
wgt::LoadOp::Clear,
wgt::Color {
r: *s[0],
g: *s[1],
b: *s[2],
a: *s[3],
},
),
GPUColorLoad::GPUColorDict(ref d) => (
wgt::LoadOp::Clear,
wgt::Color {
r: *d.r,
g: *d.g,
b: *d.b,
a: *d.a,
},
),
};
RenderPassColorAttachmentDescriptor {
attachment: color.attachment.id().0,
resolve_target: color.resolveTarget.as_ref().map(|t| t.id().0),
load_op,
store_op: match color.storeOp {
GPUStoreOp::Store => wgt::StoreOp::Store,
GPUStoreOp::Clear => wgt::StoreOp::Clear,
},
clear_color,
}
})
.collect::<Vec<_>>();
let depth_stencil = descriptor.depthStencilAttachment.as_ref().map(|depth| {
let (depth_load_op, clear_depth) = match depth.depthLoadValue {
GPULoadOpOrFloat::GPULoadOp(_) => (wgt::LoadOp::Load, 0.0f32),
GPULoadOpOrFloat::Float(f) => (wgt::LoadOp::Clear, *f),
};
let (stencil_load_op, clear_stencil) = match depth.stencilLoadValue {
GPUStencilLoadValue::GPULoadOp(_) => (wgt::LoadOp::Load, 0u32),
GPUStencilLoadValue::RangeEnforcedUnsignedLong(l) => (wgt::LoadOp::Clear, l),
};
RenderPassDepthStencilAttachmentDescriptor {
attachment: depth.attachment.id().0,
depth_load_op,
depth_store_op: match depth.depthStoreOp {
GPUStoreOp::Store => wgt::StoreOp::Store,
GPUStoreOp::Clear => wgt::StoreOp::Clear,
},
clear_depth,
depth_read_only: depth.depthReadOnly,
stencil_load_op,
stencil_store_op: match depth.stencilStoreOp {
GPUStoreOp::Store => wgt::StoreOp::Store,
GPUStoreOp::Clear => wgt::StoreOp::Clear,
},
clear_stencil,
stencil_read_only: depth.stencilReadOnly,
}
});
let desc = RenderPassDescriptor {
color_attachments: colors.as_ptr(),
color_attachments_length: colors.len(),
depth_stencil_attachment: depth_stencil.as_ref(),
};
let raw_pass = unsafe { RawPass::new_render(self.id().0, &desc) };
GPURenderPassEncoder::new(&self.global(), self.channel.clone(), raw_pass, &self)
}
/// https://gpuweb.github.io/gpuweb/#dom-gpucommandencoder-copybuffertobuffer
fn CopyBufferToBuffer(
&self,
@ -130,12 +231,12 @@ impl GPUCommandEncoderMethods for GPUCommandEncoder {
Some(_) => true,
None => false,
};
valid &= match BufferUsage::from_bits(source.usage()) {
Some(usage) => usage.contains(BufferUsage::COPY_SRC),
valid &= match wgt::BufferUsage::from_bits(source.usage()) {
Some(usage) => usage.contains(wgt::BufferUsage::COPY_SRC),
None => false,
};
valid &= match BufferUsage::from_bits(destination.usage()) {
Some(usage) => usage.contains(BufferUsage::COPY_DST),
valid &= match wgt::BufferUsage::from_bits(destination.usage()) {
Some(usage) => usage.contains(wgt::BufferUsage::COPY_DST),
None => false,
};
valid &= (*self.state.borrow() == GPUCommandEncoderState::Open) &&

View file

@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#![allow(unsafe_code)]
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::GPUComputePassEncoderBinding::GPUComputePassEncoderMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
@ -12,7 +14,6 @@ use crate::dom::gpubindgroup::GPUBindGroup;
use crate::dom::gpucommandencoder::{GPUCommandEncoder, GPUCommandEncoderState};
use crate::dom::gpucomputepipeline::GPUComputePipeline;
use dom_struct::dom_struct;
use std::cell::RefCell;
use webgpu::{
wgpu::command::{
compute_ffi::{
@ -31,27 +32,22 @@ pub struct GPUComputePassEncoder {
channel: WebGPU,
label: DomRefCell<Option<DOMString>>,
#[ignore_malloc_size_of = "defined in wgpu-core"]
raw_pass: RefCell<Option<RawPass>>,
raw_pass: DomRefCell<Option<RawPass>>,
command_encoder: Dom<GPUCommandEncoder>,
}
impl GPUComputePassEncoder {
#[allow(unsafe_code)]
fn new_inherited(channel: WebGPU, parent: &GPUCommandEncoder) -> GPUComputePassEncoder {
GPUComputePassEncoder {
fn new_inherited(channel: WebGPU, parent: &GPUCommandEncoder) -> Self {
Self {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
raw_pass: RefCell::new(Some(unsafe { RawPass::new_compute(parent.id().0) })),
raw_pass: DomRefCell::new(Some(unsafe { RawPass::new_compute(parent.id().0) })),
command_encoder: Dom::from_ref(parent),
}
}
pub fn new(
global: &GlobalScope,
channel: WebGPU,
parent: &GPUCommandEncoder,
) -> DomRoot<GPUComputePassEncoder> {
pub fn new(global: &GlobalScope, channel: WebGPU, parent: &GPUCommandEncoder) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUComputePassEncoder::new_inherited(channel, parent)),
global,
@ -70,7 +66,6 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
*self.label.borrow_mut() = value;
}
#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-dispatch
fn Dispatch(&self, x: u32, y: u32, z: u32) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
@ -78,7 +73,6 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
}
}
#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-endpass
fn EndPass(&self) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().take() {
@ -99,7 +93,6 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
}
}
#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpuprogrammablepassencoder-setbindgroup
fn SetBindGroup(&self, index: u32, bind_group: &GPUBindGroup, dynamic_offsets: Vec<u32>) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
@ -115,7 +108,6 @@ impl GPUComputePassEncoderMethods for GPUComputePassEncoder {
}
}
#[allow(unsafe_code)]
/// https://gpuweb.github.io/gpuweb/#dom-gpucomputepassencoder-setpipeline
fn SetPipeline(&self, pipeline: &GPUComputePipeline) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {

View file

@ -20,7 +20,7 @@ pub struct GPUComputePipeline {
}
impl GPUComputePipeline {
fn new_inherited(compute_pipeline: WebGPUComputePipeline) -> GPUComputePipeline {
fn new_inherited(compute_pipeline: WebGPUComputePipeline) -> Self {
Self {
reflector_: Reflector::new(),
label: DomRefCell::new(None),
@ -28,10 +28,7 @@ impl GPUComputePipeline {
}
}
pub fn new(
global: &GlobalScope,
compute_pipeline: WebGPUComputePipeline,
) -> DomRoot<GPUComputePipeline> {
pub fn new(global: &GlobalScope, compute_pipeline: WebGPUComputePipeline) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUComputePipeline::new_inherited(compute_pipeline)),
global,

View file

@ -85,7 +85,7 @@ impl GPUDevice {
limits: Heap<*mut JSObject>,
device: webgpu::WebGPUDevice,
queue: &GPUQueue,
) -> GPUDevice {
) -> Self {
Self {
eventtarget: EventTarget::new_inherited(),
channel,
@ -98,7 +98,6 @@ impl GPUDevice {
}
}
#[allow(unsafe_code)]
pub fn new(
global: &GlobalScope,
channel: WebGPU,
@ -107,7 +106,7 @@ impl GPUDevice {
limits: Heap<*mut JSObject>,
device: webgpu::WebGPUDevice,
queue: webgpu::WebGPUQueue,
) -> DomRoot<GPUDevice> {
) -> DomRoot<Self> {
let queue = GPUQueue::new(global, channel.clone(), queue);
reflect_dom_object(
Box::new(GPUDevice::new_inherited(
@ -122,6 +121,7 @@ impl GPUDevice {
fn validate_buffer_descriptor(
&self,
descriptor: &GPUBufferDescriptor,
mapped_at_creation: bool,
) -> (bool, wgt::BufferDescriptor<std::string::String>) {
// TODO: Record a validation error in the current scope if the descriptor is invalid.
let wgpu_usage = wgt::BufferUsage::from_bits(descriptor.usage);
@ -133,6 +133,7 @@ impl GPUDevice {
wgt::BufferDescriptor {
size: descriptor.size,
usage: wgpu_usage.unwrap(),
mapped_at_creation,
label: Default::default(),
},
)
@ -142,6 +143,7 @@ impl GPUDevice {
wgt::BufferDescriptor {
size: 0,
usage: wgt::BufferUsage::empty(),
mapped_at_creation,
label: Default::default(),
},
)
@ -182,7 +184,7 @@ impl GPUDeviceMethods for GPUDevice {
/// https://gpuweb.github.io/gpuweb/#dom-gpudevice-createbuffer
fn CreateBuffer(&self, descriptor: &GPUBufferDescriptor) -> DomRoot<GPUBuffer> {
let (valid, wgpu_descriptor) = self.validate_buffer_descriptor(descriptor);
let (valid, wgpu_descriptor) = self.validate_buffer_descriptor(descriptor, false);
let id = self
.global()
.wgpu_id_hub()
@ -218,7 +220,7 @@ impl GPUDeviceMethods for GPUDevice {
cx: SafeJSContext,
descriptor: &GPUBufferDescriptor,
) -> Vec<JSVal> {
let (valid, wgpu_descriptor) = self.validate_buffer_descriptor(descriptor);
let (valid, wgpu_descriptor) = self.validate_buffer_descriptor(descriptor, true);
let buffer_id = self
.global()
.wgpu_id_hub()
@ -226,7 +228,7 @@ impl GPUDeviceMethods for GPUDevice {
.create_buffer_id(self.device.0.backend());
self.channel
.0
.send(WebGPURequest::CreateBufferMapped {
.send(WebGPURequest::CreateBuffer {
device_id: self.device.0,
buffer_id,
descriptor: wgpu_descriptor.clone(),
@ -547,7 +549,9 @@ impl GPUDeviceMethods for GPUDevice {
resource: BindingResource::Buffer(BufferBinding {
buffer: bind.resource.buffer.id().0,
offset: bind.resource.offset,
size: bind.resource.size.unwrap_or(bind.resource.buffer.size()),
size: wgt::BufferSize(
bind.resource.size.unwrap_or(bind.resource.buffer.size()),
),
}),
})
.collect::<Vec<_>>();
@ -647,7 +651,13 @@ impl GPUDeviceMethods for GPUDevice {
let encoder = webgpu::WebGPUCommandEncoder(command_encoder_id);
GPUCommandEncoder::new(&self.global(), self.channel.clone(), encoder, true)
GPUCommandEncoder::new(
&self.global(),
self.channel.clone(),
self.device,
encoder,
true,
)
}
/// https://gpuweb.github.io/gpuweb/#dom-gpudevice-createtexture
@ -724,11 +734,8 @@ impl GPUDeviceMethods for GPUDevice {
mipmap_filter: convert_filter_mode(descriptor.mipmapFilter),
lod_min_clamp: *descriptor.lodMinClamp,
lod_max_clamp: *descriptor.lodMaxClamp,
compare: if let Some(c) = descriptor.compare {
convert_compare_function(c)
} else {
wgt::CompareFunction::Undefined
},
compare: descriptor.compare.map(|c| convert_compare_function(c)),
anisotropy_clamp: None,
};
self.channel
.0

View file

@ -26,7 +26,7 @@ impl GPUPipelineLayout {
bind_group_layouts: Vec<WebGPUBindGroupLayout>,
pipeline_layout: WebGPUPipelineLayout,
valid: bool,
) -> GPUPipelineLayout {
) -> Self {
Self {
reflector_: Reflector::new(),
bind_group_layouts,
@ -41,7 +41,7 @@ impl GPUPipelineLayout {
bind_group_layouts: Vec<WebGPUBindGroupLayout>,
pipeline_layout: WebGPUPipelineLayout,
valid: bool,
) -> DomRoot<GPUPipelineLayout> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUPipelineLayout::new_inherited(
bind_group_layouts,

View file

@ -23,7 +23,7 @@ pub struct GPUQueue {
}
impl GPUQueue {
fn new_inherited(channel: WebGPU, queue: WebGPUQueue) -> GPUQueue {
fn new_inherited(channel: WebGPU, queue: WebGPUQueue) -> Self {
GPUQueue {
channel,
reflector_: Reflector::new(),
@ -32,7 +32,7 @@ impl GPUQueue {
}
}
pub fn new(global: &GlobalScope, channel: WebGPU, queue: WebGPUQueue) -> DomRoot<GPUQueue> {
pub fn new(global: &GlobalScope, channel: WebGPU, queue: WebGPUQueue) -> DomRoot<Self> {
reflect_dom_object(Box::new(GPUQueue::new_inherited(channel, queue)), global)
}
}

View file

@ -0,0 +1,273 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#![allow(unsafe_code)]
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::GPUCommandEncoderBinding::GPUColor;
use crate::dom::bindings::codegen::Bindings::GPURenderPassEncoderBinding::GPURenderPassEncoderMethods;
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubindgroup::GPUBindGroup;
use crate::dom::gpubuffer::GPUBuffer;
use crate::dom::gpucommandencoder::{GPUCommandEncoder, GPUCommandEncoderState};
use crate::dom::gpurenderpipeline::GPURenderPipeline;
use dom_struct::dom_struct;
use webgpu::{
wgpu::command::{render_ffi as wgpu_render, RawPass},
wgt, WebGPU, WebGPURequest,
};
#[dom_struct]
pub struct GPURenderPassEncoder {
reflector_: Reflector,
#[ignore_malloc_size_of = "defined in webgpu"]
channel: WebGPU,
label: DomRefCell<Option<DOMString>>,
#[ignore_malloc_size_of = "defined in wgpu-core"]
raw_pass: DomRefCell<Option<RawPass>>,
command_encoder: Dom<GPUCommandEncoder>,
}
impl GPURenderPassEncoder {
fn new_inherited(channel: WebGPU, raw_pass: RawPass, parent: &GPUCommandEncoder) -> Self {
Self {
channel,
reflector_: Reflector::new(),
label: DomRefCell::new(None),
raw_pass: DomRefCell::new(Some(raw_pass)),
command_encoder: Dom::from_ref(parent),
}
}
pub fn new(
global: &GlobalScope,
channel: WebGPU,
raw_pass: RawPass,
parent: &GPUCommandEncoder,
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPURenderPassEncoder::new_inherited(
channel, raw_pass, parent,
)),
global,
)
}
}
impl GPURenderPassEncoderMethods for GPURenderPassEncoder {
/// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
fn GetLabel(&self) -> Option<DOMString> {
self.label.borrow().clone()
}
/// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
fn SetLabel(&self, value: Option<DOMString>) {
*self.label.borrow_mut() = value;
}
/// https://gpuweb.github.io/gpuweb/#dom-gpuprogrammablepassencoder-setbindgroup
fn SetBindGroup(&self, index: u32, bind_group: &GPUBindGroup, dynamic_offsets: Vec<u32>) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_set_bind_group(
raw_pass,
index,
bind_group.id().0,
dynamic_offsets.as_ptr(),
dynamic_offsets.len(),
)
};
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-setviewport
fn SetViewport(
&self,
x: Finite<f32>,
y: Finite<f32>,
width: Finite<f32>,
height: Finite<f32>,
min_depth: Finite<f32>,
max_depth: Finite<f32>,
) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_set_viewport(
raw_pass, *x, *y, *width, *height, *min_depth, *max_depth,
)
};
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-setscissorrect
fn SetScissorRect(&self, x: u32, y: u32, width: u32, height: u32) {
if width <= 0 || height <= 0 {
return warn!("Cannot set scissor rect- width and height must greater than 0");
}
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_set_scissor_rect(raw_pass, x, y, width, height)
};
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-setblendcolor
fn SetBlendColor(&self, color: GPUColor) {
let colors = match color {
GPUColor::GPUColorDict(d) => wgt::Color {
r: *d.r,
g: *d.g,
b: *d.b,
a: *d.a,
},
GPUColor::DoubleSequence(s) => wgt::Color {
r: *s[0],
g: *s[1],
b: *s[2],
a: *s[3],
},
};
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe { wgpu_render::wgpu_render_pass_set_blend_color(raw_pass, &colors) };
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-setstencilreference
fn SetStencilReference(&self, reference: u32) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe { wgpu_render::wgpu_render_pass_set_stencil_reference(raw_pass, reference) };
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderpassencoder-endpass
fn EndPass(&self) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().take() {
let (pass_data, command_encoder_id) = unsafe { raw_pass.finish_render() };
self.channel
.0
.send(WebGPURequest::RunRenderPass {
command_encoder_id,
pass_data,
})
.unwrap();
self.command_encoder.set_state(
GPUCommandEncoderState::Open,
GPUCommandEncoderState::EncodingRenderPass,
);
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderencoderbase-setpipeline
fn SetPipeline(&self, pipeline: &GPURenderPipeline) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe { wgpu_render::wgpu_render_pass_set_pipeline(raw_pass, pipeline.id().0) };
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderencoderbase-setindexbuffer
fn SetIndexBuffer(&self, buffer: &GPUBuffer, offset: u64, size: u64) {
let s = if size == 0 {
wgt::BufferSize::WHOLE
} else {
wgt::BufferSize(size)
};
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_set_index_buffer(raw_pass, buffer.id().0, offset, s)
};
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderencoderbase-setvertexbuffer
fn SetVertexBuffer(&self, slot: u32, buffer: &GPUBuffer, offset: u64, size: u64) {
let s = if size == 0 {
wgt::BufferSize::WHOLE
} else {
wgt::BufferSize(size)
};
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_set_vertex_buffer(
raw_pass,
slot,
buffer.id().0,
offset,
s,
)
};
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderencoderbase-draw
fn Draw(&self, vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_draw(
raw_pass,
vertex_count,
instance_count,
first_vertex,
first_instance,
)
};
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderencoderbase-drawindexed
fn DrawIndexed(
&self,
index_count: u32,
instance_count: u32,
first_index: u32,
base_vertex: i32,
first_instance: u32,
) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_draw_indexed(
raw_pass,
index_count,
instance_count,
first_index,
base_vertex,
first_instance,
)
};
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderencoderbase-drawindirect
fn DrawIndirect(&self, indirect_buffer: &GPUBuffer, indirect_offset: u64) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_draw_indirect(
raw_pass,
indirect_buffer.id().0,
indirect_offset,
)
};
}
}
/// https://gpuweb.github.io/gpuweb/#dom-gpurenderencoderbase-drawindexedindirect
fn DrawIndexedIndirect(&self, indirect_buffer: &GPUBuffer, indirect_offset: u64) {
if let Some(raw_pass) = self.raw_pass.borrow_mut().as_mut() {
unsafe {
wgpu_render::wgpu_render_pass_draw_indexed_indirect(
raw_pass,
indirect_buffer.id().0,
indirect_offset,
)
};
}
}
}

View file

@ -27,7 +27,7 @@ impl GPURenderPipeline {
render_pipeline: WebGPURenderPipeline,
device: WebGPUDevice,
valid: bool,
) -> GPURenderPipeline {
) -> Self {
Self {
reflector_: Reflector::new(),
label: DomRefCell::new(None),
@ -42,7 +42,7 @@ impl GPURenderPipeline {
render_pipeline: WebGPURenderPipeline,
device: WebGPUDevice,
valid: bool,
) -> DomRoot<GPURenderPipeline> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPURenderPipeline::new_inherited(
render_pipeline,
@ -54,6 +54,12 @@ impl GPURenderPipeline {
}
}
impl GPURenderPipeline {
pub fn id(&self) -> WebGPURenderPipeline {
self.render_pipeline
}
}
impl GPURenderPipelineMethods for GPURenderPipeline {
/// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
fn GetLabel(&self) -> Option<DOMString> {

View file

@ -31,7 +31,7 @@ impl GPUSampler {
compare_enable: bool,
sampler: WebGPUSampler,
valid: bool,
) -> GPUSampler {
) -> Self {
Self {
reflector_: Reflector::new(),
channel,
@ -50,7 +50,7 @@ impl GPUSampler {
compare_enable: bool,
sampler: WebGPUSampler,
valid: bool,
) -> DomRoot<GPUSampler> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUSampler::new_inherited(
channel,

View file

@ -19,7 +19,7 @@ pub struct GPUShaderModule {
}
impl GPUShaderModule {
fn new_inherited(shader_module: WebGPUShaderModule) -> GPUShaderModule {
fn new_inherited(shader_module: WebGPUShaderModule) -> Self {
Self {
reflector_: Reflector::new(),
label: DomRefCell::new(None),
@ -27,10 +27,7 @@ impl GPUShaderModule {
}
}
pub fn new(
global: &GlobalScope,
shader_module: WebGPUShaderModule,
) -> DomRoot<GPUShaderModule> {
pub fn new(global: &GlobalScope, shader_module: WebGPUShaderModule) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUShaderModule::new_inherited(shader_module)),
global,

View file

@ -49,7 +49,7 @@ impl GPUTexture {
format: GPUTextureFormat,
texture_usage: u32,
valid: bool,
) -> GPUTexture {
) -> Self {
Self {
reflector_: Reflector::new(),
texture,
@ -78,7 +78,7 @@ impl GPUTexture {
format: GPUTextureFormat,
texture_usage: u32,
valid: bool,
) -> DomRoot<GPUTexture> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUTexture::new_inherited(
texture,

View file

@ -22,11 +22,7 @@ pub struct GPUTextureView {
}
impl GPUTextureView {
fn new_inherited(
texture_view: WebGPUTextureView,
device: WebGPUDevice,
valid: bool,
) -> GPUTextureView {
fn new_inherited(texture_view: WebGPUTextureView, device: WebGPUDevice, valid: bool) -> Self {
Self {
reflector_: Reflector::new(),
device,
@ -41,7 +37,7 @@ impl GPUTextureView {
texture_view: WebGPUTextureView,
device: WebGPUDevice,
valid: bool,
) -> DomRoot<GPUTextureView> {
) -> DomRoot<Self> {
reflect_dom_object(
Box::new(GPUTextureView::new_inherited(texture_view, device, valid)),
global,
@ -49,6 +45,12 @@ impl GPUTextureView {
}
}
impl GPUTextureView {
pub fn id(&self) -> WebGPUTextureView {
self.texture_view
}
}
impl GPUTextureViewMethods for GPUTextureView {
/// https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label
fn GetLabel(&self) -> Option<DOMString> {

View file

@ -333,6 +333,7 @@ pub mod gpucomputepipeline;
pub mod gpudevice;
pub mod gpupipelinelayout;
pub mod gpuqueue;
pub mod gpurenderpassencoder;
pub mod gpurenderpipeline;
pub mod gpusampler;
pub mod gpushadermodule;

View file

@ -14,12 +14,13 @@ interface GPUAdapter {
};
dictionary GPUDeviceDescriptor : GPUObjectDescriptorBase {
GPUExtensions extensions = {};
sequence<GPUExtensionName> extensions = [];
GPULimits limits = {};
};
dictionary GPUExtensions {
boolean anisotropicFiltering = false;
enum GPUExtensionName {
"texture-compression-bc",
"pipeline-statistics-query"
};
dictionary GPULimits {

View file

@ -5,7 +5,7 @@
// https://gpuweb.github.io/gpuweb/#gpucommandencoder
[Exposed=(Window, DedicatedWorker), Serializable, Pref="dom.webgpu.enabled"]
interface GPUCommandEncoder {
// GPURenderPassEncoder beginRenderPass(GPURenderPassDescriptor descriptor);
GPURenderPassEncoder beginRenderPass(GPURenderPassDescriptor descriptor);
GPUComputePassEncoder beginComputePass(optional GPUComputePassDescriptor descriptor = {});
void copyBufferToBuffer(
@ -43,3 +43,48 @@ dictionary GPUComputePassDescriptor : GPUObjectDescriptorBase {
dictionary GPUCommandBufferDescriptor : GPUObjectDescriptorBase {
};
dictionary GPURenderPassDescriptor : GPUObjectDescriptorBase {
required sequence<GPURenderPassColorAttachmentDescriptor> colorAttachments;
GPURenderPassDepthStencilAttachmentDescriptor depthStencilAttachment;
//GPUQuerySet occlusionQuerySet;
};
dictionary GPURenderPassColorAttachmentDescriptor {
required GPUTextureView attachment;
GPUTextureView resolveTarget;
required (GPULoadOp or GPUColor) loadValue;
GPUStoreOp storeOp = "store";
};
dictionary GPURenderPassDepthStencilAttachmentDescriptor {
required GPUTextureView attachment;
required (GPULoadOp or float) depthLoadValue;
required GPUStoreOp depthStoreOp;
boolean depthReadOnly = false;
required GPUStencilLoadValue stencilLoadValue;
required GPUStoreOp stencilStoreOp;
boolean stencilReadOnly = false;
};
typedef (GPULoadOp or GPUStencilValue) GPUStencilLoadValue;
enum GPULoadOp {
"load"
};
enum GPUStoreOp {
"store",
"clear"
};
dictionary GPUColorDict {
required double r;
required double g;
required double b;
required double a;
};
typedef (sequence<double> or GPUColorDict) GPUColor;

View file

@ -0,0 +1,24 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// https://gpuweb.github.io/gpuweb/#gpurenderencoderbase
[Exposed=(Window, DedicatedWorker)]
interface mixin GPURenderEncoderBase {
void setPipeline(GPURenderPipeline pipeline);
void setIndexBuffer(GPUBuffer buffer, optional GPUSize64 offset = 0, optional GPUSize64 size = 0);
void setVertexBuffer(GPUIndex32 slot, GPUBuffer buffer, optional GPUSize64 offset = 0, optional GPUSize64 size = 0);
void draw(GPUSize32 vertexCount, optional GPUSize32 instanceCount = 1,
optional GPUSize32 firstVertex = 0, optional GPUSize32 firstInstance = 0);
void drawIndexed(GPUSize32 indexCount, optional GPUSize32 instanceCount = 1,
optional GPUSize32 firstIndex = 0,
optional GPUSignedOffset32 baseVertex = 0,
optional GPUSize32 firstInstance = 0);
void drawIndirect(GPUBuffer indirectBuffer, GPUSize64 indirectOffset);
void drawIndexedIndirect(GPUBuffer indirectBuffer, GPUSize64 indirectOffset);
};
typedef [EnforceRange] long GPUSignedOffset32;

View file

@ -0,0 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
// https://gpuweb.github.io/gpuweb/#gpurenderpassencoder
[Exposed=(Window, DedicatedWorker), Pref="dom.webgpu.enabled"]
interface GPURenderPassEncoder {
void setViewport(float x, float y,
float width, float height,
float minDepth, float maxDepth);
void setScissorRect(GPUIntegerCoordinate x, GPUIntegerCoordinate y,
GPUIntegerCoordinate width, GPUIntegerCoordinate height);
void setBlendColor(GPUColor color);
void setStencilReference(GPUStencilValue reference);
//void beginOcclusionQuery(GPUSize32 queryIndex);
//void endOcclusionQuery(GPUSize32 queryIndex);
//void executeBundles(sequence<GPURenderBundle> bundles);
void endPass();
};
GPURenderPassEncoder includes GPUObjectBase;
GPURenderPassEncoder includes GPUProgrammablePassEncoder;
GPURenderPassEncoder includes GPURenderEncoderBase;

View file

@ -74,11 +74,6 @@ pub enum WebGPURequest {
buffer_id: id::BufferId,
descriptor: wgt::BufferDescriptor<String>,
},
CreateBufferMapped {
device_id: id::DeviceId,
buffer_id: id::BufferId,
descriptor: wgt::BufferDescriptor<String>,
},
CreateCommandEncoder {
device_id: id::DeviceId,
// TODO(zakorgy): Serialize CommandEncoderDescriptor in wgpu-core
@ -155,6 +150,10 @@ pub enum WebGPURequest {
command_encoder_id: id::CommandEncoderId,
pass_data: Vec<u8>,
},
RunRenderPass {
command_encoder_id: id::CommandEncoderId,
pass_data: Vec<u8>,
},
Submit {
queue_id: id::QueueId,
command_buffers: Vec<id::CommandBufferId>,
@ -311,26 +310,9 @@ impl WGPU {
descriptor,
} => {
let global = &self.global;
let desc = wgt::BufferDescriptor {
size: descriptor.size,
usage: descriptor.usage,
label: ptr::null(),
};
let _ = gfx_select!(buffer_id => global.device_create_buffer(device_id, &desc, buffer_id));
},
WebGPURequest::CreateBufferMapped {
device_id,
buffer_id,
descriptor,
} => {
let global = &self.global;
let desc = wgt::BufferDescriptor {
size: descriptor.size,
usage: descriptor.usage,
label: ptr::null(),
};
let st = CString::new(descriptor.label.as_bytes()).unwrap();
let _ = gfx_select!(buffer_id =>
global.device_create_buffer_mapped(device_id, &desc, buffer_id));
global.device_create_buffer(device_id, &descriptor.map_label(|_| st.as_ptr()), buffer_id));
},
WebGPURequest::CreateCommandEncoder {
device_id,
@ -586,6 +568,16 @@ impl WGPU {
&pass_data
));
},
WebGPURequest::RunRenderPass {
command_encoder_id,
pass_data,
} => {
let global = &self.global;
gfx_select!(command_encoder_id => global.command_encoder_run_render_pass(
command_encoder_id,
&pass_data
));
},
WebGPURequest::Submit {
queue_id,
command_buffers,