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