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;
19
20use crate::id::*;
21use crate::{
22    BindGroupDescriptor, BindGroupLayoutDescriptor, BufferAccessError, BufferDescriptor,
23    BufferUpdate, CommandBufferDescriptor, CommandEncoderCommand, CommandEncoderDescriptor,
24    ComputePassEncoderCommand, ComputePipelineDescriptor, ContextConfiguration, DeviceDescriptor,
25    Error, ErrorFilter, Extent3d, HostMap, Label, Mapping, PRESENTATION_BUFFER_COUNT,
26    PassTimestampWrites, PipelineLayoutDescriptor, QuerySetDescriptor, RenderBundleDescriptor,
27    RenderBundleEncoderCommand, RenderBundleEncoderDescriptor, RenderPassColorAttachment,
28    RenderPassDepthStencilAttachment, RenderPassEncoderCommand, RenderPipelineDescriptor,
29    RequestAdapterOptions, SamplerDescriptor, ShaderCompilationInfo, TexelCopyBufferLayout,
30    TexelCopyTextureInfo, TextureDescriptor, TextureViewDescriptor, WebGPUAdapter,
31    WebGPUAdapterResponse, WebGPUComputePipelineResponse, WebGPUContextId, WebGPUDeviceResponse,
32    WebGPUPoppedErrorScopeResponse, WebGPURenderPipelineResponse,
33};
34
35#[derive(Debug, Deserialize, Serialize)]
36pub struct PendingTexture {
37    pub texture_id: TextureId,
38    pub encoder_id: CommandEncoderId,
39    pub command_buffer_id: CommandBufferId,
40    pub configuration: ContextConfiguration,
41}
42
43#[derive(Debug, Deserialize, Serialize)]
44pub enum WebGPURequest {
45    SetImageKey {
46        context_id: WebGPUContextId,
47        image_key: ImageKey,
48    },
49    BufferMapAsync {
50        callback: GenericCallback<Result<Mapping, BufferAccessError>>,
51        buffer_id: BufferId,
52        device_id: DeviceId,
53        host_map: HostMap,
54        offset: u64,
55        size: Option<u64>,
56        buffer_size: u64,
57    },
58    CommandEncoderFinish {
59        command_encoder_id: CommandEncoderId,
60        device_id: DeviceId,
61        desc: CommandBufferDescriptor<Label<'static>>,
62        command_buffer_id: CommandBufferId,
63    },
64    CommandEncoderCommand {
65        command_encoder_id: CommandEncoderId,
66        command: CommandEncoderCommand,
67        device_id: DeviceId,
68    },
69    CopyExternalImageToTexture {
70        device_id: DeviceId,
71        queue_id: QueueId,
72        usable_source: Option<SharedSnapshot>,
73        destination: TexelCopyTextureInfo,
74        dest_tex_descriptor: TextureDescriptor<'static>,
75        copy_size: Extent3d,
76    },
77    CreateBindGroup {
78        device_id: DeviceId,
79        bind_group_id: BindGroupId,
80        descriptor: BindGroupDescriptor<'static>,
81    },
82    CreateBindGroupLayout {
83        device_id: DeviceId,
84        bind_group_layout_id: BindGroupLayoutId,
85        descriptor: Option<BindGroupLayoutDescriptor<'static>>,
86    },
87    CreateBuffer {
88        device_id: DeviceId,
89        buffer_id: BufferId,
90        descriptor: BufferDescriptor<'static>,
91    },
92    CreateCommandEncoder {
93        device_id: DeviceId,
94        command_encoder_id: CommandEncoderId,
95        desc: CommandEncoderDescriptor<Label<'static>>,
96    },
97    CreateComputePipeline {
98        device_id: DeviceId,
99        compute_pipeline_id: ComputePipelineId,
100        descriptor: ComputePipelineDescriptor<'static>,
101        /// present only on ASYNC versions
102        async_sender: Option<GenericCallback<WebGPUComputePipelineResponse>>,
103    },
104    CreatePipelineLayout {
105        device_id: DeviceId,
106        pipeline_layout_id: PipelineLayoutId,
107        descriptor: PipelineLayoutDescriptor<'static>,
108    },
109    CreateRenderPipeline {
110        device_id: DeviceId,
111        render_pipeline_id: RenderPipelineId,
112        descriptor: RenderPipelineDescriptor<'static>,
113        /// present only on ASYNC versions
114        async_sender: Option<GenericCallback<WebGPURenderPipelineResponse>>,
115    },
116    CreateSampler {
117        device_id: DeviceId,
118        sampler_id: SamplerId,
119        descriptor: SamplerDescriptor<'static>,
120    },
121    CreateShaderModule {
122        device_id: DeviceId,
123        program_id: ShaderModuleId,
124        program: String,
125        label: Option<String>,
126        callback: GenericCallback<Option<ShaderCompilationInfo>>,
127    },
128    /// Creates context
129    CreateContext {
130        buffer_ids: ArrayVec<BufferId, PRESENTATION_BUFFER_COUNT>,
131        size: DeviceIntSize,
132        sender: GenericSender<WebGPUContextId>,
133    },
134    /// Present texture to WebRender
135    Present {
136        context_id: WebGPUContextId,
137        pending_texture: Option<PendingTexture>,
138        size: Size2D<u32>,
139        canvas_epoch: Epoch,
140    },
141    /// Create [`pixels::Snapshot`] with contents of the last present operation
142    /// or provided pending texture and send it over provided [`IpcSender`].
143    GetImage {
144        context_id: WebGPUContextId,
145        pending_texture: Option<PendingTexture>,
146        sender: GenericSender<SharedSnapshot>,
147    },
148    ValidateTextureDescriptor {
149        device_id: DeviceId,
150        texture_id: TextureId,
151        descriptor: TextureDescriptor<'static>,
152    },
153    DestroyContext {
154        context_id: WebGPUContextId,
155    },
156    CreateTexture {
157        device_id: DeviceId,
158        texture_id: TextureId,
159        descriptor: TextureDescriptor<'static>,
160    },
161    CreateTextureView {
162        texture_id: TextureId,
163        texture_view_id: TextureViewId,
164        device_id: DeviceId,
165        descriptor: Option<TextureViewDescriptor<'static>>,
166    },
167    DestroyBuffer(BufferId),
168    DestroyDevice(DeviceId),
169    DestroyTexture(TextureId),
170    DropTexture(TextureId),
171    DropAdapter(AdapterId),
172    DropDevice(DeviceId),
173    DropBuffer(BufferId),
174    DropPipelineLayout(PipelineLayoutId),
175    DropComputePipeline(ComputePipelineId),
176    DropRenderPipeline(RenderPipelineId),
177    DropBindGroup(BindGroupId),
178    DropBindGroupLayout(BindGroupLayoutId),
179    DropCommandEncoder(CommandEncoderId),
180    DropCommandBuffer(CommandBufferId),
181    DropTextureView(TextureViewId),
182    DropSampler(SamplerId),
183    DropShaderModule(ShaderModuleId),
184    DropRenderBundle(RenderBundleId),
185    DropQuerySet(QuerySetId),
186    DropComputePass(ComputePassEncoderId),
187    DropRenderPass(RenderPassEncoderId),
188    Exit(GenericOneshotSender<()>),
189    RenderBundleEncoderFinish {
190        render_bundle_encoder_id: RenderBundleEncoderId,
191        descriptor: RenderBundleDescriptor<'static>,
192        render_bundle_id: RenderBundleId,
193        device_id: DeviceId,
194    },
195    RequestAdapter {
196        sender: GenericCallback<WebGPUAdapterResponse>,
197        options: RequestAdapterOptions,
198        adapter_id: AdapterId,
199    },
200    RequestDevice {
201        sender: GenericCallback<WebGPUDeviceResponse>,
202        adapter_id: WebGPUAdapter,
203        descriptor: DeviceDescriptor<Option<String>>,
204        device_id: DeviceId,
205        queue_id: QueueId,
206        pipeline_id: PipelineId,
207    },
208    // Compute Pass
209    BeginComputePass {
210        command_encoder_id: CommandEncoderId,
211        compute_pass_id: ComputePassEncoderId,
212        label: Label<'static>,
213        timestamp_writes: Option<PassTimestampWrites>,
214        device_id: DeviceId,
215    },
216    ComputePassCommand {
217        compute_pass_id: ComputePassEncoderId,
218        compute_command: ComputePassEncoderCommand,
219        device_id: DeviceId,
220    },
221    EndComputePass {
222        compute_pass_id: ComputePassEncoderId,
223        device_id: DeviceId,
224    },
225    // Render Pass
226    BeginRenderPass {
227        command_encoder_id: CommandEncoderId,
228        render_pass_id: RenderPassEncoderId,
229        label: Label<'static>,
230        color_attachments: Vec<Option<RenderPassColorAttachment>>,
231        depth_stencil_attachment: Option<RenderPassDepthStencilAttachment<TextureViewId>>,
232        timestamp_writes: Option<PassTimestampWrites>,
233        device_id: DeviceId,
234    },
235    RenderPassCommand {
236        render_pass_id: RenderPassEncoderId,
237        render_command: RenderPassEncoderCommand,
238        device_id: DeviceId,
239    },
240    EndRenderPass {
241        render_pass_id: RenderPassEncoderId,
242        device_id: DeviceId,
243    },
244    Submit {
245        device_id: DeviceId,
246        queue_id: QueueId,
247        command_buffers: Vec<CommandBufferId>,
248    },
249    UnmapBuffer {
250        buffer_id: BufferId,
251        buffer_update: BufferUpdate,
252    },
253    WriteBuffer {
254        device_id: DeviceId,
255        queue_id: QueueId,
256        buffer_id: BufferId,
257        buffer_offset: u64,
258        data: GenericSharedMemory,
259    },
260    WriteTexture {
261        device_id: DeviceId,
262        queue_id: QueueId,
263        texture_cv: TexelCopyTextureInfo,
264        data_layout: TexelCopyBufferLayout,
265        size: Extent3d,
266        data: GenericSharedMemory,
267    },
268    QueueOnSubmittedWorkDone {
269        sender: GenericCallback<()>,
270        queue_id: QueueId,
271    },
272    PushErrorScope {
273        device_id: DeviceId,
274        filter: ErrorFilter,
275    },
276    DispatchError {
277        device_id: DeviceId,
278        error: Error,
279    },
280    PopErrorScope {
281        device_id: DeviceId,
282        callback: GenericCallback<WebGPUPoppedErrorScopeResponse>,
283    },
284    ComputeGetBindGroupLayout {
285        device_id: DeviceId,
286        pipeline_id: ComputePipelineId,
287        index: u32,
288        id: BindGroupLayoutId,
289    },
290    RenderGetBindGroupLayout {
291        device_id: DeviceId,
292        pipeline_id: RenderPipelineId,
293        index: u32,
294        id: BindGroupLayoutId,
295    },
296    CreateQuerySet {
297        device_id: DeviceId,
298        query_set_id: QuerySetId,
299        descriptor: QuerySetDescriptor<'static>,
300    },
301    /// Create planar texture and view to be imported as external texture
302    CreatePlanarTexture {
303        device_id: DeviceId,
304        size: Size2D<u32>,
305        format: SnapshotPixelFormat,
306        texture_id: TextureId,
307        /// aka plane
308        texture_view_id: TextureViewId,
309    },
310    UpdatePlanarTexture {
311        device_id: DeviceId,
312        queue_id: QueueId,
313        texture_id: TextureId,
314        snapshot: SharedSnapshot,
315    },
316    DropPlanarTexture(TextureId, TextureViewId),
317    /// Import plane as external texture, if plane not provided it creates invalid external texture
318    ImportExternalTexture {
319        device_id: DeviceId,
320        external_texture_id: ExternalTextureId,
321        label: String,
322        size: Size2D<u32>,
323        plane0: Option<TextureViewId>,
324    },
325    DestroyExternalTexture(ExternalTextureId),
326    DropExternalTexture(ExternalTextureId),
327    DestroyQuerySet(QuerySetId),
328    CreateRenderBundleEncoder {
329        device_id: DeviceId,
330        render_bundle_encoder_id: RenderBundleEncoderId,
331        desc: RenderBundleEncoderDescriptor<'static>,
332    },
333    RenderBundleEncoderCommand {
334        render_bundle_encoder_id: RenderBundleEncoderId,
335        render_command: RenderBundleEncoderCommand,
336        device_id: DeviceId,
337    },
338    DropRenderBundleEncoder(RenderBundleEncoderId),
339}