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