Skip to main content

webgpu_traits/messages/
recv.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! IPC messages that are received in the WebGPU thread
6//! (usually from the ScriptThread, and more specifically from DOM objects)
7
8use arrayvec::ArrayVec;
9use pixels::{SharedSnapshot, SnapshotPixelFormat};
10use serde::{Deserialize, Serialize};
11use servo_base::Epoch;
12use servo_base::generic_channel::{
13    GenericCallback, GenericOneshotSender, GenericSender, GenericSharedMemory,
14};
15use servo_base::id::PipelineId;
16use webrender_api::ImageKey;
17use webrender_api::euclid::default::Size2D;
18use webrender_api::units::DeviceIntSize;
19use wgpu_core::Label;
20use wgpu_core::binding_model::{
21    BindGroupDescriptor, BindGroupLayoutDescriptor, PipelineLayoutDescriptor,
22};
23use wgpu_core::command::{
24    PassTimestampWrites, RenderBundleDescriptor, RenderBundleEncoder, RenderPassColorAttachment,
25    RenderPassDepthStencilAttachment, TexelCopyBufferInfo, TexelCopyTextureInfo,
26};
27use wgpu_core::device::HostMap;
28pub use wgpu_core::id::markers::{
29    ComputePassEncoder as ComputePass, RenderPassEncoder as RenderPass,
30};
31use wgpu_core::id::{
32    AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandBufferId, CommandEncoderId,
33    ComputePassEncoderId, ComputePipelineId, DeviceId, ExternalTextureId, PipelineLayoutId,
34    QuerySetId, QueueId, RenderBundleId, RenderPassEncoderId, RenderPipelineId, SamplerId,
35    ShaderModuleId, TextureId, TextureViewId,
36};
37pub use wgpu_core::id::{
38    ComputePassEncoderId as ComputePassId, RenderPassEncoderId as RenderPassId,
39};
40use wgpu_core::instance::RequestAdapterOptions;
41use wgpu_core::pipeline::{ComputePipelineDescriptor, RenderPipelineDescriptor};
42use wgpu_core::resource::{
43    BufferAccessError, BufferDescriptor, QuerySetDescriptor, SamplerDescriptor, TextureDescriptor,
44    TextureViewDescriptor,
45};
46use wgpu_types::{
47    BufferAddress, CommandBufferDescriptor, CommandEncoderDescriptor, DeviceDescriptor, Extent3d,
48    TexelCopyBufferLayout,
49};
50
51use crate::{
52    ContextConfiguration, Error, ErrorFilter, Mapping, PRESENTATION_BUFFER_COUNT, RenderCommand,
53    ShaderCompilationInfo, WebGPUAdapter, WebGPUAdapterResponse, WebGPUComputePipelineResponse,
54    WebGPUContextId, WebGPUDeviceResponse, WebGPUPoppedErrorScopeResponse,
55    WebGPURenderPipelineResponse,
56};
57
58#[derive(Debug, Deserialize, Serialize)]
59pub struct PendingTexture {
60    pub texture_id: TextureId,
61    pub encoder_id: CommandEncoderId,
62    pub command_buffer_id: CommandBufferId,
63    pub configuration: ContextConfiguration,
64}
65
66#[derive(Debug, Deserialize, Serialize)]
67pub enum WebGPURequest {
68    SetImageKey {
69        context_id: WebGPUContextId,
70        image_key: ImageKey,
71    },
72    BufferMapAsync {
73        callback: GenericCallback<Result<Mapping, BufferAccessError>>,
74        buffer_id: BufferId,
75        device_id: DeviceId,
76        host_map: HostMap,
77        offset: u64,
78        size: Option<u64>,
79    },
80    CommandEncoderFinish {
81        command_encoder_id: CommandEncoderId,
82        device_id: DeviceId,
83        desc: CommandBufferDescriptor<Label<'static>>,
84        command_buffer_id: CommandBufferId,
85    },
86    CopyBufferToBuffer {
87        device_id: DeviceId,
88        command_encoder_id: CommandEncoderId,
89        source_id: BufferId,
90        source_offset: BufferAddress,
91        destination_id: BufferId,
92        destination_offset: BufferAddress,
93        size: BufferAddress,
94    },
95    CopyBufferToTexture {
96        device_id: DeviceId,
97        command_encoder_id: CommandEncoderId,
98        source: TexelCopyBufferInfo,
99        destination: TexelCopyTextureInfo,
100        copy_size: Extent3d,
101    },
102    CopyTextureToBuffer {
103        device_id: DeviceId,
104        command_encoder_id: CommandEncoderId,
105        source: TexelCopyTextureInfo,
106        destination: TexelCopyBufferInfo,
107        copy_size: Extent3d,
108    },
109    CopyTextureToTexture {
110        device_id: DeviceId,
111        command_encoder_id: CommandEncoderId,
112        source: TexelCopyTextureInfo,
113        destination: TexelCopyTextureInfo,
114        copy_size: Extent3d,
115    },
116    CopyExternalImageToTexture {
117        device_id: DeviceId,
118        queue_id: QueueId,
119        usable_source: Option<SharedSnapshot>,
120        destination: TexelCopyTextureInfo,
121        dest_tex_descriptor: TextureDescriptor<'static>,
122        copy_size: Extent3d,
123    },
124    CommandEncoderPushDebugGroup {
125        device_id: DeviceId,
126        command_encoder_id: CommandEncoderId,
127        label: String,
128    },
129    CommandEncoderPopDebugGroup {
130        device_id: DeviceId,
131        command_encoder_id: CommandEncoderId,
132    },
133    CommandEncoderInsertDebugMarker {
134        device_id: DeviceId,
135        command_encoder_id: CommandEncoderId,
136        label: String,
137    },
138    CreateBindGroup {
139        device_id: DeviceId,
140        bind_group_id: BindGroupId,
141        descriptor: BindGroupDescriptor<'static>,
142    },
143    CreateBindGroupLayout {
144        device_id: DeviceId,
145        bind_group_layout_id: BindGroupLayoutId,
146        descriptor: Option<BindGroupLayoutDescriptor<'static>>,
147    },
148    CreateBuffer {
149        device_id: DeviceId,
150        buffer_id: BufferId,
151        descriptor: BufferDescriptor<'static>,
152    },
153    CreateCommandEncoder {
154        device_id: DeviceId,
155        command_encoder_id: CommandEncoderId,
156        desc: CommandEncoderDescriptor<Label<'static>>,
157    },
158    CreateComputePipeline {
159        device_id: DeviceId,
160        compute_pipeline_id: ComputePipelineId,
161        descriptor: ComputePipelineDescriptor<'static>,
162        /// present only on ASYNC versions
163        async_sender: Option<GenericCallback<WebGPUComputePipelineResponse>>,
164    },
165    CreatePipelineLayout {
166        device_id: DeviceId,
167        pipeline_layout_id: PipelineLayoutId,
168        descriptor: PipelineLayoutDescriptor<'static>,
169    },
170    CreateRenderPipeline {
171        device_id: DeviceId,
172        render_pipeline_id: RenderPipelineId,
173        descriptor: RenderPipelineDescriptor<'static>,
174        /// present only on ASYNC versions
175        async_sender: Option<GenericCallback<WebGPURenderPipelineResponse>>,
176    },
177    CreateSampler {
178        device_id: DeviceId,
179        sampler_id: SamplerId,
180        descriptor: SamplerDescriptor<'static>,
181    },
182    CreateShaderModule {
183        device_id: DeviceId,
184        program_id: ShaderModuleId,
185        program: String,
186        label: Option<String>,
187        callback: GenericCallback<Option<ShaderCompilationInfo>>,
188    },
189    /// Creates context
190    CreateContext {
191        buffer_ids: ArrayVec<BufferId, PRESENTATION_BUFFER_COUNT>,
192        size: DeviceIntSize,
193        sender: GenericSender<WebGPUContextId>,
194    },
195    /// Present texture to WebRender
196    Present {
197        context_id: WebGPUContextId,
198        pending_texture: Option<PendingTexture>,
199        size: Size2D<u32>,
200        canvas_epoch: Epoch,
201    },
202    /// Create [`pixels::Snapshot`] with contents of the last present operation
203    /// or provided pending texture and send it over provided [`IpcSender`].
204    GetImage {
205        context_id: WebGPUContextId,
206        pending_texture: Option<PendingTexture>,
207        sender: GenericSender<SharedSnapshot>,
208    },
209    ValidateTextureDescriptor {
210        device_id: DeviceId,
211        texture_id: TextureId,
212        descriptor: TextureDescriptor<'static>,
213    },
214    DestroyContext {
215        context_id: WebGPUContextId,
216    },
217    CreateTexture {
218        device_id: DeviceId,
219        texture_id: TextureId,
220        descriptor: TextureDescriptor<'static>,
221    },
222    CreateTextureView {
223        texture_id: TextureId,
224        texture_view_id: TextureViewId,
225        device_id: DeviceId,
226        descriptor: Option<TextureViewDescriptor<'static>>,
227    },
228    DestroyBuffer(BufferId),
229    DestroyDevice(DeviceId),
230    DestroyTexture(TextureId),
231    DropTexture(TextureId),
232    DropAdapter(AdapterId),
233    DropDevice(DeviceId),
234    DropBuffer(BufferId),
235    DropPipelineLayout(PipelineLayoutId),
236    DropComputePipeline(ComputePipelineId),
237    DropRenderPipeline(RenderPipelineId),
238    DropBindGroup(BindGroupId),
239    DropBindGroupLayout(BindGroupLayoutId),
240    DropCommandEncoder(CommandEncoderId),
241    DropCommandBuffer(CommandBufferId),
242    DropTextureView(TextureViewId),
243    DropSampler(SamplerId),
244    DropShaderModule(ShaderModuleId),
245    DropRenderBundle(RenderBundleId),
246    DropQuerySet(QuerySetId),
247    DropComputePass(ComputePassEncoderId),
248    DropRenderPass(RenderPassEncoderId),
249    Exit(GenericOneshotSender<()>),
250    RenderBundleEncoderFinish {
251        render_bundle_encoder: RenderBundleEncoder,
252        descriptor: RenderBundleDescriptor<'static>,
253        render_bundle_id: RenderBundleId,
254        device_id: DeviceId,
255    },
256    RequestAdapter {
257        sender: GenericCallback<WebGPUAdapterResponse>,
258        options: RequestAdapterOptions,
259        adapter_id: AdapterId,
260    },
261    RequestDevice {
262        sender: GenericCallback<WebGPUDeviceResponse>,
263        adapter_id: WebGPUAdapter,
264        descriptor: DeviceDescriptor<Option<String>>,
265        device_id: DeviceId,
266        queue_id: QueueId,
267        pipeline_id: PipelineId,
268    },
269    // Compute Pass
270    BeginComputePass {
271        command_encoder_id: CommandEncoderId,
272        compute_pass_id: ComputePassId,
273        label: Label<'static>,
274        timestamp_writes: Option<PassTimestampWrites>,
275        device_id: DeviceId,
276    },
277    ComputePassSetPipeline {
278        compute_pass_id: ComputePassId,
279        pipeline_id: ComputePipelineId,
280        device_id: DeviceId,
281    },
282    ComputePassSetBindGroup {
283        compute_pass_id: ComputePassId,
284        index: u32,
285        bind_group_id: BindGroupId,
286        offsets: Vec<u32>,
287        device_id: DeviceId,
288    },
289    ComputePassDispatchWorkgroups {
290        compute_pass_id: ComputePassId,
291        x: u32,
292        y: u32,
293        z: u32,
294        device_id: DeviceId,
295    },
296    ComputePassDispatchWorkgroupsIndirect {
297        compute_pass_id: ComputePassId,
298        buffer_id: BufferId,
299        offset: u64,
300        device_id: DeviceId,
301    },
302    ComputePassPushDebugGroup {
303        compute_pass_id: ComputePassId,
304        label: String,
305        device_id: DeviceId,
306    },
307    ComputePassPopDebugGroup {
308        compute_pass_id: ComputePassId,
309        device_id: DeviceId,
310    },
311    ComputePassInsertDebugMarker {
312        compute_pass_id: ComputePassId,
313        label: String,
314        device_id: DeviceId,
315    },
316    EndComputePass {
317        compute_pass_id: ComputePassId,
318        device_id: DeviceId,
319    },
320    // Render Pass
321    BeginRenderPass {
322        command_encoder_id: CommandEncoderId,
323        render_pass_id: RenderPassId,
324        label: Label<'static>,
325        color_attachments: Vec<Option<RenderPassColorAttachment>>,
326        depth_stencil_attachment: Option<RenderPassDepthStencilAttachment<TextureViewId>>,
327        timestamp_writes: Option<PassTimestampWrites>,
328        device_id: DeviceId,
329    },
330    RenderPassCommand {
331        render_pass_id: RenderPassId,
332        render_command: RenderCommand,
333        device_id: DeviceId,
334    },
335    EndRenderPass {
336        render_pass_id: RenderPassId,
337        device_id: DeviceId,
338    },
339    Submit {
340        device_id: DeviceId,
341        queue_id: QueueId,
342        command_buffers: Vec<CommandBufferId>,
343    },
344    UnmapBuffer {
345        buffer_id: BufferId,
346        /// Return back mapping for writeback
347        mapping: Option<Mapping>,
348    },
349    WriteBuffer {
350        device_id: DeviceId,
351        queue_id: QueueId,
352        buffer_id: BufferId,
353        buffer_offset: u64,
354        data: GenericSharedMemory,
355    },
356    WriteTexture {
357        device_id: DeviceId,
358        queue_id: QueueId,
359        texture_cv: TexelCopyTextureInfo,
360        data_layout: TexelCopyBufferLayout,
361        size: Extent3d,
362        data: GenericSharedMemory,
363    },
364    QueueOnSubmittedWorkDone {
365        sender: GenericCallback<()>,
366        queue_id: QueueId,
367    },
368    PushErrorScope {
369        device_id: DeviceId,
370        filter: ErrorFilter,
371    },
372    DispatchError {
373        device_id: DeviceId,
374        error: Error,
375    },
376    PopErrorScope {
377        device_id: DeviceId,
378        callback: GenericCallback<WebGPUPoppedErrorScopeResponse>,
379    },
380    ComputeGetBindGroupLayout {
381        device_id: DeviceId,
382        pipeline_id: ComputePipelineId,
383        index: u32,
384        id: BindGroupLayoutId,
385    },
386    RenderGetBindGroupLayout {
387        device_id: DeviceId,
388        pipeline_id: RenderPipelineId,
389        index: u32,
390        id: BindGroupLayoutId,
391    },
392    CreateQuerySet {
393        device_id: DeviceId,
394        query_set_id: QuerySetId,
395        descriptor: QuerySetDescriptor<'static>,
396    },
397    ResolveQuerySet {
398        device_id: DeviceId,
399        command_encoder_id: CommandEncoderId,
400        query_set_id: QuerySetId,
401        start_query: u32,
402        query_count: u32,
403        destination: BufferId,
404        destination_offset: u64,
405    },
406    /// Create planar texture and view to be imported as external texture
407    CreatePlanarTexture {
408        device_id: DeviceId,
409        size: Size2D<u32>,
410        format: SnapshotPixelFormat,
411        texture_id: TextureId,
412        /// aka plane
413        texture_view_id: TextureViewId,
414    },
415    UpdatePlanarTexture {
416        device_id: DeviceId,
417        queue_id: QueueId,
418        texture_id: TextureId,
419        snapshot: SharedSnapshot,
420    },
421    DropPlanarTexture(TextureId, TextureViewId),
422    /// Import plane as external texture, if plane not provided it creates invalid external texture
423    ImportExternalTexture {
424        device_id: DeviceId,
425        external_texture_id: ExternalTextureId,
426        label: String,
427        size: Size2D<u32>,
428        plane0: Option<TextureViewId>,
429    },
430    DestroyExternalTexture(ExternalTextureId),
431    DropExternalTexture(ExternalTextureId),
432}