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