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;
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    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, PipelineLayoutId, QuerySetId, QueueId,
34    RenderBundleId, RenderPassEncoderId, RenderPipelineId, SamplerId, ShaderModuleId, TextureId,
35    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, 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    CommandEncoderPushDebugGroup {
117        device_id: DeviceId,
118        command_encoder_id: CommandEncoderId,
119        label: String,
120    },
121    CommandEncoderPopDebugGroup {
122        device_id: DeviceId,
123        command_encoder_id: CommandEncoderId,
124    },
125    CommandEncoderInsertDebugMarker {
126        device_id: DeviceId,
127        command_encoder_id: CommandEncoderId,
128        label: String,
129    },
130    CreateBindGroup {
131        device_id: DeviceId,
132        bind_group_id: BindGroupId,
133        descriptor: BindGroupDescriptor<'static>,
134    },
135    CreateBindGroupLayout {
136        device_id: DeviceId,
137        bind_group_layout_id: BindGroupLayoutId,
138        descriptor: Option<BindGroupLayoutDescriptor<'static>>,
139    },
140    CreateBuffer {
141        device_id: DeviceId,
142        buffer_id: BufferId,
143        descriptor: BufferDescriptor<'static>,
144    },
145    CreateCommandEncoder {
146        device_id: DeviceId,
147        command_encoder_id: CommandEncoderId,
148        desc: CommandEncoderDescriptor<Label<'static>>,
149    },
150    CreateComputePipeline {
151        device_id: DeviceId,
152        compute_pipeline_id: ComputePipelineId,
153        descriptor: ComputePipelineDescriptor<'static>,
154        /// present only on ASYNC versions
155        async_sender: Option<GenericCallback<WebGPUComputePipelineResponse>>,
156    },
157    CreatePipelineLayout {
158        device_id: DeviceId,
159        pipeline_layout_id: PipelineLayoutId,
160        descriptor: PipelineLayoutDescriptor<'static>,
161    },
162    CreateRenderPipeline {
163        device_id: DeviceId,
164        render_pipeline_id: RenderPipelineId,
165        descriptor: RenderPipelineDescriptor<'static>,
166        /// present only on ASYNC versions
167        async_sender: Option<GenericCallback<WebGPURenderPipelineResponse>>,
168    },
169    CreateSampler {
170        device_id: DeviceId,
171        sampler_id: SamplerId,
172        descriptor: SamplerDescriptor<'static>,
173    },
174    CreateShaderModule {
175        device_id: DeviceId,
176        program_id: ShaderModuleId,
177        program: String,
178        label: Option<String>,
179        callback: GenericCallback<Option<ShaderCompilationInfo>>,
180    },
181    /// Creates context
182    CreateContext {
183        buffer_ids: ArrayVec<BufferId, PRESENTATION_BUFFER_COUNT>,
184        size: DeviceIntSize,
185        sender: GenericSender<WebGPUContextId>,
186    },
187    /// Present texture to WebRender
188    Present {
189        context_id: WebGPUContextId,
190        pending_texture: Option<PendingTexture>,
191        size: Size2D<u32>,
192        canvas_epoch: Epoch,
193    },
194    /// Create [`pixels::Snapshot`] with contents of the last present operation
195    /// or provided pending texture and send it over provided [`IpcSender`].
196    GetImage {
197        context_id: WebGPUContextId,
198        pending_texture: Option<PendingTexture>,
199        sender: GenericSender<SharedSnapshot>,
200    },
201    ValidateTextureDescriptor {
202        device_id: DeviceId,
203        texture_id: TextureId,
204        descriptor: TextureDescriptor<'static>,
205    },
206    DestroyContext {
207        context_id: WebGPUContextId,
208    },
209    CreateTexture {
210        device_id: DeviceId,
211        texture_id: TextureId,
212        descriptor: TextureDescriptor<'static>,
213    },
214    CreateTextureView {
215        texture_id: TextureId,
216        texture_view_id: TextureViewId,
217        device_id: DeviceId,
218        descriptor: Option<TextureViewDescriptor<'static>>,
219    },
220    DestroyBuffer(BufferId),
221    DestroyDevice(DeviceId),
222    DestroyTexture(TextureId),
223    DropTexture(TextureId),
224    DropAdapter(AdapterId),
225    DropDevice(DeviceId),
226    DropBuffer(BufferId),
227    DropPipelineLayout(PipelineLayoutId),
228    DropComputePipeline(ComputePipelineId),
229    DropRenderPipeline(RenderPipelineId),
230    DropBindGroup(BindGroupId),
231    DropBindGroupLayout(BindGroupLayoutId),
232    DropCommandEncoder(CommandEncoderId),
233    DropCommandBuffer(CommandBufferId),
234    DropTextureView(TextureViewId),
235    DropSampler(SamplerId),
236    DropShaderModule(ShaderModuleId),
237    DropRenderBundle(RenderBundleId),
238    DropQuerySet(QuerySetId),
239    DropComputePass(ComputePassEncoderId),
240    DropRenderPass(RenderPassEncoderId),
241    Exit(GenericOneshotSender<()>),
242    RenderBundleEncoderFinish {
243        render_bundle_encoder: RenderBundleEncoder,
244        descriptor: RenderBundleDescriptor<'static>,
245        render_bundle_id: RenderBundleId,
246        device_id: DeviceId,
247    },
248    RequestAdapter {
249        sender: GenericCallback<WebGPUAdapterResponse>,
250        options: RequestAdapterOptions,
251        adapter_id: AdapterId,
252    },
253    RequestDevice {
254        sender: GenericCallback<WebGPUDeviceResponse>,
255        adapter_id: WebGPUAdapter,
256        descriptor: DeviceDescriptor<Option<String>>,
257        device_id: DeviceId,
258        queue_id: QueueId,
259        pipeline_id: PipelineId,
260    },
261    // Compute Pass
262    BeginComputePass {
263        command_encoder_id: CommandEncoderId,
264        compute_pass_id: ComputePassId,
265        label: Label<'static>,
266        device_id: DeviceId,
267    },
268    ComputePassSetPipeline {
269        compute_pass_id: ComputePassId,
270        pipeline_id: ComputePipelineId,
271        device_id: DeviceId,
272    },
273    ComputePassSetBindGroup {
274        compute_pass_id: ComputePassId,
275        index: u32,
276        bind_group_id: BindGroupId,
277        offsets: Vec<u32>,
278        device_id: DeviceId,
279    },
280    ComputePassDispatchWorkgroups {
281        compute_pass_id: ComputePassId,
282        x: u32,
283        y: u32,
284        z: u32,
285        device_id: DeviceId,
286    },
287    ComputePassDispatchWorkgroupsIndirect {
288        compute_pass_id: ComputePassId,
289        buffer_id: BufferId,
290        offset: u64,
291        device_id: DeviceId,
292    },
293    ComputePassPushDebugGroup {
294        compute_pass_id: ComputePassId,
295        label: String,
296        device_id: DeviceId,
297    },
298    ComputePassPopDebugGroup {
299        compute_pass_id: ComputePassId,
300        device_id: DeviceId,
301    },
302    ComputePassInsertDebugMarker {
303        compute_pass_id: ComputePassId,
304        label: String,
305        device_id: DeviceId,
306    },
307    EndComputePass {
308        compute_pass_id: ComputePassId,
309        device_id: DeviceId,
310    },
311    // Render Pass
312    BeginRenderPass {
313        command_encoder_id: CommandEncoderId,
314        render_pass_id: RenderPassId,
315        label: Label<'static>,
316        color_attachments: Vec<Option<RenderPassColorAttachment>>,
317        depth_stencil_attachment: Option<RenderPassDepthStencilAttachment<TextureViewId>>,
318        device_id: DeviceId,
319    },
320    RenderPassCommand {
321        render_pass_id: RenderPassId,
322        render_command: RenderCommand,
323        device_id: DeviceId,
324    },
325    EndRenderPass {
326        render_pass_id: RenderPassId,
327        device_id: DeviceId,
328    },
329    Submit {
330        device_id: DeviceId,
331        queue_id: QueueId,
332        command_buffers: Vec<CommandBufferId>,
333    },
334    UnmapBuffer {
335        buffer_id: BufferId,
336        /// Return back mapping for writeback
337        mapping: Option<Mapping>,
338    },
339    WriteBuffer {
340        device_id: DeviceId,
341        queue_id: QueueId,
342        buffer_id: BufferId,
343        buffer_offset: u64,
344        data: GenericSharedMemory,
345    },
346    WriteTexture {
347        device_id: DeviceId,
348        queue_id: QueueId,
349        texture_cv: TexelCopyTextureInfo,
350        data_layout: TexelCopyBufferLayout,
351        size: Extent3d,
352        data: GenericSharedMemory,
353    },
354    QueueOnSubmittedWorkDone {
355        sender: GenericCallback<()>,
356        queue_id: QueueId,
357    },
358    PushErrorScope {
359        device_id: DeviceId,
360        filter: ErrorFilter,
361    },
362    DispatchError {
363        device_id: DeviceId,
364        error: Error,
365    },
366    PopErrorScope {
367        device_id: DeviceId,
368        callback: GenericCallback<WebGPUPoppedErrorScopeResponse>,
369    },
370    ComputeGetBindGroupLayout {
371        device_id: DeviceId,
372        pipeline_id: ComputePipelineId,
373        index: u32,
374        id: BindGroupLayoutId,
375    },
376    RenderGetBindGroupLayout {
377        device_id: DeviceId,
378        pipeline_id: RenderPipelineId,
379        index: u32,
380        id: BindGroupLayoutId,
381    },
382}