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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use crate::visibility::visibility_object_allocator::{SlotMapArc, ViewFrustumObjectId};
use crate::RafxResult;
use crossbeam_channel::Sender;
use glam::Vec3;
use parking_lot::{RwLock, RwLockReadGuard};
use rafx_api::RafxError;
use rafx_visibility::{
    AsyncCommand, Projection, ViewFrustumHandle, VisibilityQuery, VisibilityWorldArc, ZoneHandle,
};
use slotmap::SlotMap;
use std::sync::Arc;

#[derive(Clone)]
pub struct ViewFrustumArc {
    inner: Arc<RemoveViewFrustumWhenDropped>,
}

impl ViewFrustumArc {
    pub fn new(
        static_view_frustum: Option<ViewFrustumObjectId>,
        dynamic_view_frustum: Option<ViewFrustumObjectId>,
        storage: SlotMapArc<ViewFrustumObjectId, ViewFrustumObject>,
        visibility_world: VisibilityWorldArc,
    ) -> Self {
        Self {
            inner: Arc::new(RemoveViewFrustumWhenDropped {
                static_view_frustum,
                dynamic_view_frustum,
                storage,
                visibility_world,
                visibility_query: Default::default(),
            }),
        }
    }

    #[allow(dead_code)]
    pub(super) fn set_zone(
        &self,
        static_zone: Option<ZoneHandle>,
        dynamic_zone: Option<ZoneHandle>,
    ) -> &Self {
        let storage = self.inner.storage.read();

        if let Some(static_view_frustum) =
            self.view_frustum(&self.inner.static_view_frustum, &storage)
        {
            static_view_frustum.set_zone(static_zone);
        }

        if let Some(dynamic_view_frustum) =
            self.view_frustum(&self.inner.dynamic_view_frustum, &storage)
        {
            dynamic_view_frustum.set_zone(dynamic_zone);
        }

        self
    }

    pub fn set_transform(
        &self,
        eye: Vec3,
        look_at: Vec3,
        up: Vec3,
    ) -> &Self {
        let storage = self.inner.storage.read();

        if let Some(static_view_frustum) =
            self.view_frustum(&self.inner.static_view_frustum, &storage)
        {
            static_view_frustum.set_transform(eye, look_at, up);
        }

        if let Some(dynamic_view_frustum) =
            self.view_frustum(&self.inner.dynamic_view_frustum, &storage)
        {
            dynamic_view_frustum.set_transform(eye, look_at, up);
        }

        self
    }

    pub fn set_projection(
        &self,
        projection: &Projection,
    ) -> &Self {
        let storage = self.inner.storage.read();

        if let Some(static_view_frustum) =
            self.view_frustum(&self.inner.static_view_frustum, &storage)
        {
            static_view_frustum.set_projection(projection);
        }

        if let Some(dynamic_view_frustum) =
            self.view_frustum(&self.inner.dynamic_view_frustum, &storage)
        {
            dynamic_view_frustum.set_projection(projection);
        }

        self
    }

    pub fn query_visibility(&mut self) -> RafxResult<RwLockReadGuard<VisibilityQuery>> {
        let mut results = self.inner.visibility_query.write();

        results.objects.clear();
        results.volumes.clear();

        self.inner.visibility_world.update();

        let storage = self.inner.storage.read();

        if let Some(static_view_frustum) =
            self.view_frustum(&self.inner.static_view_frustum, &storage)
        {
            static_view_frustum.query_visibility(&mut results)?;
        }

        if let Some(dynamic_view_frustum) =
            self.view_frustum(&self.inner.dynamic_view_frustum, &storage)
        {
            dynamic_view_frustum.query_visibility(&mut results)?;
        }

        std::mem::drop(results);

        Ok(self.inner.visibility_query.read())
    }

    fn view_frustum<'a>(
        &self,
        view_frustum: &Option<ViewFrustumObjectId>,
        storage: &'a SlotMap<ViewFrustumObjectId, ViewFrustumObject>,
    ) -> Option<&'a ViewFrustumObject> {
        view_frustum.as_ref().map(|key| storage.get(*key).unwrap())
    }
}

struct RemoveViewFrustumWhenDropped {
    static_view_frustum: Option<ViewFrustumObjectId>,
    dynamic_view_frustum: Option<ViewFrustumObjectId>,
    storage: SlotMapArc<ViewFrustumObjectId, ViewFrustumObject>,
    visibility_world: VisibilityWorldArc,
    visibility_query: RwLock<VisibilityQuery>,
}

impl Drop for RemoveViewFrustumWhenDropped {
    fn drop(&mut self) {
        // NOTE(dvd): When this inner handle is dropped, we'll remove the key
        // from the storage. That will then destroy the object created in
        // in the visibility world.
        let mut storage = self.storage.write();
        if let Some(id) = self.static_view_frustum {
            storage.remove(id).unwrap();
        }
        if let Some(id) = self.dynamic_view_frustum {
            storage.remove(id).unwrap();
        }
    }
}

pub struct ViewFrustumObject {
    commands: Sender<AsyncCommand>,
    visibility_world: VisibilityWorldArc,
    handle: ViewFrustumHandle,
}

impl ViewFrustumObject {
    pub fn new(
        handle: ViewFrustumHandle,
        commands: Sender<AsyncCommand>,
        visibility_world: VisibilityWorldArc,
    ) -> Self {
        Self {
            handle,
            commands,
            visibility_world,
        }
    }

    #[allow(dead_code)]
    pub(super) fn set_zone(
        &self,
        zone: Option<ZoneHandle>,
    ) -> &Self {
        self.commands
            .send(AsyncCommand::SetViewFrustumZone(self.handle, zone))
            .expect("Unable to send SetViewFrustumZone command.");
        self
    }

    pub fn set_transform(
        &self,
        eye: Vec3,
        look_at: Vec3,
        up: Vec3,
    ) -> &Self {
        self.commands
            .send(AsyncCommand::SetViewFrustumTransforms(
                self.handle,
                eye,
                look_at,
                up,
            ))
            .expect("Unable to send SetViewFrustumTransforms command.");
        self
    }

    pub fn set_projection(
        &self,
        projection: &Projection,
    ) -> &Self {
        self.commands
            .send(AsyncCommand::SetViewFrustumProjection(
                self.handle,
                projection.clone(),
            ))
            .expect("Unable to send SetViewFrustumProjection command.");
        self
    }

    pub fn query_visibility(
        &self,
        results: &mut VisibilityQuery,
    ) -> RafxResult<()> {
        self.visibility_world
            .query_visibility(self.handle, results)
            .map_err(|_err| RafxError::StringError("Unable to query visibility.".to_string()))?;
        Ok(())
    }
}

impl Drop for ViewFrustumObject {
    fn drop(&mut self) {
        let _ = self
            .commands
            .send(AsyncCommand::DestroyViewFrustum(self.handle));
    }
}