1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use ipc_channel::ipc::IpcSender;
use msg::constellation_msg::PipelineId;
use serde::{Deserialize, Serialize};

use crate::wgpu::id::{
    AdapterId, BindGroupId, BindGroupLayoutId, BufferId, CommandBufferId, ComputePipelineId,
    DeviceId, PipelineLayoutId, QuerySetId, RenderBundleId, RenderPipelineId, SamplerId,
    ShaderModuleId, StagingBufferId, SurfaceId, TextureId, TextureViewId, TypedId,
};
use crate::wgpu::identity::{
    GlobalIdentityHandlerFactory, IdentityHandler, IdentityHandlerFactory,
};
use crate::wgt::Backend;
use crate::{ErrorScopeId, WebGPUDevice, WebGPURequest};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum WebGPUOpResult {
    ValidationError(String),
    OutOfMemoryError,
    Success,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum WebGPUMsg {
    FreeAdapter(AdapterId),
    FreeDevice(DeviceId),
    FreeBuffer(BufferId),
    FreePipelineLayout(PipelineLayoutId),
    FreeComputePipeline(ComputePipelineId),
    FreeRenderPipeline(RenderPipelineId),
    FreeBindGroup(BindGroupId),
    FreeBindGroupLayout(BindGroupLayoutId),
    FreeCommandBuffer(CommandBufferId),
    FreeTexture(TextureId),
    FreeTextureView(TextureViewId),
    FreeSampler(SamplerId),
    FreeSurface(SurfaceId),
    FreeShaderModule(ShaderModuleId),
    FreeRenderBundle(RenderBundleId),
    FreeStagingBuffer(StagingBufferId),
    FreeQuerySet(QuerySetId),
    WebGPUOpResult {
        device: WebGPUDevice,
        scope_id: Option<ErrorScopeId>,
        pipeline_id: PipelineId,
        result: WebGPUOpResult,
    },
    CleanDevice {
        device: WebGPUDevice,
        pipeline_id: PipelineId,
    },
    Exit,
}

#[derive(Debug)]
pub struct IdentityRecycler {
    sender: IpcSender<WebGPUMsg>,
    self_sender: IpcSender<(Option<ErrorScopeId>, WebGPURequest)>,
}

pub struct IdentityRecyclerFactory {
    pub sender: IpcSender<WebGPUMsg>,
    pub self_sender: IpcSender<(Option<ErrorScopeId>, WebGPURequest)>,
}

macro_rules! impl_identity_handler {
    ($id:ty, $st:tt, $($var:tt)*) => {
        impl IdentityHandler<$id> for IdentityRecycler {
            type Input = $id;
            fn process(&self, id: $id, _backend: Backend) -> Self::Input {
                log::debug!("process {} {:?}", $st, id);
                //debug_assert_eq!(id.unzip().2, backend);
                id
            }
            fn free(&self, id: $id) {
                log::debug!("free {} {:?}", $st, id);
                let msg = $($var)*(id);
                if self.sender.send(msg.clone()).is_err() {
                    log::error!("Failed to send {:?}", msg);
                }
            }
        }
    };
}

impl_identity_handler!(AdapterId, "adapter", WebGPUMsg::FreeAdapter);
impl_identity_handler!(SurfaceId, "surface", WebGPUMsg::FreeSurface);
impl_identity_handler!(SamplerId, "sampler", WebGPUMsg::FreeSampler);
impl_identity_handler!(TextureId, "texture", WebGPUMsg::FreeTexture);
impl_identity_handler!(TextureViewId, "texture_view", WebGPUMsg::FreeTextureView);
impl_identity_handler!(BufferId, "buffer", WebGPUMsg::FreeBuffer);
impl_identity_handler!(BindGroupId, "bind_group", WebGPUMsg::FreeBindGroup);
impl_identity_handler!(ShaderModuleId, "shader_module", WebGPUMsg::FreeShaderModule);
impl_identity_handler!(RenderBundleId, "render_bundle", WebGPUMsg::FreeRenderBundle);
impl_identity_handler!(
    StagingBufferId,
    "staging_buffer",
    WebGPUMsg::FreeStagingBuffer
);
impl_identity_handler!(QuerySetId, "quary_set", WebGPUMsg::FreeQuerySet);
impl_identity_handler!(
    RenderPipelineId,
    "render_pipeline",
    WebGPUMsg::FreeRenderPipeline
);
impl_identity_handler!(
    ComputePipelineId,
    "compute_pipeline",
    WebGPUMsg::FreeComputePipeline
);
impl_identity_handler!(
    CommandBufferId,
    "command_buffer",
    WebGPUMsg::FreeCommandBuffer
);
impl_identity_handler!(
    BindGroupLayoutId,
    "bind_group_layout",
    WebGPUMsg::FreeBindGroupLayout
);
impl_identity_handler!(
    PipelineLayoutId,
    "pipeline_layout",
    WebGPUMsg::FreePipelineLayout
);

impl IdentityHandler<DeviceId> for IdentityRecycler {
    type Input = DeviceId;
    fn process(&self, id: DeviceId, _backend: Backend) -> Self::Input {
        log::debug!("process device {:?}", id);
        //debug_assert_eq!(id.unzip().2, backend);
        id
    }
    fn free(&self, id: DeviceId) {
        log::debug!("free device {:?}", id);
        if self.sender.send(WebGPUMsg::FreeDevice(id)).is_err() {
            log::error!("Failed to send FreeDevice({:?}) to script", id);
        }
        if self
            .self_sender
            .send((None, WebGPURequest::FreeDevice(id)))
            .is_err()
        {
            log::error!("Failed to send FreeDevice({:?}) to server", id);
        }
    }
}

impl<I: TypedId + Clone + std::fmt::Debug> IdentityHandlerFactory<I> for IdentityRecyclerFactory
where
    I: TypedId + Clone + std::fmt::Debug,
    IdentityRecycler: IdentityHandler<I>,
{
    type Filter = IdentityRecycler;
    fn spawn(&self) -> Self::Filter {
        IdentityRecycler {
            sender: self.sender.clone(),
            self_sender: self.self_sender.clone(),
        }
    }
}

impl GlobalIdentityHandlerFactory for IdentityRecyclerFactory {
    fn ids_are_generated_in_wgpu() -> bool {
        false
    }
}