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    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    },
57    CommandEncoderFinish {
58        command_encoder_id: CommandEncoderId,
59        device_id: DeviceId,
60        desc: CommandBufferDescriptor<Label<'static>>,
61        command_buffer_id: CommandBufferId,
62    },
63    CommandEncoderCommand {
64        command_encoder_id: CommandEncoderId,
65        command: CommandEncoderCommand,
66        device_id: DeviceId,
67    },
68    CopyExternalImageToTexture {
69        device_id: DeviceId,
70        queue_id: QueueId,
71        usable_source: Option<SharedSnapshot>,
72        destination: TexelCopyTextureInfo,
73        dest_tex_descriptor: TextureDescriptor<'static>,
74        copy_size: Extent3d,
75    },
76    CreateBindGroup {
77        device_id: DeviceId,
78        bind_group_id: BindGroupId,
79        descriptor: BindGroupDescriptor<'static>,
80    },
81    CreateBindGroupLayout {
82        device_id: DeviceId,
83        bind_group_layout_id: BindGroupLayoutId,
84        descriptor: Option<BindGroupLayoutDescriptor<'static>>,
85    },
86    CreateBuffer {
87        device_id: DeviceId,
88        buffer_id: BufferId,
89        descriptor: BufferDescriptor<'static>,
90    },
91    CreateCommandEncoder {
92        device_id: DeviceId,
93        command_encoder_id: CommandEncoderId,
94        desc: CommandEncoderDescriptor<Label<'static>>,
95    },
96    CreateComputePipeline {
97        device_id: DeviceId,
98        compute_pipeline_id: ComputePipelineId,
99        descriptor: ComputePipelineDescriptor<'static>,
100        /// present only on ASYNC versions
101        async_sender: Option<GenericCallback<WebGPUComputePipelineResponse>>,
102    },
103    CreatePipelineLayout {
104        device_id: DeviceId,
105        pipeline_layout_id: PipelineLayoutId,
106        descriptor: PipelineLayoutDescriptor<'static>,
107    },
108    CreateRenderPipeline {
109        device_id: DeviceId,
110        render_pipeline_id: RenderPipelineId,
111        descriptor: RenderPipelineDescriptor<'static>,
112        /// present only on ASYNC versions
113        async_sender: Option<GenericCallback<WebGPURenderPipelineResponse>>,
114    },
115    CreateSampler {
116        device_id: DeviceId,
117        sampler_id: SamplerId,
118        descriptor: SamplerDescriptor<'static>,
119    },
120    CreateShaderModule {
121        device_id: DeviceId,
122        program_id: ShaderModuleId,
123        program: String,
124        label: Option<String>,
125        callback: GenericCallback<Option<ShaderCompilationInfo>>,
126    },
127    /// Creates context
128    CreateContext {
129        buffer_ids: ArrayVec<BufferId, PRESENTATION_BUFFER_COUNT>,
130        size: DeviceIntSize,
131        sender: GenericSender<WebGPUContextId>,
132    },
133    /// Present texture to WebRender
134    Present {
135        context_id: WebGPUContextId,
136        pending_texture: Option<PendingTexture>,
137        size: Size2D<u32>,
138        canvas_epoch: Epoch,
139    },
140    /// Create [`pixels::Snapshot`] with contents of the last present operation
141    /// or provided pending texture and send it over provided [`IpcSender`].
142    GetImage {
143        context_id: WebGPUContextId,
144        pending_texture: Option<PendingTexture>,
145        sender: GenericSender<SharedSnapshot>,
146    },
147    ValidateTextureDescriptor {
148        device_id: DeviceId,
149        texture_id: TextureId,
150        descriptor: TextureDescriptor<'static>,
151    },
152    DestroyContext {
153        context_id: WebGPUContextId,
154    },
155    CreateTexture {
156        device_id: DeviceId,
157        texture_id: TextureId,
158        descriptor: TextureDescriptor<'static>,
159    },
160    CreateTextureView {
161        texture_id: TextureId,
162        texture_view_id: TextureViewId,
163        device_id: DeviceId,
164        descriptor: Option<TextureViewDescriptor<'static>>,
165    },
166    DestroyBuffer(BufferId),
167    DestroyDevice(DeviceId),
168    DestroyTexture(TextureId),
169    DropTexture(TextureId),
170    DropAdapter(AdapterId),
171    DropDevice(DeviceId),
172    DropBuffer(BufferId),
173    DropPipelineLayout(PipelineLayoutId),
174    DropComputePipeline(ComputePipelineId),
175    DropRenderPipeline(RenderPipelineId),
176    DropBindGroup(BindGroupId),
177    DropBindGroupLayout(BindGroupLayoutId),
178    DropCommandEncoder(CommandEncoderId),
179    DropCommandBuffer(CommandBufferId),
180    DropTextureView(TextureViewId),
181    DropSampler(SamplerId),
182    DropShaderModule(ShaderModuleId),
183    DropRenderBundle(RenderBundleId),
184    DropQuerySet(QuerySetId),
185    DropComputePass(ComputePassEncoderId),
186    DropRenderPass(RenderPassEncoderId),
187    Exit(GenericOneshotSender<()>),
188    RenderBundleEncoderFinish {
189        render_bundle_encoder_id: RenderBundleEncoderId,
190        descriptor: RenderBundleDescriptor<'static>,
191        render_bundle_id: RenderBundleId,
192        device_id: DeviceId,
193    },
194    RequestAdapter {
195        sender: GenericCallback<WebGPUAdapterResponse>,
196        options: RequestAdapterOptions,
197        adapter_id: AdapterId,
198    },
199    RequestDevice {
200        sender: GenericCallback<WebGPUDeviceResponse>,
201        adapter_id: WebGPUAdapter,
202        descriptor: DeviceDescriptor<Option<String>>,
203        device_id: DeviceId,
204        queue_id: QueueId,
205        pipeline_id: PipelineId,
206    },
207    // Compute Pass
208    BeginComputePass {
209        command_encoder_id: CommandEncoderId,
210        compute_pass_id: ComputePassEncoderId,
211        label: Label<'static>,
212        timestamp_writes: Option<PassTimestampWrites>,
213        device_id: DeviceId,
214    },
215    ComputePassCommand {
216        compute_pass_id: ComputePassEncoderId,
217        compute_command: ComputePassEncoderCommand,
218        device_id: DeviceId,
219    },
220    EndComputePass {
221        compute_pass_id: ComputePassEncoderId,
222        device_id: DeviceId,
223    },
224    // Render Pass
225    BeginRenderPass {
226        command_encoder_id: CommandEncoderId,
227        render_pass_id: RenderPassEncoderId,
228        label: Label<'static>,
229        color_attachments: Vec<Option<RenderPassColorAttachment>>,
230        depth_stencil_attachment: Option<RenderPassDepthStencilAttachment<TextureViewId>>,
231        timestamp_writes: Option<PassTimestampWrites>,
232        device_id: DeviceId,
233    },
234    RenderPassCommand {
235        render_pass_id: RenderPassEncoderId,
236        render_command: RenderPassEncoderCommand,
237        device_id: DeviceId,
238    },
239    EndRenderPass {
240        render_pass_id: RenderPassEncoderId,
241        device_id: DeviceId,
242    },
243    Submit {
244        device_id: DeviceId,
245        queue_id: QueueId,
246        command_buffers: Vec<CommandBufferId>,
247    },
248    UnmapBuffer {
249        buffer_id: BufferId,
250        /// Return back mapping for writeback
251        mapping: Option<Mapping>,
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}