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